This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Before a few days ago, When I was performing query optimization at that time I have seen one warning in the result of SQL Server Query Execution Plan like: “Operator used tempdb to spill data during execution with spill level 2”.
I performed RND on this and came to know very interesting reason this warning.
If you don’t know, please note that Database Statistics are playing main to prepare and generate the Query Execution Plan.
SQL Server Statistics contain different types of meta data information like total number records, indexes, access path and others.
This warning occurred because my database Statistics are not updated.
For example, my table has total 50000 records and Statistics contains only 5000 records information so the planner prepares the query execution plan according to 5000 records only.
While executing the query, If it finds more than 5000 records, It transfers other additional records first into TempDB and continue further process using TempDB. Because It calculated processing capacity only for 5000 records.
This process degrades the overall query performance and that’s why you can see a warning like: “Operator used tempdb to spill data during execution with spill level 2”.
The solution is:
Find Statistics data for tables and indexes and If Statistics data are not updated, update your Statistics data, so that Query Optimizer can prepare the best Query Execution Plan.
Check the Statistics for particular index:
If you find difference between Rows and Rows Sampled, Statistics is not updated.
1 |
DBCC SHOW_STATISTICS ("TableName", IndexName); |
Update all Statistics for Database:
1 |
EXEC sp_updatestats; |
Update Statistics for Table:
1 |
UPDATE STATISTICS TABLE_NAME; |
Update Statistics for Index:
1 |
UPDATE STATISTICS TABLE_NAME INDEX_NAME; |