The Fastest SQL Query Is the One That Shrinks the World First
Hatched by Kai Nguyen
Aug 28, 2026
10 min read
2 views
94%
What if the most important performance decision in a SQL query is not the index, the database engine, or even the hardware? What if it is the moment when you decide what counts as relevant?
A database query looks like a request for information. In practice, it is a plan for reducing a large world to a small answer. The quality of that plan depends on two questions that are easy to confuse:
- Which rows belong in the answer?
- What form must the answer take before it can be useful?
The first question is handled by predicates such as WHERE. The second involves ordering, deduplication, grouping, and calculation. These operations may look like simple pieces of SQL syntax, but they express different kinds of work. Some reduce the number of candidates. Others rearrange or transform candidates that have already survived.
That distinction produces a powerful general rule:
Efficient queries do not merely ask for less information. They arrange the work so that the database can discard irrelevant information as early, cheaply, and decisively as possible.
This is why a small change in the shape of a condition can matter more than a sophisticated optimization trick. It is also why correctness and performance are connected more deeply than they first appear. A query that fails to state its ordering, uniqueness, or filtering requirements clearly is not only harder to optimize. It is making a weaker claim about reality.
A query is a pipeline of decisions, not a sentence
SQL is written in an order that resembles ordinary language: SELECT, FROM, WHERE, ORDER BY. But the database does not treat the statement as a sentence read from left to right. Conceptually, it builds an answer through stages.
First, it identifies the rows under consideration. Then it evaluates Boolean conditions to decide which rows survive. Later, it may remove duplicates, sort the survivors, or calculate a final presentation. The exact physical strategy can vary, but the conceptual distinction remains useful: selection reduces the search space, while later operations shape the result.
Consider a library table called books:
SELECT title, publication_year
FROM books
WHERE genre = 'History'
ORDER BY publication_year DESC;
The WHERE clause asks a membership question: does this row satisfy the condition? The ORDER BY clause asks a presentation question: in what sequence should the qualifying rows appear?
These questions have different computational profiles. Filtering can often be performed with an index that points directly to qualifying rows. Sorting usually requires comparing and arranging the qualifying rows, unless an existing index already provides the needed order. If the filter eliminates ninety nine percent of the table, sorting the remaining one percent is manageable. If the filter is applied late, the system may be forced to carry a much larger intermediate result into an expensive operation.
This is the database version of a broader principle found in search, statistics, and decision making: reduce uncertainty before you perform costly transformations.
Imagine searching a warehouse for red ceramic mugs. The efficient strategy is not to load every object onto a conveyor belt, alphabetize the objects by manufacturer, and only then check their color and material. It is to use the strongest available constraints first, shrink the pile, and sort only what remains.
SQL optimization is often the engineering discipline of asking: Which operation can make the pile smaller before the next operation begins?
The hidden difference between a filter and a disguise
A Boolean expression in a WHERE clause evaluates to true or false. That sounds straightforward, but not every expression gives the database an equally useful route to the answer.
Suppose a table has an indexed column called publication_year. These two conditions may look equivalent:
WHERE publication_year >= 2000
and:
WHERE publication_year + 1 >= 2001
The same rows may qualify. Yet the second expression wraps the indexed column in arithmetic. Instead of comparing the stored value directly with a boundary, the database may need to calculate a transformed value for many rows before it can determine whether each row qualifies.
The issue is not that arithmetic is inherently expensive. The issue is that the expression hides the column's natural ordering. An index is useful because it organizes values in a way that supports rapid navigation. When a query applies a function or calculation to those values, the original organization may no longer answer the question directly.
This is the central idea behind SARGABLE predicates, expressions that are shaped so the database can use an index to search for qualifying values. A SARGABLE condition preserves the relationship between the stored data and the test being made.
Compare:
WHERE order_date >= '2026-01-01'
with:
WHERE YEAR(order_date) = 2026
The first describes a range in the native order of the date column. The second asks the database to derive a year from each date. A function based index or specialized database feature may make the second form efficient, but without such support, the first form more clearly exposes the searchable structure.
The same pattern appears in several common forms:
WHERE price * 1.2 < 100
can often be rewritten as:
WHERE price < 83.3333
A condition such as:
WHERE NOT status = 'archived'
may be less selective and less directly navigable than a positive condition that identifies a small set of desired states. And a pattern such as:
WHERE title LIKE '%database'
cannot usually use a conventional index to jump to a starting point, because the first character is unknown. By contrast:
WHERE title LIKE 'Database%'
provides a searchable prefix.
These examples reveal a useful mental model: an index is not a magic lookup table. It is a map, and a query is efficient when it gives the map a recognizable destination.
The three kinds of work hidden in a simple query
Many SQL mistakes become clearer if query operations are divided into three categories.
1. Reduction
Reduction removes rows or values that are not needed. WHERE is the most important example. A good reduction condition is selective, direct, and compatible with an available index.
2. Arrangement
Arrangement changes the order or structure of what remains. ORDER BY is the obvious case. Sorting is not implied by the physical order of rows in a table. Unless an order is explicitly requested, rows have no guaranteed sequence.
This is more than a performance detail. It is a correctness issue. If an application displays the newest books first, the query must say so:
SELECT title, publication_year
FROM books
ORDER BY publication_year DESC;
Without ORDER BY, an accidental order may appear stable during testing and then change after an index is added, data is reorganized, or the execution plan changes. The database has not become unreliable. The query made no promise about sequence.
3. Compression or transformation
DISTINCT compresses repeated values into a set of unique results:
SELECT DISTINCT genre
FROM books;
This is not merely a cosmetic modifier. It changes the level of detail in the answer. The original table may contain hundreds of books, but the result now represents genres, not books.
That shift is easy to underestimate. If a table contains multiple books in the same genre, DISTINCT genre intentionally discards multiplicity. The result can no longer answer questions such as how many books belong to each genre or which title produced a particular row. Deduplication is therefore a form of information loss, even when it is exactly the desired loss.
The distinction matters for performance because arrangement and compression usually operate on an intermediate result. If you can reduce the rows first, there is less material to sort or deduplicate. For example:
SELECT DISTINCT genre
FROM books
WHERE publication_year >= 2000;
usually gives the system a smaller population to process than a plan that carries every book into a later transformation.
This gives us a compact formula for query design:
First reduce the population. Then arrange or compress the survivors. Finally calculate what the user actually needs.
It is not an absolute law. Query optimizers can reorder operations when semantics allow it, and an index may make a later operation nearly free. But as a design principle, it directs attention toward the most valuable question: where can irrelevant data be eliminated?
Why “correct” and “fast” are often the same design problem
Performance advice is frequently presented as a collection of mechanical rules: add an index, avoid a leading wildcard, limit the result set, remove unnecessary sorting. The deeper connection is that each rule concerns the shape of the claim made by the query.
A query that says:
SELECT *
FROM books
WHERE genre = 'History';
requests every available column. Perhaps the application needs only title and publication_year. Returning unnecessary columns increases data movement and may prevent certain covering index strategies. More importantly, SELECT * hides the real information requirement. The database and the future maintainer must infer what the consumer actually needs.
Likewise, a query that uses DISTINCT to silence duplicate rows may be technically valid while concealing an unclear relationship in the underlying data. Are duplicates genuinely irrelevant, or did an unintended join multiply rows? Deduplication can improve the output while making the cause of the problem harder to see.
The same tension appears in sorting. If a result is displayed to a user, explicit order is part of correctness. If no consumer cares about order, unnecessary sorting is wasted work. The question is not simply whether sorting is expensive. It is whether the query is paying for a guarantee that nobody requested.
We can describe this as the semantic budget principle:
Every guarantee in a query has a computational cost, and every omitted guarantee creates a correctness risk. Spend guarantees deliberately.
ORDER BY spends resources to provide a stable sequence. DISTINCT spends resources to provide uniqueness. Broad expressions spend resources to compute derived values. A well designed query pays only for guarantees that matter, while stating all guarantees that users rely on.
This principle also explains why limiting results is so powerful. If an interface shows the first twenty records, ask the database for twenty records, not ten thousand records that the application will discard. The limit is not merely a user interface preference. It is a statement about the size of the answer, and it can prevent unnecessary work throughout the pipeline.
A practical framework: preserve, prune, and promise
When writing or reviewing a query, use three questions.
Preserve the searchable structure
Can the database compare the indexed column directly with a value or range? Avoid unnecessary arithmetic, functions, and transformations on indexed columns. Prefer conditions that expose the column's natural order. If a transformation is unavoidable, investigate a function based index, a generated column, or another database specific feature rather than assuming the engine will solve it automatically.
Prune before expensive work
Apply selective filters as early as the query's meaning allows. Return only the columns needed. Limit the result set when the consumer needs only a page or a small sample. Treat sorting, grouping, deduplication, and calculations as operations that should receive the smallest sensible input.
Promise only what the consumer needs, but promise it explicitly
Use ORDER BY whenever sequence matters. Use DISTINCT when uniqueness is part of the intended result, not merely because duplicates are surprising. Avoid expensive guarantees that have no semantic value, but do not rely on accidental physical behavior for guarantees that users can observe.
A review based on these questions is often more revealing than staring at the SQL text. Ask what the query preserves, what it discards, and what it promises. Then inspect the execution plan to see whether the physical strategy reflects those intentions.
Key Takeaways
- Think of a query as a reduction pipeline. Filter the candidate rows before sorting, deduplicating, or performing costly calculations whenever the semantics allow it.
- Write searchable predicates. Keep indexed columns visible in direct comparisons. Be cautious with functions, arithmetic, negation, and leading wildcard patterns.
- Treat ordering as a promise. Tables are not inherently ordered. If sequence matters, write an explicit
ORDER BYclause. - Use
DISTINCTintentionally. It changes the grain of the result and discards multiplicity. Confirm that this is the information the consumer actually wants. - Spend computational guarantees carefully. Select only needed columns, limit large results, and avoid sorting or grouping that serves no real requirement.
The deepest lesson is not that databases prefer one syntax over another. It is that computation becomes efficient when the structure of a request matches the structure of the information being searched.
A good SQL query does not force the database to examine the whole world and then explain why most of it was irrelevant. It gives the database a map, a boundary, and a clear definition of what must be preserved. The fastest path to an answer is usually not a clever shortcut at the end. It is the discipline of making the world smaller at the beginning.
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 🐣