This script permit executes the sp_updatestats in all databases at same time.
First I create the stored procedure above 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
First I create the stored procedure above 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
举个例子吧:
分析你的查询如何优化,索引是否正确需要正确的统计数据,数据一般要建表或加列时不会默认自动更新统计列,也就是说如果你的表的数据有1万行,后来变成1百万行的时候,如果没有及时的更新统计那么优化器就可能生正不正确的优化结果,造成不正确的索引建议。
所以,你可以建立维护计划定期更新统计,用于之后优化器能生成正确的优化建议。
这是我个人的理解
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
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
这里涉及到一个存储过程 sp_updatestats,对当前数据库中所有用户定义的表运行 UPDATE STATISTICS。这个可以从联机手册上看到。但是 UPDATE STATISTICS 有什么用处可能很多人都不太清楚,下面是来之csdn的回答:
分析你的查询如何优化,索引是否正确需要正确的统计数据,数据一般要建表或加列时不会默认自动更新统计列,也就是说如果你的表的数据有1万行,后来变成1百万行的时候,如果没有及时的更新统计那么优化器就可能生正不正确的优化结果,造成不正确的索引建议。
所以,你可以建立维护计划定期更新统计,用于之后优化器能生成正确的优化建议。
这是我个人的理解。
本文转自 Fanr_Zh 博客园博客,原文链接:http://www.cnblogs.com/Amaranthus/archive/2011/03/30/1999460.html,如需转载请自行联系原作者