Skip to content

Instantly share code, notes, and snippets.

@ronascentes
Created November 21, 2016 17:57
Show Gist options
  • Save ronascentes/089b9a8924c0365808ea9607b7fb96f9 to your computer and use it in GitHub Desktop.
Save ronascentes/089b9a8924c0365808ea9607b7fb96f9 to your computer and use it in GitHub Desktop.
Check statistics with less than 25% of sample and may require a full scan
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT DISTINCT 'UPDATE STATISTICS ' + t.name + '.' + OBJECT_NAME(mst.[object_id]) + ' ' + ss.name + ' WITH FULLSCAN'
FROM sys.objects AS o
INNER JOIN sys.tables AS mst ON mst.[object_id] = o.[object_id]
INNER JOIN sys.schemas AS t ON t.[schema_id] = mst.[schema_id]
INNER JOIN sys.stats AS ss ON ss.[object_id] = mst.[object_id]
CROSS APPLY sys.dm_db_stats_properties(ss.[object_id], ss.[stats_id]) AS sp
WHERE sp.[rows] > 0
AND CAST((sp.rows_sampled/(sp.[rows]*1.00))*100.0 AS DECIMAL(5,2)) < 25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment