The Query Is Not the Plan: Why Good Engineering Advice Must Explain Its Mechanism

Kai Nguyen

Hatched by Kai Nguyen

Aug 18, 2026

11 min read

94%

0

A query can be logically correct and still be disastrously slow. A programming practice can be widely praised and still be disastrously wrong for a particular system. These look like different problems, but they share a deeper failure: we confuse a readable description of an action with the actual process that produces its result.

SQL makes this confusion unusually visible. You write a query from top to bottom, but the database does not necessarily execute it in that order. It builds a plan, estimates costs, chooses access paths, and transforms your declarative request into a sequence of operations. The words you typed are only the surface expression of a deeper mechanism.

Advice about programming fails in a similar way. “Always use indexes.” “Never use functions in a filter.” “Avoid negation.” “Keep code clean.” Each statement may point toward a useful pattern, but none is a universal law. Its value depends on the goal, the context, and the evidence available.

The common lesson is this: good engineering advice is not a commandment. It is a hypothesis about a mechanism.

Once you see advice this way, query optimization becomes more than a collection of SQL tricks. It becomes a model for thinking clearly about all technical practice.

The Surface Is Not the System

Consider a simple query:

SELECT *
FROM orders
WHERE customer_id = 42;

A developer might look at the statement and reason directly from its appearance: the database sees a table, checks the condition, and returns matching rows. But the database may instead use an index on customer_id, jump directly to relevant entries, and fetch only the associated records. Or it may scan the entire table because the table is tiny, the index is poorly selective, or the statistics suggest that scanning is cheaper.

The query is a request, not a complete explanation of how the request will be fulfilled. This distinction is the foundation of declarative systems. You describe what you want, while the engine determines how to produce it.

Human discussions of programming practice often collapse in the opposite direction. We treat a technique as if its visible form guarantees its result. An index is assumed to mean speed. A function is assumed to mean slowness. A short method is assumed to mean maintainability. A “clean” abstraction is assumed to mean good design.

But mechanisms operate beneath appearances. An index can consume memory, slow writes, and provide little benefit when a query returns most of a table. A function applied to an indexed column can prevent the database from using that index efficiently, but the same function may be harmless when applied to a constant, or when a functional index exists. A compact abstraction can hide complexity rather than remove it.

The practical question is therefore not, “Is this practice good?” It is:

What mechanism is this practice trying to influence, and what evidence would show that it worked?

This question turns slogans into testable claims.

SARGability Is a Case Study in Mechanistic Thinking

One of the most useful SQL concepts is SARGability, short for “Searched Argument Able.” A predicate is SARGable when it can be transformed into an efficient search against an index or another suitable access structure.

Suppose an orders table has an index on created_at. Compare these two filters:

WHERE created_at >= '2026-01-01'

and:

WHERE YEAR(created_at) = 2026

Both express a similar business intention. Yet the second wraps the indexed column in a function. Depending on the database and its indexes, the engine may be unable to use the ordinary index as efficiently because it cannot simply locate a contiguous range of raw created_at values. It may need to evaluate the function for many rows instead.

The first version exposes a range that the index can search directly. The second obscures that range behind a transformation.

This is not because functions are morally bad. The issue is not cleanliness, style, or obedience to a rule. The issue is where computation occurs and whether the engine can use its existing structure before performing that computation.

The same reasoning explains why several common query patterns can be costly:

  • Arithmetic on an indexed column can make direct lookup difficult.
  • Negation can describe a broad or irregular set that is hard to retrieve through an index.
  • A leading wildcard, such as LIKE '%phone', often prevents a conventional index from locating a useful starting point.
  • Unnecessary sorting and grouping can force expensive work after rows have already been selected.
  • Large result sets can dominate the cost even when filtering itself is efficient.

The important insight is not to memorize these examples. It is to recognize their shared structure. The query becomes expensive when it hides the shape of the search from the execution engine.

That gives us a more general design principle: expose useful structure early. Express ranges as ranges. Filter before expanding, sorting, or aggregating. Limit the amount of data carried through later stages. Choose indexes that correspond to the questions the system actually asks.

This principle applies well beyond SQL. In a compiler, explicit types can enable optimization. In a search engine, structured fields can outperform arbitrary text. In an organization, clear ownership can reduce coordination cost. In each case, performance improves when the system can recognize the structure relevant to its job.

Why Universal Advice Fails

If the mechanism matters, then advice must be conditional. Yet technical culture often rewards sentences that sound unconditional. “Never use SELECT *.” “Always normalize.” “Avoid premature optimization.” “Use the simplest technology.” These statements spread because they compress complicated experience into memorable language.

Compression is useful, but it discards information. The danger begins when a reminder is mistaken for a law.

Take the recommendation to limit result sets. Returning only the rows a user needs is usually sensible. It reduces network transfer, memory usage, and downstream processing. But adding a limit without understanding the product requirement can create a different failure. A customer searching for an invoice might receive the first twenty results while the system silently omits the relevant one. The optimization has improved a technical metric by damaging the user’s ability to complete a task.

Or consider adding an index. It may improve a frequently executed read query, but every additional index can increase write cost and storage use. If the query is rare, or the table is small, the index may never repay its maintenance burden. The “right” choice depends on workload, data distribution, latency requirements, and operational constraints.

Even advice to filter early requires interpretation. Relational optimizers often reorder operations automatically. A developer may write a restrictive WHERE clause, yet the database can choose a different execution strategy because of statistics, joins, or estimated costs. Writing filters early can improve clarity and sometimes enable better planning, but the real test is the execution plan and measured behavior, not the visual order of clauses alone.

