Update statistics of all databases

Source: Internet
Author: User

This script permit executes the sp_updatestats in all databases at same time.

First I create the Stored Procedure abve in an admin database, for sample I create one database with Admin name where I put all administration objects create for me. you can create this SP in exist dB, if you want.

Now you can execute this SP in query analyzer or create a job to automate this operation. to execute this, call the next commands:

Use admin -- or the name of database where you create this SP
Exec spupdatestats

Good work

For example:
Analyze how your query is optimized and whether the index is correct requires correct statistics. When you create a table or add a column, the statistical column is not automatically updated by default, that is to say, if your table has 10 thousand rows of data and then becomes 1 million rows, if the statistics are not updated in time, the Optimizer may produce incorrect optimization results, incorrect index suggestion.
Therefore, you can set up a maintenance plan to regularly update statistics so that the optimizer can generate correct optimization suggestions.
This is my personal understanding.

CREATE PROCEDURE SPUpdateStats
AS

Set Nocount on
Declare db Cursor For
Select name from master.dbo.sysdatabases where name not in ('master','TempDB', 'msdb', 'model')

Declare @dbname varchar(60)
Declare @execmd nvarchar(150)

Open db
Fetch Next from db into @dbname
While @@Fetch_status=0
begin
if @dbname is null
Begin
Print 'null Value'
end
else
Begin
PRINT '###########################################################################'
PRINT 'Update Statistics in ' + @dbname
SELECT @execmd = 'USE ' + @dbname + ' EXEC sp_updatestats'
EXEC(@execmd)
PRINT ''
End
Fetch Next from db into @dbname
end
Close db
Deallocate db
GO

This involves a stored procedure sp_updatestats, which runs update statistics on all user-defined tables in the current database. This can be seen from the online manual. However, many people may not be quite clear about the usefulness of update statistics. Here is the answer from csdn:

Analyze how your query is optimized and whether the index is correct requires correct statistics. When you create a table or add a column, the statistical column is not automatically updated by default, that is to say, if your table has 10 thousand rows of data and then becomes 1 million rows, if the statistics are not updated in time, the Optimizer may produce incorrect optimization results, incorrect index suggestion.
Therefore, you can set up a maintenance plan to regularly update statistics so that the optimizer can generate correct optimization suggestions.
This is my personal understanding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.