Analyzequery
Home Statistics and Cardinality Estimation The Guessing Game: How Databases Predict the Future
Statistics and Cardinality Estimation

The Guessing Game: How Databases Predict the Future

By Siobhán O'Malley May 14, 2026
The Guessing Game: How Databases Predict the Future
All rights reserved to analyzequery.com

Computers are usually seen as very precise. They follow rules and don't make guesses. But inside a database engine, there is a lot of guessing going on. This happens during query optimization. To build a fast plan, the database needs to know how much data it’s dealing with before it actually starts the work. This is called cardinality estimation. It’s a bit like a wedding planner trying to figure out how much cake to buy. They don't know exactly how many people will show up, but they look at the guest list and make a really good guess. If they guess wrong, they either waste money or run out of cake. Databases do the same thing with rows of data.

To make these guesses, the database keeps a set of statistics. It tracks things like how many unique values are in a column or how the data is spread out. Is everyone in the 'State' column from California, or are they spread across all fifty states? If the statistics are old or wrong, the optimizer might pick a terrible plan. It might think it’s looking for ten rows when it’s actually looking for ten million. When that happens, the system slows to a crawl. That’s why keeping these statistics updated is a big part of the job for people who run these systems. It's about giving the optimizer the best possible information so it can make the smartest guess.

What changed

Old WayNew Way
Simple rules based on table size only.Complex math using data distribution histograms.
Manual tuning by humans.Automatic updates and self-healing stats.
Basic indexes like simple lists.Smart structures like B-trees and bitmap indexes.

The Power of the Index

Imagine trying to find a word in a book without a table of contents or an index at the back. You’d have to read every single page. That is what a database does when it doesn't have an index. It’s called a full table scan. It’s slow and uses a ton of power. An index is a special structure that helps the database jump right to the data it needs. The most common kind is a B-tree. It works like a game of 'higher or lower.' It splits the data into branches, so the computer only has to make a few choices to find the right row. There are also hash indexes for exact matches and bitmap indexes for things like 'True/False' data. The optimizer has to look at these tools and decide which one is right for the job. It’s like a mechanic looking at a toolbox. Do I need a wrench or a screwdriver? The wrong choice might work, but it’ll take much longer.

Why Accuracy is Everything

The whole system relies on a concept called the cost model, which was pioneered by a researcher named Pat Selinger in the 1970s. Her work changed everything. She realized that we shouldn't just look at the steps, but at the cost of those steps. But here is the catch: the cost is only as good as the numbers you put into the math. If the database thinks a certain path will be fast because it only expects a few results, but it gets a million instead, the whole plan falls apart. This is why practitioners spend so much time looking at query graphs. They want to see where the optimizer got confused. It’s a bit like being a detective. You look at the clues, find where the logic went sideways, and then try to fix the stats so it doesn't happen again.

A Constant Balancing Act

In the end, optimizing a query is about balance. You want to spend enough time finding a good plan, but not so much time that the planning takes longer than the actual search. If it takes five seconds to find a plan for a one-second search, you’ve failed. The engine has to be fast at being fast. It uses heuristic algorithms—basically rules of thumb—to cut down the number of options it looks at. It focuses on the most likely winners and ignores the paths that look like dead ends. It’s a high-speed chess match played every time you refresh a page. Have you ever thought about how much math happens just to show you a simple list of your recent orders?

#Cardinality estimation# database statistics# indexing# B-tree# SQL performance
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

The Math Behind the Join: Why Database Order Matters Indexing Strategies and Physical Access Paths All rights reserved to analyzequery.com

The Math Behind the Join: Why Database Order Matters

Mara Vance - May 15, 2026
How Your Database Picks the Fastest Route to Your Data Execution Plan Analysis and Visualization All rights reserved to analyzequery.com

How Your Database Picks the Fastest Route to Your Data

Aris Varma - May 15, 2026
The Invisible Brain Choosing Your Data Paths Join Ordering and Execution Algorithms All rights reserved to analyzequery.com

The Invisible Brain Choosing Your Data Paths

Mara Vance - May 14, 2026
Analyzequery