-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckSampleRate.sql
More file actions
69 lines (54 loc) · 1.99 KB
/
CheckSampleRate.sql
File metadata and controls
69 lines (54 loc) · 1.99 KB
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
/* This script returns output from DBCC SHOW_STATISTICS ... WITH STAT_HEADER */
/* for all schemas, tables and indexes in the current database */
/* 2009-02-17, elisabeth@sqlserverland.com, PROVIDED "AS IS" */
DECLARE @i int
DECLARE @sch sysname
DECLARE @table sysname
DECLARE @index sysname
DECLARE @Statement nvarchar(300)
SET @i = 1
/* Table to hold the output from DBCC SHOW_STATISTICS */
CREATE TABLE #dbccStat
(
IdxName sysname
, Updated datetime
, Rows int
, RowsSampled int
, Steps int
, Density int
, AvgKeyLength int
, StringIdx char (3)
)
/* Table to hold information about all indexes for all tables and schemas */
CREATE TABLE #indexes
(
myid int identity
, mySch sysname
, myTbl sysname
, myIdx sysname
)
/* Insert data about all user tables (type = 'u') and their indexes, */
/* in #indexes. Heaps (si.index_id > 0) and system tables (si.object_id >100) */
/* are excluded. */
INSERT INTO #indexes (mySch, myTbl, myIdx)
SELECT schema_name(so.schema_id),object_name(si.object_id), si.name
FROM sys.indexes si INNER JOIN sys.objects so ON si.object_id = so.object_id
WHERE si.object_id >100
AND so.type = 'U'
AND si.index_id > 0
/* Loop through all rows in #indexes */
WHILE
@i < (SELECT max(myid) FROM #indexes)
BEGIN
SELECT @sch = mySch, @table=myTbl, @index = myIdx
FROM #indexes
WHERE myid = @i
SET @statement = N'DBCC SHOW_STATISTICS (['+ @sch + N'.' + @table + N'],' + @index + N')' + N'WITH STAT_HEADER'
INSERT INTO #dbccstat EXEC sp_executesql @statement
SET @i = @i + 1
END
/* Present result */
SELECT * FROM #dbccstat order by 3 desc
/* Clean up */
DROP TABLE #indexes
DROP TABLE #dbccstat