Statistics are an integral part of SQL Server and query performance. In short, the query optimizer uses statistics to create query plans that will improve the overall performance of the queries ran. Each statistic object is created on a list of one or more table columns and includes a histogram displaying the distribution of values in the first column. The histogram can have up to 200 steps, but no more regardless of the number of rows in the column or index.
In this post we’ll take a look at one specific performance issue that you might find in an execution plan of a query. If you’ve ever noticed the following warning, then this post is for you:
Within the AdventureWorks2014 DB, I’ll use the following query for my example:
SELECT BusinessEntityID, FirstName, LastName, EmailPromotion FROM [AdventureWorks2014].[Person].[Person] WHERE EmailPromotion > 0 ORDER BY LastName
Looking at this query I can already tell contention may be present so I’ll go ahead and add a covering index:
CREATE NONCLUSTERED INDEX [IX_Person_EmailPromotion_INCLUDES] ON [Person].[Person] ([EmailPromotion]) INCLUDE ([BusinessEntityID],[FirstName],[LastName])
When adding the index above, statistics were automatically created and updated. Since the addition of this index I’ve added a few thousand rows to the Person table.
Let’s run the query and make sure the “Include Actual Execution Plan” button is selected.
After the query executes let’s take a look at the execution plan by clicking on the tab in the Results pane:
These warnings were added to SQL Server Management Studio 2012 so if you’re using an older version you may not see this. The spill data to TempDB warning means that the query was not granted enough memory to finish the operation and spilled over into the TempDB to complete the operation. We all know reading from memory is much faster than reading from disk and this is exactly what is happening here. The query read as much as it could from memory before moving over to the TempDB disk.