Site icon IT Tutorial

SQL Server Update Database Statistic

Hi,

Database objects stats should be up to date for making the right decision of SQL Server optimizer.

 

If database objects stats are not up to date then SQL Server database optimizer will make a wrong decision for execution plan of transaction.

You can update all database statistics with following script in a SQL Server Instance.

 

You should execute following query in a spare time of database for example evening or weekends.

 

DECLARE @SQL VARCHAR(1000)  
DECLARE @DB sysname  

DECLARE curDB CURSOR FORWARD_ONLY STATIC FOR  
   SELECT [name]  
   FROM master..sysdatabases 
   WHERE [name] NOT IN ('model', 'tempdb') 
   ORDER BY [name] 
     
OPEN curDB  
FETCH NEXT FROM curDB INTO @DB  
WHILE @@FETCH_STATUS = 0  
   BEGIN  
       SELECT @SQL = 'USE [' + @DB +']' + CHAR(13) + 'EXEC sp_updatestats' + CHAR(13)  
       PRINT @SQL  
       FETCH NEXT FROM curDB INTO @DB  
   END  
    
CLOSE curDB  
DEALLOCATE curDB

 

 

Do you want to learn Microsoft SQL Server DBA Tutorials for Beginners, then read the following articles.

https://ittutorial.org/sql-server-tutorials-microsoft-database-for-beginners/

Exit mobile version