Muhammed Senussi
Muhammed Senussi
  • Muhammed Senussi
Learning to Read a Query PlanJava

An index is a promise about how you intend to ask questions. Adding one without knowing the question is how tables end up with fourteen of them and no faster queries.

Muhammed Senussi

Developers who would never ship a method they had not read will happily ship a query they have never explained. The plan is not exotic; it is a tree of decisions with row estimates attached.

Read the estimates first

Compare estimated rows with actual rows. When they differ by orders of magnitude the planner is working from bad statistics and every choice downstream is suspect. That single comparison explains most surprises.

  • A sequential scan on a large table is not automatically wrong; on a query returning most of the table it is correct.
  • A nested loop over a bad estimate is how a fast query becomes a timeout after a data migration.
  • Sorts that spill to disk are visible in the plan and invisible in the code.

Capture the plan for your important queries in a test. When it changes shape you want to hear about it from CI, not from a customer.

2 Comments

  • Omar Haddad

    September 22, 2025

    Capturing plans in tests is clever. Does it not become noisy as the data grows?

    • Muhammed Senussi

      Muhammed Senussi

      AuthorSeptember 22, 2025

      We assert on the shape, not the numbers: which join strategy, which index, whether a sort appears. Row counts drift constantly; a nested loop turning into a hash join is the thing worth waking up for.

Leave a comment