Fast SQL Is Not About Speed, It Is About Asking the Database Better Questions

Kai Nguyen

Hatched by Kai Nguyen

Jul 04, 2026

10 min read

88%

0

The hidden cost of asking the wrong question

Most slow SQL is not slow because the database is weak. It is slow because the question was asked in the wrong shape.

That is the uncomfortable insight behind writing better queries: performance is rarely just a matter of adding more hardware, more indexes, or more clever syntax. The deeper issue is whether the query tells the engine, as early and as clearly as possible, what it should care about. A database is not a magician. It is a highly disciplined interpreter. The clearer the structure of your request, the less work it must do.

This is why two ideas that often get taught separately are actually the same idea in disguise. One is about query structure: CTEs, partitioning, ranking, aggregate functions, positional functions, window functions. The other is about query execution: SARGability, indexes, filtering early, avoiding unnecessary sorting, avoiding calculations on indexed columns. Taken together, they reveal a powerful principle:

SQL performance comes from aligning the shape of your thought with the shape of the data.

That sounds abstract, but it becomes concrete fast. A query can be logically correct and still be physically expensive. It can say what you mean and still force the engine into needless labor. The art is not only writing the answer, but writing it in a form the database can solve efficiently.


SQL is a language of intent, but the engine reads structure

A human reads a query by meaning. The database reads it by execution order.

That distinction explains a lot of common frustration. People often assume that because they wrote a condition in the WHERE clause, the database will naturally use it first. But the engine does not experience the query the way a reader does. It has to decide how to access rows, when to filter them, whether an index can help, whether sorting can be avoided, and what intermediate sets it must materialize.

This is where execution order becomes more than a technical detail. It is the bridge between intent and cost. If you filter early, you shrink the problem before it grows. If you sort unnecessarily, you force the database to do global work when local work would do. If you wrap an indexed column in a function, you may hide the very information that would let the engine move quickly.

Consider a simple example. Suppose you have a table of orders with an index on order_date, and you want all orders from January. A query that directly expresses a date range gives the optimizer a clean path. But if you write a transformation on the column, such as extracting the month from every row, the database may have to inspect far more data than necessary. The logic is the same, but the physical cost is not.

This is not a minor optimization trick. It is a mental model. Readable to humans is not always readable to machines. SQL asks you to think in two languages at once: semantic clarity and execution clarity.

A good query does not merely state the answer. It stages the answer in a form that can be discovered efficiently.


The real power of CTEs and window functions is not elegance, it is controlled context

CTEs often get introduced as a way to make queries cleaner, and that is true. But their deeper value is not cosmetic. They let you break a large question into smaller logical steps without losing the thread of the analysis.

That matters because many analytical problems are not about one flat filter. They are about stages. First define the population. Then group it. Then compare rows within each group. Then rank, aggregate, or select positions inside that context. This is exactly what window functions do so well: they let you compute across a defined partition while still preserving row-level detail.

Think of a sales table. If you want the top three products in each category, a naive approach might group everything and then try to recover detail later. But a window function lets you say: within each category, rank products by revenue. The PARTITION BY clause creates the local universe. The ranking function performs the calculation inside that universe. The result is both precise and expressive.

That is a profound idea. Partitioning is not just grouping data, it is creating a boundary around a question.

In everyday thinking, we often mix questions together. We ask: what is the total revenue, who is the best performer, which rows come first, how do categories compare, and what is the latest record? SQL forces discipline by making those distinctions explicit. A CTE can isolate the subset of interest. A window function can compute over that subset without collapsing it. Aggregate functions can summarize. Positional functions can locate. Ranking functions can order relative importance.

The benefit is not only readability. It is control.

When you write a query in stages, you are saying to the database: first, establish the relevant world; then, operate inside it. That is a much better instruction than dumping a single tangled expression and hoping the engine can untangle your intent.

Good SQL is not just a list of operations. It is a sequence of decisions about scope.


SARGability is really about keeping doors open

The most useful performance concept in SQL is also one of the easiest to misunderstand: SARGable means a query can use indexes efficiently because the search condition remains usable by the optimizer.

The practical advice follows naturally. Avoid arithmetic on indexed columns in the WHERE clause. Avoid negation when it blocks index use. Avoid leading wildcards that force scans. Use appropriate indexes. Filter early. Limit result size. Avoid unnecessary sorting and grouping.

These rules can feel like a checklist, but the deeper theme is simpler: do not close off possibilities before the engine has had a chance to exploit them.

Imagine you are searching a library. If you ask for all books published after 2020, the librarian can go straight to that shelf. If you ask for books where the publication year plus one is greater than 2021, the librarian has to do extra arithmetic on every candidate. If you ask for titles that end in a certain phrase, the librarian may need to inspect much more of the catalog than if you asked for titles that begin with it. The issue is not whether the query is understandable. The issue is whether the search can remain indexed.