This is where context enters. A practice suitable for a large analytical warehouse may be inappropriate for a small transactional application. A technique that reduces latency may increase complexity beyond what a small team can safely operate. A design that is excellent under heavy reads may be wrong under heavy writes.

Advice is a map of conditions, not a substitute for noticing the terrain.

The word “should” often hides this missing context. “You should use an index” sounds like a statement about virtue. A better version is conditional: “If this query is frequent, selective, and latency sensitive, and if write overhead is acceptable, an index may be a good experiment.” The second sentence is less elegant, but far more useful.

Optimization Needs a Gradient

There is another reason bad advice survives: teams often lack a reliable gradient, meaning a signal that distinguishes improvement from deterioration.

Imagine a team adopting a new coding standard. Reviews become more uniform, and everyone praises the consistency. But no one tracks defect rates, delivery time, onboarding difficulty, or change failure. There is no external signal showing whether the standard helps. Social agreement fills the vacuum. The practice becomes “correct” because it receives approval, not because it produces better outcomes.

SQL optimization can suffer from the same problem. Someone rewrites a query to avoid a function, adds an index, or removes a sort. The change looks more sophisticated, so it feels like progress. Yet without measurements, the team does not know whether execution time improved, whether resource consumption increased, or whether a different workload became slower.

The execution plan provides a partial gradient. It can reveal whether the database is scanning or seeking, how many rows it expects to process, where sorting occurs, and which operations dominate cost. Runtime measurements provide another gradient: actual latency, variance, CPU usage, memory consumption, and effects on concurrent requests.

Neither signal is perfect. Estimated plans can be wrong because statistics are stale. A query that is fast in isolation can cause contention under load. A small improvement in average latency can be irrelevant if the worst cases remain severe. Good optimization therefore combines multiple forms of evidence.

A useful loop is:

  1. State the goal in operational terms, such as reducing p95 latency for a specific endpoint.
  2. Form a mechanism based hypothesis, such as “the function on the indexed column prevents a range search.”
  3. Inspect the plan and measure representative workloads.
  4. Change one important variable where possible.
  5. Measure again, including costs that may have moved elsewhere.
  6. Keep the change only if it improves the goal without unacceptable tradeoffs.

This loop is valuable because it resists two opposite errors. The first is blind loyalty to convention. The second is random experimentation without a causal theory. A good engineer does not merely ask whether a technique is popular. They ask what it changes in the system and how that change will be detected.

A Three Layer Test for Technical Practice

The ideas above can be organized into a simple framework for evaluating almost any recommendation.

1. Intent: What outcome matters?

Do not begin with the technique. Begin with the purpose. Is the goal lower latency, lower infrastructure cost, easier maintenance, safer deployment, faster development, or better comprehension?

These goals can conflict. An abstraction may slow execution while making a critical business rule easier to change. A denormalized table may improve read performance while making writes and consistency more difficult. A query rewrite may reduce database time while increasing application complexity.

If the goal is undefined, optimization becomes aesthetic preference.

2. Mechanism: What causal path connects the practice to the outcome?

For a SARGable predicate, the path might be: expose a searchable range, enable an index access method, reduce rows examined, lower CPU and I/O, improve latency.

For a code convention, the path might be: reduce ambiguity, improve review quality, lower defect probability, reduce maintenance cost.

If no plausible mechanism can be described, the advice may be cargo cult behavior. If several mechanisms are possible, identify which one matters in this system.

3. Evidence: What observation could disconfirm the belief?

A claim that cannot be falsified is not engineering guidance. If someone says an index will improve performance, ask which query, under what data distribution, by how much, and at what cost. If someone says a pattern makes code cleaner, ask how the team will recognize improved comprehension or reduced change risk.

Evidence does not require perfect experimentation. Even a before and after comparison on realistic data is better than confidence alone. The discipline is to remain open to results that contradict the initial story.

This final layer protects against confirmation bias. Teams often notice evidence that supports the practice they already favor and ignore exceptions. A measured framework makes disagreement productive because people can debate goals, mechanisms, and observations instead of trading slogans.

Key Takeaways

  • Treat every practice as a hypothesis. Replace “always” and “never” with a statement about conditions, mechanism, and expected outcome.
  • Look beneath the syntax. In SQL, inspect the execution plan. In other systems, identify the hidden process that turns an apparent action into a result.
  • Expose structure the system can exploit. Use searchable ranges, appropriate indexes, early reduction of data, and representations that preserve useful information.
  • Define the gradient before optimizing. Choose metrics that reveal whether the change improved the real goal, including latency, cost, reliability, and maintenance burden.
  • Keep the context attached to the advice. A technique that works for one workload, team, or scale may be harmful in another.

The Real Meaning of “Best Practice”

The most mature engineers are not those who know the longest list of rules. They are the ones who can reconstruct why a rule exists, identify the conditions under which it applies, and notice when those conditions have changed.

SQL teaches this lesson with unusual precision. The statement is not the execution. The index is not the benefit. The optimization is not the rewrite. Between each visible choice and its outcome lies a mechanism that must be understood and measured.

The same is true of programming advice. “Simple,” “clean,” “fast,” and “scalable” are not properties that techniques possess in isolation. They are relationships between a practice and a context.

So the next time someone offers a confident prescription, do not ask only whether it sounds familiar. Ask what problem it solves, how it solves it, and what result would prove it wrong.

That habit may produce fewer memorable rules. It produces something better: the ability to generate the right rule for the system in front of you.

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 🐣