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.