The Fastest SQL Queries Make Most of the Data Irrelevant
Hatched by Kai Nguyen
Aug 12, 2026
12 min read
0 views
88%
The database is not slow. Your question may be badly shaped.
What if the most important SQL optimization is not choosing a better index, but asking a question the database can answer without doing unnecessary work?
That sounds obvious until you look at how queries are written. A developer often begins with the result they want: the top three products in each category, customers whose spending exceeds a threshold, or orders from the previous month. Then they add joins, calculations, ranking functions, sorting, and filters until the result appears. The query may be logically correct, readable, and even elegant. Yet the database may still be forced to examine millions of rows, calculate values it will later discard, and sort data that never needed to be sorted.
This reveals a deeper tension in SQL. SQL is both a language for expressing meaning and a set of instructions that must eventually be executed by a machine. The best queries respect both sides. They make the intended result clear while presenting that intent in a shape that allows the database to avoid work.
The central skill is therefore not memorizing isolated rules such as “use indexes” or “write common table expressions.” It is learning to control the shape, timing, and scope of computation.
A fast query is not merely a correct question. It is a question whose unnecessary work has been made impossible.
Logical order is a map of the work
SQL looks as if it runs from top to bottom. You write SELECT, then FROM, then WHERE, then perhaps GROUP BY and ORDER BY. But the database reasons about the query in a different logical sequence.
A simplified model is:
- Choose the source rows with
FROMandJOIN. - Filter rows with
WHERE. - Form groups with
GROUP BY. - Filter groups with
HAVING. - Calculate the requested expressions in
SELECT. - Remove duplicates with
DISTINCTif required. - Sort with
ORDER BY. - Return only the requested portion with
LIMITor an equivalent clause.
The exact physical execution may differ because the optimizer can rearrange operations. Still, this logical order is a powerful mental model. It shows that a query is not one undifferentiated instruction. It is a pipeline of transformations, and each stage determines how much data the next stage must handle.
Imagine a warehouse with ten million boxes. You need to find the most valuable blue items shipped last month. One approach is to bring every box into a central room, inspect it, calculate its value, sort everything, and then discard most of the boxes. Another approach is to use the warehouse catalog to go directly to recent blue shipments, calculate value only for those items, and rank the reduced set.
SQL optimization is largely the difference between these two approaches.
The earlier a condition can reduce the candidate set, the less work later operations must perform. Joins become smaller. Aggregations process fewer rows. Window functions rank fewer records. Sorting becomes cheaper. Memory pressure falls. Sometimes an entire stage disappears because an index can provide the rows in the needed order.
This is why “filter early” is more than a style preference. It is a statement about the growth of work through a pipeline. If a table contains ten million rows and a date filter reduces it to fifty thousand, every subsequent operation has just become dramatically less expensive. If the filter is applied only after a join or a ranking operation, the database may already have paid the cost of processing the other 9,950,000 rows.
There is an important qualification: the logical order is not a command to force a particular physical plan. Modern optimizers can push predicates down, reorder joins, and simplify expressions on their own. But clear query structure gives the optimizer more obvious opportunities, and it gives the human reviewer a way to identify accidental work.
SARGability: write conditions that preserve navigation
An index is often described as a book index. That analogy is useful, but incomplete. An index does not simply make data “faster.” It gives the database a navigable structure for locating values without inspecting every row.
A condition is commonly called SARGable, meaning that it is suitable for use as a search argument. In practice, the condition preserves the indexed column in a form the database can navigate efficiently.
Consider a table called orders with an index on created_at.
SELECT *
FROM orders
WHERE DATE(created_at) = '2026-08-01';
This asks the database to apply a function to created_at before comparing it. Depending on the database system and available indexes, the engine may be unable to use the ordinary index efficiently because it cannot directly seek to the relevant range of raw timestamps.
A range expression usually preserves that navigability:
SELECT *
FROM orders
WHERE created_at >= '2026-08-01'
AND created_at < '2026-08-02';
The two queries express nearly the same business idea, but they expose different computational paths. The second describes a continuous interval in the indexed value. The database can often seek to the beginning of the interval and scan until the end.
The same principle explains several familiar anti patterns:
- Applying arithmetic to an indexed column, such as
price * 1.2 > 100. - Applying a function to an indexed column when a range or transformed parameter would work.
- Negating a condition, such as
NOT status = 'cancelled', when a more selective positive condition is available. - Searching with a leading wildcard, such as
name LIKE '%son', because the beginning of the indexed value is unknown.
The issue is not that functions, negation, or wildcards are morally wrong. The issue is that they can destroy the ordering information an index provides. If the database must transform every value before it knows whether the row qualifies, the index may no longer function as a shortcut.
A useful test is this:
Can the database locate the relevant region of the index before evaluating the expression for every row?
If yes, the predicate is likely preserving a useful access path. If no, the query may be asking the database to turn its map into a pile of unexamined documents.
This also clarifies why indexes are not magic. An index on a column that is wrapped in an opaque expression may be practically invisible to the query. An index on a column with very low selectivity may not reduce enough work to justify its use. And an index can help one stage while making writes and storage more expensive. Optimization is not “add indexes everywhere.” It is matching the structure of the question to the structure of the data.
CTEs and window functions: readability can reveal the right computation
Performance advice sometimes creates a false opposition between readable SQL and fast SQL. Developers learn that a query should be compact, then compress several ideas into nested subqueries. The result may be shorter but harder to inspect, modify, and optimize mentally.
Common table expressions, introduced with WITH, offer a different possibility. A CTE can name an intermediate relation and make the query’s stages explicit:
WITH recent_orders AS (
SELECT customer_id, order_id, total_amount, created_at
FROM orders
WHERE created_at >= '2026-07-01'
AND created_at < '2026-08-01'
), customer_totals AS (
SELECT customer_id,
SUM(total_amount) AS total_spent
FROM recent_orders
GROUP BY customer_id
)
SELECT customer_id, total_spent
FROM customer_totals
WHERE total_spent > 1000;
The value of this structure is not automatically performance. Different database systems, versions, and query plans may inline a CTE, materialize it, or treat it differently based on context. The deeper value is that the CTE exposes the data reduction boundary. It tells the reader that only July orders belong in the later aggregation.
A CTE is therefore best understood as a reasoning tool first and a performance tool second. It lets you ask precise questions:
- Is the initial filter selective enough?
- Does the aggregation need all columns carried forward?
- Is the intermediate result reused?
- Would materializing it reduce repeated work, or would it create an unnecessary temporary result?
Window functions add another layer of expressive power. An aggregate function collapses rows into a smaller result. A window function calculates across a related set while preserving the individual rows. That distinction is crucial.
Suppose the goal is to find the three highest value orders for each customer. A global ORDER BY total_amount DESC LIMIT 3 returns only three orders overall. The business question is partitioned: three per customer. A window function can encode that structure directly:
WITH ranked_orders AS (
SELECT
customer_id,
order_id,
total_amount,
RANK() OVER (
PARTITION BY customer_id
ORDER BY total_amount DESC
) AS customer_rank
FROM orders
WHERE created_at >= '2026-07-01'
AND created_at < '2026-08-01'
)
SELECT customer_id, order_id, total_amount
FROM ranked_orders
WHERE customer_rank <= 3;
Here, PARTITION BY defines the groups over which the ranking is calculated. RANK expresses the ordering within each group. The outer filter selects the desired rank after the calculation has been made.
This query is powerful because it separates three different ideas that are often confused:
- Candidate selection: Which rows are eligible, such as orders from July?
- Analytical context: Within which group should rows be compared, such as each customer?
- Selection of winners: Which positions matter, such as ranks one through three?
That separation is not just aesthetically pleasing. It prevents a common performance and correctness mistake: ranking a much larger universe than necessary. If the business rule concerns July orders, applying the date filter before the window function reduces the window’s input. The database does not need to compare current orders against historical orders that cannot appear in the result.
There are still costs. Window functions often require sorting or organizing rows by their partition and ordering columns. Ranking a billion rows is expensive even when the final answer contains a few dozen. The right question is not “Are window functions fast?” It is “Have I reduced the rows and columns entering the window operation, and have I chosen the right partition?”
The three shapes of an efficient query
A practical way to design SQL is to think in three shapes: the candidate shape, the computation shape, and the result shape.
The candidate shape answers: what is the smallest defensible set of rows that could contain the answer? This is where date ranges, status conditions, tenant boundaries, and join restrictions belong. It is also where SARGability matters most. A vague or non navigable predicate enlarges the candidate shape and sends unnecessary data downstream.
The computation shape answers: what operation transforms candidates into meaning? Is the task aggregation, ranking, comparison with a previous row, or positional analysis? Window functions are especially useful here because they let you describe group aware computation without prematurely collapsing the data.
The result shape answers: what must actually be returned? If the consumer needs ten columns, do not carry fifty through every stage. If it needs the first page, do not sort and return an enormous result set. If it needs a summary, do not preserve detail rows beyond the point where they are useful.
These shapes create a simple optimization discipline:
Reduce candidates first.
Choose the computation that matches the question.
Return only what the consumer needs.
Consider a dashboard that asks for the highest spending customer in each region during the previous quarter. A weak design might join every customer to every order, calculate lifetime totals, rank all customers, and filter by region and date at the end. A stronger design first restricts orders to the quarter, aggregates by customer and region, then ranks the smaller customer summary within each region.
WITH quarter_totals AS (
SELECT
c.region,
o.customer_id,
SUM(o.total_amount) AS quarter_spend
FROM orders AS o
JOIN customers AS c
ON c.customer_id = o.customer_id
WHERE o.created_at >= '2026-04-01'
AND o.created_at < '2026-07-01'
GROUP BY c.region, o.customer_id
), ranked_customers AS (
SELECT
region,
customer_id,
quarter_spend,
RANK() OVER (
PARTITION BY region
ORDER BY quarter_spend DESC
) AS region_rank
FROM quarter_totals
)
SELECT region, customer_id, quarter_spend
FROM ranked_customers
WHERE region_rank = 1;
The query is not merely organized into neat blocks. It mirrors the information theory of the problem. The date filter removes irrelevant history. The aggregation compresses many orders into one row per customer and region. The window function ranks summaries rather than raw transactions. Each stage reduces or clarifies the data before the next stage begins.
This is a general pattern for analytical SQL: filter, compress, compare, select. It is not universally optimal, and execution plans must confirm the result. But it is a strong default because it aligns the query’s structure with the natural flow of information.
Key Takeaways
-
Treat logical execution order as a work budget. Every row that survives one stage becomes potential work for the next stage. Ask where you can safely reduce the candidate set.
-
Preserve index navigability. Prefer range predicates on indexed columns over expressions that transform the column for every row. Check whether the database can seek before it scans.
-
Use CTEs to expose reasoning boundaries. A CTE can make filtering, aggregation, and ranking understandable. Do not assume it is automatically faster or slower; inspect the execution plan.
-
Match the analytical tool to the question. Aggregates collapse rows. Window functions calculate across partitions while preserving row detail.
PARTITION BYis the explicit expression of “within each group.” -
Reduce before expensive operations. Filter before joining when appropriate, aggregate before ranking when the question concerns summaries, limit selected columns, and avoid sorting data that will later be discarded.
Performance is a form of clarity
The deepest lesson is that SQL optimization is not a separate technical ritual performed after a query has been written. It begins when the question is modeled.
A query becomes expensive when its wording hides the boundaries of relevance. It becomes efficient when the database can see which rows matter, which relationships matter, which comparisons matter, and when each calculation becomes necessary. SARGability preserves the map. CTEs expose the stages. Window functions preserve the context in which comparisons make sense. Logical execution order reveals where work expands.
The surprising connection is that these are all forms of the same discipline: make information narrower before making it more elaborate.
A database does not reward cleverness for its own sake. It rewards questions that eliminate impossibilities early. The best SQL is not the query with the fewest characters, nor even the query with the most sophisticated syntax. It is the query that expresses the business question while refusing to perform computations outside that question’s boundaries.
Good SQL does not merely tell the database what answer to produce. It tells the database what it is allowed to ignore.
Once you see queries this way, optimization stops being a collection of disconnected rules. It becomes an art of shaping attention: narrow the world, preserve the paths through it, compute within the right groups, and return only what matters. That is how a declarative language becomes operationally intelligent.
Sources
Hatch New Ideas with Glasp AI 🐣
Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)
Start Hatching 🐣