Why Won't PostgreSQL Use My Covering Index?
Dear Postgres, Why won’t you use my covering index?
Lately I’ve been learning to tune queries running against PostgreSQL, and it’s …
Read MoreBy Kendra Little on • 6 min read
Should you look at automatically created statistics on your tables in SQL Server to help you design better indexes? Learn why in this 20 minute video, or subscribe to the Dear SQL DBA podcast.
No time to watch? Scroll on down, everything is written in article form below the video.
Here’s this week’s question:
Dear SQL DBA,
I’ve noticed that many indexes in my data warehouse aren’t used frequently. Is there a way to use the automatically generated statistics to make useful indexes?
… (insert witty pun about indexes)
I’ve been asked this question several times, and I even remember once wondering this myself.
There’s no good way to analyze the column based statistics that SQL Server automatically generates for the purpose of index design. Let’s talk about why.
Let’s say we have a table named dbo.Grades. It has columns for GradeId, ClassId, StudentId, Year, Term, and Grade.
We run a query looking for all StudentIds where Year=1990.
The table has a Clustered Primary Key on GradeId – so the whole table is stored sorted by GradeId. That doesn’t help our query at all, we want the StudentIds where Year = 1990.
The table has a nonclustered index on Year and GradeId. That nonclustered index does NOT contain StudentId, though.
So SQL Server has two main options:
How does SQL Server know which is better?
SQL Server uses statistics to guess which way is the most efficient to execute your query.
Statistics are little, tiny pieces of metadata that describe the data in the table – things like an approximation of the number of rows equal to 1990. Statistics don’t take up a lot of space in the database.
When you create an index, a statistic is automatically created for the key columns in the index. This can be a multi-column statistic if you have multiple key columns, but the statistic describes the first column in the index in the most detail.
If you use a predicate like Year = 1990 against a column that isn’t the leading column in an index, SQL Server will create a single-column statistic. (Fine print: automatic stats creation can be disabled using a database setting.)
On the other hand, indexes are COPIES of the data itself for the columns defined in the index. The index on Year and GradeId on the dbo.Grades table takes up extra space on disk and on memory and has copies of all the rows for Year and GradeId.
SQL Server uses statistics to optimize an execution plan.
SQL Server uses indexes within the execution plan to find data.
We’ve talked a lot so far about how much statistics and indexes are related. This is why it seems like statistics might be useful for designing indexes!
But here’s the thing – SQL Server doesn’t track and report on how many times a statistic was used during optimization.
I didn’t write the optimizer, but I’m not sad about this at all, I think it’s fine, because:
To have useful information about which single-column statistics might be needed in an index, SQL Server would have to do a lot of work– and it’s already got a feature in optimization for this at a higher level.
Whenever there’s more than one way to run a query, SQL Server thinks about whether an index would help the query run faster. If it thinks an index would help, it records this using the “Missing Indexes” feature.
If a column does NOT have a statistic on it, that isn’t proof that the column is unused.
Statistics are automatically created on columns where you use joins and ‘where’ predicates.
Some columns may just be returned to the user without being filtered. They wouldn’t need a statistic generated for that. But the columns are still in use– and in some cases, using them as “included columns” in indexes might be useful.
So I wouldn’t use the existence OR non-existence of column statistics to make decisions about indexes.
And anyway, we have other options!
There’s two main approaches you can take. After you’ve been doing this a while, you’ll probably mix the approaches.
Check out my article “Updating Statistics in SQL Server: Maintenance Questions and Answers,” or my article and podcast episode “Teach Yourself SQL Server Performance Tuning”.
Copyright (c) 2025, Catalyze SQL, LLC; all rights reserved. Opinions expressed on this site are solely those of Kendra Little of Catalyze SQL, LLC. Content policy: Short excerpts of blog posts (3 sentences) may be republished, but longer excerpts and artwork cannot be shared without explicit permission.