Table/Column/Index statistics updates

  • Hi,

    Do we need to update the statistics even if we have the settings AUTO_UPDATE_STATISTICS on ? I know we have to do an update when we restore a db but not understanding the need to do manual updates ?

    Thanks

  • statistics auto update when a count of rows equal to 20 percent of a total rows have changed.

    on small tables, that s fine, but on large tables, say a million rows, that 200,000 rows; but "stale" statistics on a bi table can adversely affect your queries if only 1% of the rows are changed.

    as a result, tables that are inserted/updated/deleted from a lot should have their statistics refreshed more often. I've got a huge table that we update statistics on 4 times a day during business hours, and other experienced posters have reporting updating specific tables once an hour or sometimes more often than that!

    there's no hard and fast rule, the proper frequency can vary quite a bit; i

    and it's only tables with changes beneath the threshold you need to worry about.

    here's a query i play with now and then to see what i think should be updated.

    --The Analysis: my crappy assumptions:

    --UPDATE STATISTICS dbo.GMPVDET

    --tables under 1000 rows, I'll leave then at 20%

    --tables with more rows than that, I'll use an arbitrary sliding scale formula.

    --formula to be modified based on analysis

    SELECT X.*,

    ISNULL(CASE

    WHEN X.[Total Rows]<=1000

    THEN

    CASE

    WHEN [Percent Modified] >=20.0

    THEN 'UPDATE STATISTICS ' + [Schema Name] + '.' + [Table Name] + ' WITH ALL, FULLSCAN --20% Small Table Rule'

    END

    WHEN [Percent Modified] = 100.00

    THEN 'UPDATE STATISTICS ' + [Schema Name] + '.' + [Table Name] + ' WITH ALL, FULLSCAN --100% No real Stats Rule'

    --WHEN X.[Rows Modified] > 1000

    --THEN 'UPDATE STATISTICS ' + [Schema Name] + '.' + [Table Name] + ' WITH ALL, FULLSCAN --1000 Rows Modified Rule'

    ELSE

    CASE

    WHEN X.[Total Rows] > 1000000000 --billion rows

    THEN CASE

    WHEN [Percent Modified] > 0.1

    THEN 'UPDATE STATISTICS ' + [Schema Name] + '.' + [Table Name] + ' WITH ALL, FULLSCAN -- 1B Big Table Rule'

    END

    WHEN X.[Total Rows] > 100000000 --hundred million rows

    THEN CASE

    WHEN [Percent Modified] > 1.0

    THEN 'UPDATE STATISTICS ' + [Schema Name] + '.' + [Table Name] + ' WITH ALL, FULLSCAN -- 100M Big Table Rule'

    END

    WHEN X.[Total Rows] > 10000000 --ten million rows

    THEN CASE

    WHEN [Percent Modified] > 2.0

    THEN 'UPDATE STATISTICS ' + [Schema Name] + '.' + [Table Name] + ' WITH ALL, FULLSCAN -- 10M Big Table Rule'

    END

    WHEN X.[Total Rows] > 1000000 --million rows

    THEN CASE

    WHEN [Percent Modified] > 5.0

    THEN 'UPDATE STATISTICS ' + [Schema Name] + '.' + [Table Name] + ' WITH ALL, FULLSCAN -- 1M Big Table Rule'

    END

    WHEN X.[Total Rows] > 100000 --hundred thousand rows

    THEN CASE

    WHEN [Percent Modified] > 10.0

    THEN 'UPDATE STATISTICS ' + [Schema Name] + '.' + [Table Name] + ' WITH ALL, FULLSCAN -- 100K Big Table Rule'

    END

    WHEN X.[Total Rows] > 10000 --ten thousand rows

    THEN CASE

    WHEN [Percent Modified] > 20.0

    THEN 'UPDATE STATISTICS ' + [Schema Name] + '.' + [Table Name] + ' WITH ALL, FULLSCAN -- 10K Big Table Rule'

    END

    END

    END,'') AS [Statistics SQL]

    FROM (

    SELECT DISTINCT

    DB_NAME() AS [Database],

    S.name AS [Schema Name],

    T.name AS [Table Name],

    I.rowmodctr AS [Rows Modified],

    P.rows AS [Total Rows],

    CASE

    WHEN I.rowmodctr > P.rows

    THEN 100

    ELSE CONVERT(decimal(8,2),((I.rowmodctr * 1.0) / P.rows * 1.) * 100.0)

    END AS [Percent Modified]

    FROM

    sys.partitions P

    INNER JOIN sys.tables T ON P.object_Id = T.object_id

    INNER JOIN sys.schemas S ON T.schema_id = S.schema_id

    INNER JOIN sysindexes I ON P.object_id = I.id

    WHERE P.index_id in (0,1)

    AND I.rowmodctr > 0

    ) X

    WHERE [Rows Modified] > 1000

    ORDER BY [Rows Modified] DESC

    --sp_getddl GMCTRACT

    --CREATE CLUSTERED INDEX PK_CENSUSTRACT ON CENSUSTRACTS(CENSUSTRACT,STATEFIPS,COUNTYFIPS)

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thank you 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply