Live registration reopens October 1, 2026, in 2d 04h 49m — Notify me

Category: Statistics

How Multi-Column Statistics Work

The short answer: in the real world, only the first column works. When SQL Server needs data about the second column, it builds its own stats on that column instead (assuming they don't already exist), and uses those two statistics together - but they're not really correlated.

For the longer answer, let's take a large version of the Stack Overflow database, create a two-column index on the Users table, and then view the resulting statistics:

Read more about How Multi-Column Statistics Work 4 comments — Join the discussion

Automatic Stats Updates Don’t Always Invalidate Cached Plans

Normally, when SQL Server updates statistics on an object, it invalidates the cached plans that rely on that statistic as well. That's why you'll see recompiles happen after stats updates: SQL Server knows the stats have changed, so it's a good time to build new execution plans based on the changes in the data.

However, updates to system-created stats don't necessarily cause plan recompiles.

Read more about Automatic Stats Updates Don’t Always Invalidate Cached Plans 9 comments — Join the discussion

How Bad Statistics Cause Bad SQL Server Query Performance

SQL Server uses statistics to guess how many rows will match what your query is looking for. When it guesses too low, your queries will perform poorly because they won't get enough memory or CPU resources. When it guesses too high, SQL Server will allocate too much memory and your Page Life Expectancy (PLE) will nosedive.

Read more about How Bad Statistics Cause Bad SQL Server Query Performance 3 comments — Join the discussion

The 201 Buckets Problem, Part 2: How Bad Estimates Backfire As Your Data Grows

In the last post, I talked about how we don't get accurate estimates because SQL Server's statistics only have up to 201 buckets in the histogram. It didn't matter much in that post, though, because we were using the small StackOverflow2010 database. But what happens as our data grows? Let's move to a newer Stack…

Read more about The 201 Buckets Problem, Part 2: How Bad Estimates Backfire As Your Data Grows 14 comments — Join the discussion

The 201 Buckets Problem, Part 1: Why You Still Don’t Get Accurate Estimates

I'll start with the smallest Stack Overflow 2010 database and set up an index on Location: [crayon-6ababbc2777e2439445207/] There are about 300,000 Users - not a lot, but enough that it will start to give SQL Server some estimation problems: When you create an index, SQL Server automatically creates a statistic with the same name. A…

Read more about The 201 Buckets Problem, Part 1: Why You Still Don’t Get Accurate Estimates 7 comments — Join the discussion

How to Think Like the SQL Server Engine: When Statistics Don’t Help

In our last episode, we saw how SQL Server estimates row count using statistics. Let's write two slightly different versions of our query - this time, only looking for a single day's worth of users - and see how its estimations go:
[crayon-6ababbc279241744169346/]
Both of those queries are theoretically identical in that they accomplish the same result by producing exactly the same rows - but their execution plans are different. On this one, you'll probably want to click to zoom in, and play spot-the-differences:

Read more about How to Think Like the SQL Server Engine: When Statistics Don’t Help 16 comments — Join the discussion

How to Think Like the SQL Server Engine: Using Statistics to Build Query Plans

In our last episode, SQL Server was picking between index seeks and table scans, dancing along the tipping point to figure out which one would be more efficient for a query.

One of my favorite things about SQL Server is the sheer number of things it has to consider when building a query plan. It has to think about:

Read more about How to Think Like the SQL Server Engine: Using Statistics to Build Query Plans 8 comments — Join the discussion
Performance Tuning

Filtered Statistics Follow-up

During our pre-con in Seattle
A really sharp lady brought up using filtered statistics, and for a good reason. She has some big tables, and with just 200 histogram steps, you can miss out on a lot of information about data distribution when you have millions or billions of rows in a table. There's simply not enough room to describe it all accurately, even with a full scan of the stats.

Read more about Filtered Statistics Follow-up 5 comments — Join the discussion

Hidden in SQL Server 2017 CTP v1.1: sys.dm_db_stats_histogram

It's Friday night, so I'm waiting for new CTP Releases
As soon as I got the email, I started reading the release notes. Some interesting stuff, of course.

Batch mode queries now support “memory grant feedback loops,” which learn from memory used during query execution and adjusts on subsequent query executions; this can allow more queries to run on systems that are otherwise blocking on memory.

Read more about Hidden in SQL Server 2017 CTP v1.1: sys.dm_db_stats_histogram 5 comments — Join the discussion