Skip to content

Commit bb68eaa

Browse files
committed
Add table statistic query
1 parent ac72030 commit bb68eaa

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

table_stat.sql

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
WITH table_clustering AS (
2+
SELECT
3+
ix.[object_id]
4+
,ix.index_id
5+
,CASE
6+
WHEN ix.is_primary_key = 1 THEN ix.[type_desc] + N' PK'
7+
WHEN ix.is_unique = 1 THEN ix.[type_desc] + N' UNIQUE'
8+
WHEN ix.is_disabled = 1 THEN N'!!! DISABLED ' + ix.[type_desc]
9+
ELSE ix.[type_desc]
10+
END AS clustering_type
11+
FROM
12+
sys.indexes AS ix
13+
WHERE
14+
ix.[type] IN (0, 1, 5) -- HEAP, CLUSTERED B-tree/Columnstore
15+
),
16+
table_partitions AS (
17+
SELECT
18+
p.[object_id]
19+
,p.index_id
20+
,COUNT(1) AS partitions_count
21+
,SUM(p.[rows]) AS rows_count
22+
,MIN(p.data_compression_desc) AS [compression]
23+
,COUNT(IIF(p.[data_compression] IS NOT NULL, 1, NULL)) AS compressed_partitions
24+
FROM
25+
sys.partitions AS p
26+
GROUP BY
27+
p.[object_id]
28+
,p.index_id
29+
),
30+
table_space AS (
31+
SELECT
32+
s.[object_id]
33+
,CAST(SUM(s.[used_page_count]) * 8. / 1024. / 1024. AS decimal(19, 2)) AS reserved_gb
34+
,CAST(SUM(IIF(ix.[type] IN (0, 1, 5), s.[used_page_count], 0)) * 8. / 1024. / 1024. AS decimal(19, 2)) AS table_gb
35+
,CAST(SUM(IIF(ix.[type] IN (2, 6, 7), s.[used_page_count], 0)) * 8. / 1024. / 1024. AS decimal(19, 2)) AS indexes_gb
36+
FROM
37+
sys.dm_db_partition_stats AS s
38+
INNER JOIN sys.indexes AS ix
39+
ON s.[object_id] = ix.[object_id]
40+
AND s.index_id = ix.index_id
41+
GROUP BY
42+
s.[object_id]
43+
),
44+
nc_indexes_source AS (
45+
SELECT
46+
qryix.[object_id]
47+
,LTRIM(
48+
qryix.index_is_disabled
49+
+ N' ' + qryix.index_has_filter
50+
+ N' ' + qryix.index_type_1
51+
+ N' ' + qryix.index_type_2
52+
+ N': ' + CAST(qryix.index_count AS nvarchar(50))
53+
) AS index_type
54+
FROM
55+
(
56+
SELECT
57+
ix.[object_id]
58+
,ix.[type]
59+
,ix.is_unique
60+
,ix.is_primary_key
61+
,ix.is_unique_constraint
62+
,ix.is_disabled
63+
,COUNT(1) AS index_count
64+
,CASE
65+
WHEN ix.is_disabled = 1 THEN N'FILTERED'
66+
ELSE N''
67+
END AS index_is_disabled
68+
,CASE
69+
WHEN ix.has_filter = 1 THEN N'FILTERED'
70+
ELSE N''
71+
END AS index_has_filter
72+
,CASE
73+
WHEN ix.[type] = 2 THEN N'NC B-TREE'
74+
WHEN ix.[type] = 6 THEN N'NC COLUMNSTORE'
75+
WHEN ix.[type] = 7 THEN N'HASH'
76+
END index_type_1
77+
,CASE
78+
WHEN ix.is_primary_key = 1 THEN N'PK'
79+
WHEN ix.is_unique_constraint = 1 THEN N'UQ CNSTRNT'
80+
WHEN ix.is_unique = 1 THEN N'UQ'
81+
ELSE N''
82+
END index_type_2
83+
FROM
84+
sys.indexes AS ix
85+
WHERE
86+
ix.[type] IN (2, 6, 7) -- NONCLUSTERED B-tree/Columnstore, HASH
87+
GROUP BY
88+
ix.[object_id]
89+
,ix.[type]
90+
,ix.is_unique
91+
,ix.is_primary_key
92+
,ix.is_unique_constraint
93+
,ix.is_disabled
94+
,ix.has_filter
95+
) AS qryix
96+
),
97+
nc_indexes AS (
98+
SELECT
99+
ncix.[object_id]
100+
,N'{' + STUFF(CAST((
101+
SELECT
102+
[text()] = '; ' + index_type
103+
FROM
104+
nc_indexes_source AS qry
105+
WHERE
106+
qry.[object_id] = ncix.[object_id]
107+
FOR xml PATH (''), TYPE
108+
)
109+
AS nvarchar(max))
110+
, 1, 1, N'') + N'}' AS nc_indexes
111+
FROM
112+
(
113+
SELECT DISTINCT
114+
[object_id]
115+
FROM
116+
nc_indexes_source
117+
) AS ncix
118+
)
119+
SELECT
120+
DB_NAME() AS [database_name]
121+
,t.[object_id]
122+
,OBJECT_SCHEMA_NAME(t.[object_id]) + N'.' + t.[name] AS table_name
123+
,t.is_memory_optimized AS is_inmem
124+
,tblcls.clustering_type
125+
,tblprttn.partitions_count
126+
,tblprttn.compressed_partitions
127+
,tblprttn.[compression]
128+
,tblprttn.rows_count
129+
,tblspc.reserved_gb
130+
,tblspc.table_gb
131+
,tblspc.indexes_gb
132+
,ncix.nc_indexes
133+
FROM
134+
sys.tables AS t
135+
INNER JOIN table_clustering AS tblcls
136+
ON t.[object_id] = tblcls.[object_id]
137+
INNER JOIN table_partitions AS tblprttn
138+
ON t.[object_id] = tblprttn.[object_id]
139+
AND tblcls.index_id = tblprttn.index_id
140+
INNER JOIN table_space AS tblspc
141+
ON t.[object_id] = tblspc.[object_id]
142+
LEFT JOIN nc_indexes AS ncix
143+
ON t.[object_id] = ncix.[object_id]
144+
WHERE
145+
t.[type] = N'U'
146+
ORDER BY tblspc.reserved_gb DESC;

0 commit comments

Comments
 (0)