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.
DBCC SHOW_STATISTICS ("TableName", IndexName);
Update all Statistics for Database:
Update Statistics for Table:
UPDATE STATISTICS TABLE_NAME;
Update Statistics for Index:
UPDATE STATISTICS TABLE_NAME INDEX_NAME;
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.