How to Write the Perfect, Optimized Query: A Quick and Practical Guide

September 26, 2025 | by dbsnoop

How to Write the Perfect, Optimized Query: A Quick and Practical Guide

In the world of databases, there is a fundamental difference between a query that works and a query that is good. A query that “works” simply returns the correct data. A “good” query does the same, but efficiently, scalably, and with minimal impact on server resources. The first solves an immediate problem; the second builds a sustainable application. Most performance crises that bring down production environments are not caused by hardware failures, but by an accumulation of queries that “work” but are, in fact, inefficiency time bombs.

Writing an optimized query is not a dark art reserved for senior DBAs. It is a discipline based on a set of fundamental principles. Ignoring them guarantees that your application, sooner or later, will become a bottleneck. This practical guide presents the essential commandments for writing the perfect query, transforming your code from a simple data request into a high-performance instruction.

The 5 Commandments of the Optimized Query

1. Thou Shalt Select Only What is Necessary (SELECT columns vs. SELECT *)

This is the most common and easiest mistake to fix. SELECT * is a convenient shortcut during development, but a poison in production.

Why is it bad?

  • Network Overhead: You are transferring columns that your application won’t even use, consuming unnecessary network bandwidth.
  • Memory Pressure: The database needs to load all columns into memory (in the Buffer Pool/Cache), even those that are not needed, which can “evict” more important data.
  • Prevents the Use of Covering Indexes: A “covering index” is an index that contains all the columns the query needs. If you use one, the database can answer your question by reading only the index without ever touching the table, which is absurdly fast. SELECT * makes this optimization impossible.

The Right Way:

-- Bad:
SELECT * FROM Orders WHERE CustomerID = 123;

-- Good:
SELECT OrderID, OrderDate, TotalAmount FROM Orders WHERE CustomerID = 123;

2. Thou Shalt Filter Intelligently (SARGable WHERE Clauses)

The WHERE clause is your most powerful tool to tell the database optimizer to do less work. The efficiency of your filter depends on whether it is “SARGable” (Search Argument Able). A predicate is SARGable if the optimizer can use an index to satisfy it.

What makes a WHERE clause NON-SARGable? Primarily, applying functions to the column being filtered.

The Right Way:

-- BAD: The optimizer cannot use an index on OrderDate.
-- It must execute the YEAR() function for EVERY ROW in the table.
SELECT OrderID, TotalAmount FROM Orders WHERE YEAR(OrderDate) = 2023;

-- GOOD: The optimizer can use an index on OrderDate to
-- go directly to the date range.
SELECT OrderID, TotalAmount FROM Orders
WHERE OrderDate >= '2023-01-01' AND OrderDate < '2024-01-01';

3. Thou Shalt Join with Precision (JOIN on Indexed Columns)

JOINs are powerful but can be the biggest source of slowness if misused. The rule is simple: the columns used in the ON conditions (t1.CustomerID = t2.ID) should almost always be indexed. Without an index, the database is forced to use inefficient join methods, such as a “Nested Loop” on an unindexed table, which compares every row from the first table with every row from the second.

4. Thou Shalt Think in Sets, Not in Loops

Developers coming from procedural languages (Java, C#, Python) often try to solve database problems the same way they would in the application: fetching a set of data and then iterating over it with a CURSOR or WHILE loop to apply logic. This is an anti-pattern in SQL. SQL is a declarative language designed to operate on sets of data. Almost anything that can be done with a cursor can be rewritten as a single UPDATE or MERGE statement, which will be orders of magnitude faster.

5. Thou Shalt Use EXISTS for Existence Checks

When you need to fetch records from one table (TableA) only if they have a match in another (TableB), it’s common to use the IN syntax. However, for large subqueries, EXISTS is generally more performant.

Why? IN forces the subquery to collect all matching results first. EXISTS simply checks for existence and stops as soon as it finds the first match, which can be much more efficient.

The Right Way:

-- OK, but can be slow if the Sales table is huge:
SELECT Name FROM Customers WHERE CustomerID IN (SELECT CustomerID FROM Sales);

-- Generally better, as it stops searching after the first sale is found:
SELECT c.Name FROM Customers c WHERE EXISTS (SELECT 1 FROM Sales v WHERE v.CustomerID = c.CustomerID);

The Final Test: The Execution Plan Doesn’t Lie

How do you know if your query is truly good? You ask the database itself. The Execution Plan is the map that the query optimizer creates to retrieve the data. Analyzing it is the final proof.

  • Look for Index Seek: This is your goal. It means the database used an index to jump directly to the data it needed.
  • Beware of Table Scan or Index Scan: This means the database had to read the entire table or index. It’s the equivalent of reading a book from start to finish to find a single word. It is the clearest sign of an inefficient query.

From Manual Writing to Assisted Optimization with dbsnOOp

Following these commandments is the homework of every good developer and DBA. But in a complex environment with thousands of queries, manually analyzing each execution plan is impractical.

dbsnOOp acts as the performance expert that continuously reviews your code.

  • Proactive Analysis: The platform automatically identifies queries that are using inefficient execution plans (like Table Scans) and that represent the highest cost to the server, even if they aren’t the “slowest.”
  • Intelligent Recommendations: dbsnOOp goes beyond diagnosis. It analyzes the inefficient query and recommends the exact corrective action, such as the CREATE INDEX script needed to transform a costly Table Scan into a surgical Index Seek.

The perfect query is not a myth. It is the result of applying solid principles and continuous validation.

Schedule a meeting with our specialist or watch a live demo!

Schedule a demo here.

Learn more about dbsnOOp!

Learn about database monitoring with advanced tools here.

Visit our YouTube channel to learn about the platform and watch tutorials.

Recommended Reading

  • Generate SQL Queries in Seconds: After learning to write queries manually, explore how Artificial Intelligence can accelerate this process, helping to create optimized SQL from the start.
  • SQL Server Fine-Tuning: Optimizing a single query is part of a larger discipline. This article delves into other tuning techniques in SQL Server to ensure the performance of the entire environment.
  • AI Database Tuning: Understand the philosophy behind continuous and automated optimization. This post explains how AI can proactively identify problematic queries in your workload and recommend the best solutions.
Share

Read more

MONITOR YOUR ASSETS WITH FLIGHTDECK

NO INSTALL – 100% SAAS

Complete the form below to proceed

*Mandatory