Cardinality Estimation and Row Estimates

How SQL Server estimates rows and how estimate errors affect plan choices.

60 associated posts57 primary posts

Performance Tuning

SQL Server 2022 Finally Fixed a SQL Server 2008 Query Plan Bug!

For yeeeeeears, when I've explained execution plans, part of my explanation has included the instructions, "Read the plan from right to left, top to bottom, looking for the place where the estimates vs actuals are suddenly way off." Here's an example:

Things seem to be going okay on the query plan until you hit the key lookup, which brought back 13 rows of an estimated 19,452. That would appear to be a pretty doggone bad estimate.

Read more about SQL Server 2022 Finally Fixed a SQL Server 2008 Query Plan Bug! 5 comments — Join the discussion
T-SQL & Development

T-SQL Has Regex in SQL Server 2025. Don’t Get Too Excited.

Regular expressions are a way of doing complex string searches. They can be really useful, but they have a reputation: they're hard to write, hard to read, and they're even harder to troubleshoot. Once you master 'em, though, they come in handy for very specific situations.

This post isn't about their complexity, though. This post is about Azure SQL DB & SQL Server 2025's regex performance.

Read more about T-SQL Has Regex in SQL Server 2025. Don’t Get Too Excited. 23 comments — Join the discussion
Performance Tuning

Yes, Cardinality Estimation Keeps Changing After SQL Server 2014.

About 10 years ago, Microsoft made changes to the Cardinality Estimator (CE) which caused some problems for SQL Server upgrades. When folks upgraded to SQL Server 2014, they also casually switched their databases' compatibility level to the latest version, because for years that hadn't really affected query plans. They just figured they wanted the "latest and greatest" compat level, without regard to the effects. That backfired badly when they suddenly got 2014's Cardinality Estimation changes.

Read more about Yes, Cardinality Estimation Keeps Changing After SQL Server 2014. 4 comments — Join the discussion
Performance Tuning

Query Exercise: Why Are These 3 Estimates So Wrong?

Our prior Query Exercise introduced SQL Server's 201 buckets problem: its inability to accurately estimate rows for more than 201 outliers in a table. I followed up with a solution using filtered statistics to help with the next 200 outliers, and I talked about how that's really overkill for the simple problem we were facing in that initial challenge.

Read more about Query Exercise: Why Are These 3 Estimates So Wrong? 6 comments — Join the discussion
Performance Tuning

Query Exercise Answers: Solving the 201 Buckets Problem

In this week's Query Exercise challenge, I explained SQL Server's 201 buckets problem. SQL Server's statistics only handle up to ~201 outliers, which means that outliers ~202-300 get wildly inaccurate estimates.

In our example, I had an index on Location and perfectly accurate statistics, but even still, this query gets bad estimates because Lithuania is in outliers ~202-300:

Read more about Query Exercise Answers: Solving the 201 Buckets Problem 8 comments — Join the discussion
Performance Tuning

Query Exercise: Solving The 201 Buckets Problem

When you run a query, SQL Server needs to estimate the number of matching rows it'll find - so that it can decide which indexes to use, whether to go parallel, how much memory to grant, and more.

For example, take any Stack Overflow database, and let's say I have an index on Location, and I want to find the top-ranking users in Lithuania:

Read more about Query Exercise: Solving The 201 Buckets Problem 14 comments — Join the discussion
Performance Tuning

Improving Cardinality Estimation: Answers & Discussion

Your challenge for last week was to take this Stack Overflow database query to show the top-ranking users in the most popular location:
[crayon-6a6eb881d0d33394853395/]
And make it read less pages only by tuning the query? You weren't allowed to make index or server changes, and you weren't allowed to hard code the location in the query since it might change over time.
The Core of the Problem
The main problem is that when we run a statement (like SELECT), SQL Server:

Read more about Improving Cardinality Estimation: Answers & Discussion 16 comments — Join the discussion
Performance Tuning

Query Exercise: Improving Cardinality Estimation

Your challenge for this week is to tune a query. Say Stack Overflow has a dashboard that shows the top-ranking users in their most popular location. It's even got an index to support it:
[crayon-6a6eb881d1114227621094/]
You can test it with any version of the Stack Overflow database. To test it, we'll turn on a couple of tuning options:
[crayon-6a6eb881d1119694939573/]
The actual execution plan does use our index - not just once, but twice:

Read more about Query Exercise: Improving Cardinality Estimation 33 comments — Join the discussion
T-SQL & Development

Should You Use SQL Server 2022’s STRING_SPLIT?

SQL Server 2022 improved the STRING_SPLIT function so that it can now return lists that are guaranteed to be in order. However, that's the only thing they improved - there's still a critical performance problem with it. Let's take the Stack Overflow database, Users table, put in an index on Location, and then test a…

Read more about Should You Use SQL Server 2022’s STRING_SPLIT? 2 comments — Join the discussion