Analyzequery
Home Indexing Strategies and Physical Access Paths Why Your App is Slow: The Math of SQL Plans
Indexing Strategies and Physical Access Paths

Why Your App is Slow: The Math of SQL Plans

By Siobhán O'Malley Jun 7, 2026
Why Your App is Slow: The Math of SQL Plans
All rights reserved to analyzequery.com

We have all been there. You click a button and wait. And wait. Usually, the problem isn't the internet or your phone. It is a database struggling with a complex question. When you write a SQL statement, you are talking to a system that uses relational query optimization mechanics. That is a mouthful, right? Basically, it is just the math the computer uses to stop itself from doing unnecessary work. It tries to be efficient by transforming your query into different algebraic shapes. It is like rearranging an equation in school to make it easier to solve. The goal is always the same: do less, but get the right answer.

The secret sauce is the cost-based model. The database doesn't just guess. It assigns a number to every possible path. This number represents how much effort the computer thinks a path will take. It looks at CPU cycles and disk reads. Then it picks the path with the lowest number. But here is the catch: the computer is only as smart as the information it has. If it thinks a table has ten rows but it actually has ten million, it will pick a terrible plan. This is why we talk so much about statistics. Statistics are the eyes of the optimizer. Without them, it is flying blind.

By the numbers

Efficiency in a database comes down to a few specific metrics. Here is what the optimizer is usually looking at when it builds your execution plan:

  • I/O Operations:How many times the system has to touch the disk.
  • CPU Cycles:The raw processing power needed to sort or compare data.
  • Memory Usage:The amount of RAM required to hold temporary results.
  • Network Latency:The time it takes to move data between different servers.

The logic of pushing things down

One of the coolest tricks a database uses is called predicate pushdown. Imagine you want a list of customers who bought a specific shoe in Chicago. You have a table of millions of sales and a table of customers. You could join them all first and then look for Chicago. But that would be silly. You would be joining millions of rows just to throw most of them away. Instead, the optimizer 'pushes' the Chicago filter down. It finds the Chicago customers first. Then it only looks for sales for those specific people. It sounds obvious to us, but the computer has to prove that this change won't mess up the final answer. This is where view merging also comes in. It breaks down complex layers of your query to see if it can find a more direct path to the data. It is like taking a complicated recipe and realizing you can cook three of the steps in one pan at the same time.

The ghost in the machine

Most of what we do today still follows the work of Patricia Selinger from 1979. Her team at IBM created the first real cost-based optimizer. They realized that you could use probability to predict how many rows would come out of a filter. This is called cardinality estimation. If you filter a table of 100 people by 'eye color equals blue,' the computer might guess that 20% of people have blue eyes. So it expects 20 rows. If it gets 20, the plan stays fast. If it gets 80, the plan might fail. Modern databases have gotten much better at this, but the core idea hasn't changed in over forty years. We are still using those same cascading rules to find the best plan. It is a bit like how we still use wheels on cars—the materials got better, but the shape is still the best one for the job.

When the optimizer gets it wrong

Sometimes, the optimizer gets stuck. This happens a lot with complex queries that have many layers. The math gets too heavy, and the system makes a 'heuristic' choice. That is just a fancy word for a rule of thumb. It says, 'time to calculate this perfectly, so I'll just do what usually works.' Usually, this is fine. But sometimes, it leads to a 'table scan' where the computer reads every single row in a massive file. That is the nightmare scenario for developers. It is like looking for a needle in a haystack by picking up every single piece of straw one by one. To fix it, experts look at query graphs. These are visual maps of how the data flows. They look for spots where the data pile gets too big. They might add a hint to tell the computer which index to use, or they might rewrite the SQL to be clearer. It is part science and part art. Isn't it wild that so much of our digital life depends on these tiny math choices made in milliseconds?

#Cost-based optimization# SQL performance# predicate pushdown# database statistics# execution plan# cardinality estimation
Siobhán O'Malley

Siobhán O'Malley

A Senior Writer who dissects the latent logic of predicate pushdown and the complexities of view merging. She is passionate about helping readers visualize the cascading application of rules within execution plans to optimize intermediate result sets.

View all articles →

Related Articles

A Few Lessons on Flow and Finding the Best Path Execution Plan Analysis and Visualization All rights reserved to analyzequery.com

A Few Lessons on Flow and Finding the Best Path

Siobhán O'Malley - Jun 8, 2026
The Art of the Join: Why Your Database Loves Shortcuts Statistics and Cardinality Estimation All rights reserved to analyzequery.com

The Art of the Join: Why Your Database Loves Shortcuts

Mara Vance - Jun 8, 2026
The Invisible Brain Inside Your Database: How Query Optimization Works Join Ordering and Execution Algorithms All rights reserved to analyzequery.com

The Invisible Brain Inside Your Database: How Query Optimization Works

Siobhán O'Malley - Jun 8, 2026
Analyzequery