Analyzequery
Home Execution Plan Analysis and Visualization Why Databases Make Guesses (And What Happens When They're Wrong)
Execution Plan Analysis and Visualization

Why Databases Make Guesses (And What Happens When They're Wrong)

By Elias Thorne Jun 2, 2026
Why Databases Make Guesses (And What Happens When They're Wrong)
All rights reserved to analyzequery.com

When you type a search into a website, you expect an answer right away. You don't see the frantic calculations happening in the background. The database is basically playing a game of 'The Price is Right.' It looks at your request and tries to guess how many rows of data it will find. This guess is called cardinality estimation. If the guess is right, everything is fast. If the guess is wrong, the whole system can grind to a halt. It's a high-stakes game where the stakes are your time and the company's money.

Think about a library. If you ask for 'books about dogs,' the librarian knows there are hundreds. If you ask for 'books about three-legged blue dogs from Mars,' the librarian knows there might be zero. The librarian uses their brain to estimate the result size. A database doesn't have a human brain, so it uses statistics. It keeps little charts, called histograms, that tell it how often certain values appear in a column. This helps it decide if it should use a fast shortcut or if it has to settle in for a long, slow scan of every record.

What changed

In the early days, databases were much simpler. They didn't have to worry about petabytes of data or thousands of users hitting the system at once. Over time, the way we optimize these searches has evolved. Here is how things have shifted:

  1. From Rules to Costs:Old systems used simple rules like 'always use an index if it exists.' New systems look at the actual data distribution to see if an index is actually worth the effort.
  2. Better Stats:We now use complex math like HyperLogLog to count unique items without actually looking at every single one. This saves a massive amount of memory.
  3. Adaptive Plans:Some modern databases can change their plan in the middle of a search. If it realizes it guessed wrong, it stops, pivots, and tries a new strategy.
  4. Parallel Processing:Instead of one brain doing all the work, databases now split a single query into dozens of smaller pieces that run at the same time across many CPU cores.

The Math of the Search

One of the most powerful tools in a database's belt is 'predicate pushdown.' Imagine you are looking for a red apple in a bin of a thousand fruits. You could pick up every fruit, check if it's an apple, and then check if it's red. Or, you could just look for red things first. By filtering the data early, you reduce the 'intermediate result set.' That's just a fancy way of saying you have less junk to carry around while you work. The smaller the pile of data, the faster the CPU can chew through it. It's all about minimizing the physical movement of data from the hard drive to the memory.

Is it faster to read a small amount of data from a slow disk, or a large amount of data from a fast disk? The optimizer calculates this every time you hit enter.

The Index Dilemma

We often think that more indexes make things faster. But there is a hidden cost. Every time you add an index, the database has more work to do when you save new information. It has to update the table *and* the index. It's like having a separate filing cabinet for every possible way you might want to sort your files. Eventually, you run out of room, and it takes longer to file a new paper than it does to find an old one. This is why practitioners spend so much time analyzing 'B-trees' and 'Bitmap indexes.' They are trying to find the perfect balance between fast reading and fast writing.

When a database makes a bad plan, it's usually because the statistics were 'stale.' If you just added a million new customers, but the database still thinks you only have ten, it will pick a strategy that is totally inappropriate for the new size. This is why modern database administrators are so obsessed with keeping their stats up to date. They are essentially feeding the optimizer the information it needs to make good choices. Without good data, the smartest math in the world won't save you from a slow query. It's a reminder that even in the world of high-tech algorithms, the quality of your input determines the quality of your output. It's all a big cycle of guessing, measuring, and refining.

#Cardinality estimation# database statistics# SQL performance# predicate pushdown# B-trees# indexing
Elias Thorne

Elias Thorne

As Editor, Elias focuses on the historical evolution of cost-based optimization models and the enduring legacy of Selinger's principles. He meticulously tracks the shift from rule-based heuristics to modern algebraic transformations in database engines.

View all articles →

Related Articles

The Invisible GPS: How Your Database Picks the Best Path Indexing Strategies and Physical Access Paths All rights reserved to analyzequery.com

The Invisible GPS: How Your Database Picks the Best Path

Julian Krell - Jun 2, 2026
Smart Paths and Better Plans: This Week's Network Digest Execution Plan Analysis and Visualization All rights reserved to analyzequery.com

Smart Paths and Better Plans: This Week's Network Digest

Julian Krell - Jun 1, 2026
How Your Database Guesses the Future to Save You Time Cost-Based Optimization Models All rights reserved to analyzequery.com

How Your Database Guesses the Future to Save You Time

Aris Varma - Jun 1, 2026
Analyzequery