Member of the LearnItFirst.com Video Training Network | LearnSqlServer.com | SQL SSIS Training | SQL Programming Tutorials |
LearnSqlServer.com Forums LearnSqlServer.com
Welcome Guest Search | New Posts | Members | Log In | Register

Reset all Statistics to Use the Default Sampling Rate Options
Scott Whigham
Posted: Wednesday, September 20, 2006 5:27:06 PM


Rank: Super Mod

Joined: 3/20/2006
Posts: 345
Points: 748
Location: Dallas, TX
Code:
/*
    Author: Scott Whigham from http://www.LearnSqlServer.com/

    Description: This script resets all stats to use the Default sampling rate/amount

    Misc Notes: I had a client who's previous DBA had two jobs that ran one after the other every night:
        Step 1: DBCC DBREINDEX all tables
        Step 2: UPDATE STATISTICS WITH FULLSCAN, NORECOMPUTE for all tables

        In case you don't know, DBCC DBREINDEX includes rebuilding the stats making Step 2 unnecessary but, because
            it was there, SQL Server ran it anyway. I needed a script to reset all table stats (including columns and indexes)
            to the default sampling rate so that I could then run sp_updatestats on each table.
   
    Versions: SQL Server 2000, 7.0
   
    Creation Date: September 20, 2006

    For more scripts like this one, visit http://forums.learnsqlserver.com/codesamples.aspx
*/
SET NOCOUNT ON

DECLARE @Table TABLE (Id INT NOT NULL IDENTITY(1,1), TableName SYSNAME, SchemaName SYSNAME)
DECLARE @CurrentId INT, @Counter INT, @DynamicSql NVARCHAR(1024), @TableName SYSNAME
    , @StartTime DATETIME, @EndTime DATETIME

SET @StartTime = GETDATE()

INSERT @Table
SELECT TABLE_NAME, TABLE_SCHEMA
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

SELECT TOP 1 @CurrentId = Id, @TableName = '['+SchemaName+'].['+TableName+']', @Counter = Id FROM @Table

WHILE @CurrentId IS NOT NULL
    BEGIN
        SET @DynamicSql = 'UPDATE STATISTICS ' + @TableName -- This resets it to use the default sampling rate/amount
        EXEC(@DynamicSql)
        DELETE @Table WHERE Id = @CurrentId
        SET @CurrentId = NULL
        SELECT TOP 1 @CurrentId = Id, @TableName = '['+SchemaName+'].['+TableName+']', @Counter = Id FROM @Table
    END

SET @EndTime = GETDATE()

SET @DynamicSql = 'Finished running sp_autostats ''ON'' for ' + CAST(@Counter AS VARCHAR) + ' tables in '
    + CAST(DATEDIFF(second, @StartTime, @EndTime) AS VARCHAR) + ' seconds'
PRINT @DynamicSql
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
     
Don't Forget!
LearnItFirst.com
Don't Forget!
LearnExchange.com
 
Home | About Us | Support | Contact Us | Privacy | Site Map | Blogs Blogs Refer a Friend and Get a Free Subscription!
© Copyright 2004-2007 LearnItFirst.com LLC. All rights reserved. All trademarks remain the property of their respective owners.
This site is not affiliated in any way with the Microsoft Corporation.