6

In a system there is a nightly data load process where all the foreign keys are dropped, all tables are truncated, repopulated from an external source, and the foreign keys are put back in place.

At the end of this process I would expect sp_updatestats to run, so that statistics for all this new data is updated.

However, while inspecting the code I noticed the following line instead:

EXEC sp_MSforeachtable 'UPDATE STATISTICS ? WITH FULLSCAN'

Can you compare that with running sp_updatestats? That is what is the differences and advantages/disadvantages of one over the other?

5
  • 2
    I'd suggest not using sp_MSforeachtable in general. Commented Jan 21, 2016 at 21:47
  • @JohnM, yep, why? Commented Jan 21, 2016 at 21:48
  • That's what the link is for :) It's an undocumented stored procedure that can't always be trusted. Commented Jan 21, 2016 at 21:49
  • @JohnM, silly me! Admit, you sneaked up this link in in the last second! ;) Commented Jan 21, 2016 at 21:51
  • Just expanded the words the link used to make it more obvious Commented Jan 22, 2016 at 16:18

1 Answer 1

8

Couple of things ..

  1. Stop using the undocumented sp_MSforeachtable
  2. From Erin's blog - Understanding What sp_updatestats Really Updates

    sp_updatestats updates only the statistics that require updating based on the rowmodctr information in the sys.sysindexes catalog view, thus avoiding unnecessary updates of statistics on unchanged rows

Note that sp_updatestats in the code sp_helptext sp_updatestats, it runs 'UPDATE STATISTICS '

enter image description here

Recommendation:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.