Is there any difference between updating the statistics of a table using sp_updatestats
with out resample and updating stats of table using UPDATE STATISTICS
without sample options(FULLSCAN,SAMPLE PERCENT,RESAMPLE)
exec sp_updatestats vs update statistics tablename
updating the tables using sp_updatestats
with default value NO
will update the statistics with default sampling rate.
similarly updating statistics of table using UPDATE STATISTICS
without sample options(FULLSCAN,SAMPLE PERCENT,RESAMPLE)
will also update the table statistics with default sampling.
So is there any difference between both methods? Am I missing anything here ?
Update :
I know that sp_updatestats
runs on all the tables but using UPDATE STATISTICS
we can update statistics of specific table.
Best Answer
Adding to what is already mentioned by @Gameiswar, the other difference which I know with
sp_updatestats
, when you run it for all tables of database it ONLY updates statistics of tables which has at least one row changed. BOL saysSo when you run sp_updatestats and it says statistics for all tables have been updated this is incorrect/misleading only for tables which has at least one row changed was stats updated.
While going with UPDATE STATISTICS you have quite lot of options to use for stats update.
Plus when you rebuild the index with full scan or with default options the stats are updated for that index so no need to rebuild stats again for it.
PS: Whatever the difference may be, I would not use sp_updatestats as the mechanism it uses to update stats is not good, why should i update stats for table if just one row has changed and I have 100K rows in table, it will just consume resources and cause more issue.
EDIT:
If you want to update stats selectively and do not want to run sp_updatestas use below query to filter out outdated stats. This query uses DMF and would work from
SQL Server 2008 R2 SP2 , SQL Server 2012 Sp1 and above
, Copied from Erin Stellato's blogNOTE: Realize that different tables may have different thresholds and you will need to tweak the query above for your databases. For some tables, waiting until 15% or 20% of the rows have been modified may be ok. But for others, you may need to update at 10% or even 5%, depending on the actual values and their skew.
If you want to avoid all such hassle you can also use Ola Hallengren update stats script which takes care of lot of such things.