The important connection here is that SARGability is not only about speed, it is about preserving structure. A query with a clean predicate preserves the database's ability to navigate. A query with a wrapped column, hidden transformation, or broad wildcard turns navigation into inspection.

This explains why so many clever-looking queries are slow. Cleverness often comes from compressing logic into fewer lines. Performance often comes from making logic easier to follow. Those are not the same thing.

There is a useful heuristic here:

  1. Can the engine isolate the rows before reading them all?
  2. Can it use existing order or indexing rather than building new order?
  3. Can it postpone expensive work until after the data has been reduced?

If the answer is yes, the query is probably in good shape. If not, the query may be correct but expensive.


The deeper pattern: databases reward locality

Across both advanced SQL design and SQL optimization, one principle keeps reappearing: locality beats global work.

A WHERE clause creates locality by narrowing the set of rows. An index creates locality by letting the engine jump directly to relevant positions. A CTE can create conceptual locality by isolating a step. A PARTITION BY clause creates analytical locality by limiting a calculation to a subset. Even avoiding unnecessary sorting is a way of preserving locality, because sorting usually requires the system to reason about a much broader set than necessary.

This is why the best SQL often feels almost architectural. You are not merely writing a question. You are designing a path through data.

Here is the distinction that changes everything: a query is not just a statement, it is a route. A poorly designed route makes the engine travel through too many places. A well designed route lets it take shortcuts that are safe because the structure of the problem makes them valid.

You can see this in analytical workflows. Suppose a company wants the highest spending customer in each region, but only among customers who bought in the last 30 days. If you first rank all customers and then filter later, you are making the database carry too much. If you filter first, then partition by region, then rank spending within each region, you reduce the universe before applying the expensive logic. The answer is the same. The cost is not.

This has a broader intellectual lesson as well. Many forms of reasoning become better when we stop treating every detail as equally urgent. We create boundaries, categories, and stages because they make complex systems tractable. SQL simply makes that discipline visible.


A practical framework: think in three layers

If you want faster, cleaner SQL, use this three layer model.

1. Reduce the problem first

Before you rank, aggregate, or sort, ask what can be filtered away. Use selective WHERE clauses. Be careful not to wrap indexed columns in functions or expressions that block index use. If you only need a recent slice of data, say so directly.

2. Define the analytical scope

Once the dataset is smaller, decide how rows relate to each other. This is where CTEs and PARTITION BY shine. A CTE can isolate one step in the logic. A window function can compare rows within a category, customer, day, or region without destroying the underlying detail.

3. Compute only what you need

Ask whether the calculation truly needs a global sort, a full grouping, or an extra transformation. If you only need the top few rows, limit the result. If you only need a rank within a partition, do not force the engine to rank the entire universe unnecessarily. If a value can be derived without an expensive function on every row, prefer the simpler route.

This framework is useful because it mirrors how optimizers think. You are not trying to outsmart the database. You are trying to make the optimal path obvious.

The fastest query is often the one that turns a huge question into a small one as early as possible.


Key Takeaways

  • Write for the optimizer, not just for the reader. A query can be logically clear and still physically expensive.
  • Filter early and preserve indexability. Avoid arithmetic, negation, or leading wildcards on indexed columns when they prevent efficient searches.
  • Use CTEs to stage thought. They help break complex logic into smaller steps with clearer scope.
  • Use window functions to compare without collapsing. PARTITION BY creates local context for ranking, aggregation, and positional analysis.
  • Think in terms of locality. The fewer rows the engine must inspect, sort, or transform, the faster the query usually runs.

The query is the architecture

The most valuable way to think about SQL is not as syntax, but as architecture.

A good architect does not merely make a building look elegant. They decide where load is carried, where movement is easy, where access should be direct, and where complexity should be hidden. SQL works the same way. CTEs organize the floors. WHERE clauses place the entrances. Indexes create efficient routes. Window functions define the rooms where comparisons happen. Execution order determines whether the structure is coherent or wasteful.

That is why performance tuning is not just a technical cleanup exercise. It is a form of reasoning. Every time you choose a query shape, you are deciding how much work the database must do to prove what you already know.

The deeper lesson is unexpectedly broad: clarity is an optimization strategy. When your question is shaped well, the answer arrives faster. When your logic respects the structure of the data, the engine can do less to do more. And when you learn to see SQL as a sequence of scoped decisions rather than a pile of clauses, you stop writing queries that merely work and start writing queries that think.

Sources

← Back to Library

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 🐣