Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does exec sp_updatestats do?

What is the use of sp_updatestats? Can I run that in the production environment for performance improvement?

like image 331
Vignesh Avatar asked May 03 '14 05:05

Vignesh


People also ask

How long does Exec Sp_updatestats take?

sp_updatestats can run from 2 minutes to 4 hours. Each night the ETL deletes all rows and recopies all rows, but overall, there is almost no difference in the size of the data.

How often should I run Sp_updatestats?

If you mean UPDATE STATISTICS WITH FULLSCAN, weekly or monthly is probably enough for most situations. However, I often setup a job to run nightly to run EXECUTE SP_UPDATESTATS in active user databases, because it only updates the stats that are needed. It takes much less time and resources to run.

Does Sp_updatestats do Fullscan?

Answers. Some people suggested sp_updatestats, but sp_updatetats has no guaranteed way to result in a FULLSCAN for all tables.

What is the purpose of update statistics?

Updating statistics ensures that queries compile with up-to-date statistics. However, updating statistics causes queries to recompile. We recommend not updating statistics too frequently because there is a performance tradeoff between improving query plans and the time it takes to recompile queries.


1 Answers

sp_updatestats updates all statistics for all tables in the database, where even a single row has changed. It does it using the default sample, meaning it doesn't scan all rows in the table so it will likely produce less accurate statistics than the alternatives.

If you have a maintenance plan with 'rebuild indexes' included, it will also refresh statistics, but more accurate because it scans all rows. No need to rebuild stats after rebuilding indexes.

Manually updating particular statistics object or a table with update statistics command gives you much better control over the process. For automating it, take a look here.

Auto-update fires only when optimizer decides it has to. There was a change in math for 2012: in <2012, auto update was fired for every 500 + 20% change in table rows; in 2012+ it is SQRT(1000 * Table rows). It means it is more frequent on large tables. Temporary tables behave differently, of course.

To conclude, sp_updatestats could actually do more damage than good, and is the least recommendable option.

like image 145
dean Avatar answered Sep 30 '22 18:09

dean