Does Truncate Table Reset Statistics?

Short answer: the SQL Server optimizer will know that the table was truncated, but statistics might not update when you expect. For the long answer, let’s walk through an example using the WideWorldImporters sample database. I’ll be using Trace Flag 3604 and 2363 to get SQL Server to print information about how it optimized my query out to the messages tab. (Thanks to Paul White for blogging about this trace flag.) First, a fresh restore of WideWorldImporters USE master; GO IF DB_ID('WideWorldImporters') IS NOT NULL ALTER DATABASE WideWorldImporters SET OFFLINE WITH ROLLBACK IMMEDIATE RESTORE DATABASE WideWorldImporters FROM DISK= 'S:\MSSQL\Backup\WideWorldImporters-Full.bak' WITH REPLACE GO USE WideWorldImporters; GO Before we do anything, what do the statistics look like on Sales.OrderLines? Here’s the query that I’m using to inspect the statistics: SELECT sp.last_updated, as stats_name, STUFF((SELECT ', ' + FROM sys.stats_columns AS statcols JOIN sys.columns AS cols ON statcols.column_id=cols.column_id AND statcols.object_id=cols.object_id WHERE statcols.stats_id = stat.stats_id…
Read More

Managing Statistics in SQL Server for DBAs (videos)

Want to learn more about managing statistics updates in SQL Server? Watch my free 27 minute presentation on managing statistics: You can also watch 12 minutes of audience Q&A on statistics from when I presented this on a live Hangout on Air. Questions include: Should I create single column statistics for non-leading key columns in an index? What is asynchronous statistics update? What are these statistics with “WA_sys” names? I have a bunch of user created statistics and it’s blocking a release. It is safe to drop them? Got more questions on statistics? Check out my reference post on Updating Statistics in SQL Server, and ask away in the comments of the post!
Read More

Updating Statistics in SQL Server: Maintenance Questions & Answers

I’ve been asked a lot of questions about updating statistics in SQL Server over the years. And I’ve asked a lot of questions myself! Here’s a rundown of all the practical questions that I tend to get about how to maintain these in SQL Server. I don’t dig into the internals of statistics and optimization in this post. If you’re interested in that, head on over and read the fahhhbulous white paper, Statistics Used by the Query Optimizer in SQL Server 2008. Then wait a couple days and chase it with it’s charming cousin, Optimizing Your Query Plans with the SQL Server 2014 Cardinality Estimator. I’m also not talking about statistics for memory optimized tables in this article. If you’re interested in those, head over here. Quick Links to Jump Around This Article General Advice on Statistics Maintenance Which Free Third Party Tools Help Update Statistics? What are Statistics? What Creates Column Statistics? What are…
Read More