-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateStats.sql
87 lines (67 loc) · 2.42 KB
/
UpdateStats.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
DECLARE @WithFullscan BIT
SELECT @WithFullscan = 0
DECLARE @StartTime DATETIME
SELECT @StartTime = GETDATE()
IF OBJECT_ID('tempdb..#TablesToUpdateStats') IS NOT NULL
BEGIN
DROP TABLE #TablesToUpdateStats
END
DECLARE @NumTables VARCHAR(20)
SELECT s.[Name] AS SchemaName
, t.[name] AS TableName
, SUM(p.rows) AS RowsInTable
INTO #TablesToUpdateStats
FROM sys.schemas s
LEFT JOIN sys.tables t
ON s.schema_id = t.schema_id
LEFT JOIN sys.partitions p
ON t.object_id = p.object_id
LEFT JOIN sys.allocation_units a
ON p.partition_id = a.container_id
WHERE p.index_id IN ( 0, 1 )
AND p.rows IS NOT NULL
AND a.type = 1
GROUP BY s.[Name]
, t.[name]
SELECT @NumTables = @@ROWCOUNT
DECLARE updatestats CURSOR
FOR
SELECT ROW_NUMBER() OVER ( ORDER BY ttus.RowsInTable )
, ttus.SchemaName
, ttus.TableName
, ttus.RowsInTable
FROM #TablesToUpdateStats AS ttus
ORDER BY ttus.RowsInTable
OPEN updatestats
DECLARE @TableNumber VARCHAR(20)
DECLARE @SchemaName NVARCHAR(128)
DECLARE @tableName NVARCHAR(128)
DECLARE @RowsInTable VARCHAR(20)
DECLARE @Statement NVARCHAR(300)
DECLARE @Status NVARCHAR(300)
DECLARE @FullScanSQL VARCHAR(20)
IF @WithFullscan = 1
BEGIN
SELECT @FullScanSQL = ' WITH FULLSCAN'
END
ELSE
BEGIN
SELECT @FullScanSQL = ''
END
FETCH NEXT FROM updatestats INTO @TableNumber, @SchemaName, @tablename,
@RowsInTable
WHILE ( @@FETCH_STATUS = 0 )
BEGIN
SET @Statement = 'UPDATE STATISTICS [' + @SchemaName + '].['
+ @tablename + ']' + @FullScanSQL
SET @Status = 'Table ' + @TableNumber + ' of ' + @NumTables
+ ': Running ''' + @Statement + ''' (' + @RowsInTable
+ ' rows)'
RAISERROR (@Status, 0, 1) WITH NOWAIT
EXEC sp_executesql @Statement
FETCH NEXT FROM updatestats INTO @TableNumber, @SchemaName,
@tablename, @RowsInTable
END
CLOSE updatestats
DEALLOCATE updatestats
DROP TABLE #TablesToUpdateStats