Okay, now that I've click-bated the MVP's, knowing full well the answer is "It Depends", I would like to try and get a feel for what everyone uses as a sample size. :-DI'm taking a look at this [b][url=http://www.i-programmer.info/programming/database/5170-improve-sql-performance-an-intelligent-update-statistics-utility.html]post[/url][/b] and it got me thinking about flipping percentages on a dynamic UPATE STATISTICS query I'm building that joins against sys.sysindexes to get the rowcount. The goal is to get a query built like:[font="Courier New"]UPDATE STATISTICS [schema].[object] [statsname] WITH <sample size>[/font]I also added to the script a time component (eventually this will become a proc). So for any stats whose last update of statistics occurred more than X days ago, update them.Is this time component mixed with Auto sample size a good way to approach this? SP_UPDATESTATS does not work because I confirm the stats date is not updated.Thoughts on this approach as well as what sampling rules you tend to follow?[code="sql"]select quotename(SCHEMA_NAME(schema_id)) + '.' + quotename(object_name(S.object_id)) [TableName], quotename(S.NAME) [StatsName], STATS_DATE(S.object_id, S.stats_id) as [LastStatsUpdate], I.rowcnt [NumRows]-- , S.*into #statsdumpfrom sys.stats as S inner join sys.tables as T on T.object_id = S.object_id inner join sys.sysindexes as I on I.id = S.object_idwhere I.indid <= 1declare @ageindays intset @ageindays = 3 -- update anything older than N dayscreate table #work_to_do( rownum int identity(1,1) primary key clustered, updatecommand varchar(max))insert into #work_to_do(updatecommand)select 'UPDATE STATISTICS ' + TableName + ' ' + StatsName + ' WITH SAMPLE ' + CASE when NumRows < 500000 then '100 PERCENT' when NumRows < 1000000 then '50 PERCENT' when NumRows < 5000000 then '25 PERCENT' when NumRows < 10000000 then '10 PERCENT' when NumRows < 50000000 then '2 PERCENT' else '3000000 ROWS' end +' -- Table Rows: ' + cast(NumRows as varchar(max)) [UpdateCommand] from #statsdump where laststatsupdate < getdate() - @ageindays order by numrows descDECLARE @CMD varchar(max)DECLARE UpdateStatsCursor CURSORFOR SELECT UpdateCommand from #work_to_do order by rownum ascOPEN UpdateStatsCursorFETCH NEXT FROM UpdateStatsCursor into @CMDWHILE @@FETCH_STATUS = 0BEGIN print(@cmd) FETCH NEXT FROM UpdateStatsCursor into @CMDENDCLOSE UpdateStatsCursorDEALLOCATE UpdateStatsCursorgodrop table #statsdumpdrop table #work_to_do[/code]
↧