-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathWhatsUpIndexes.sql
More file actions
87 lines (79 loc) · 2.7 KB
/
WhatsUpIndexes.sql
File metadata and controls
87 lines (79 loc) · 2.7 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
SET ANSI_NULLS ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
SET IMPLICIT_TRANSACTIONS OFF;
SET STATISTICS TIME, IO OFF;
GO
/*
This is a quick one-off script I use in some presentations to look at index sizes.
https://code.erikdarling.com
Copyright (c) 2025 Darling Data, LLC
https://www.erikdarling.com/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
IF OBJECT_ID(N'dbo.WhatsUpIndexes', N'V') IS NULL
BEGIN
DECLARE
@vsql nvarchar(MAX) = N'
CREATE VIEW
dbo.WhatsUpIndexes
AS
SELECT
x = 138;';
PRINT @vsql;
EXECUTE (@vsql);
END;
GO
ALTER VIEW
dbo.WhatsUpIndexes
AS
SELECT TOP (9223372036854775807)
view_name = 'WhatsUpIndexes',
database_name = DB_NAME(),
schema_name = s.name,
table_name = OBJECT_NAME(ps.object_id),
index_name = i.name,
data_compression_desc = p.data_compression_desc,
in_row_pages_mb =
CONVERT
(
decimal(38, 2),
(ps.reserved_page_count * 8. / 1024.)
),
lob_pages_mb =
CONVERT
(
decimal(38, 2),
(ps.lob_reserved_page_count * 8. / 1024.)
),
ps.in_row_used_page_count,
ps.row_count
FROM sys.dm_db_partition_stats AS ps
JOIN sys.objects AS so
ON ps.object_id = so.object_id
AND so.is_ms_shipped = 0
AND so.type <> 'TF'
JOIN sys.schemas AS s
ON s.schema_id = so.schema_id
JOIN sys.indexes AS i
ON ps.object_id = i.object_id
AND ps.index_id = i.index_id
JOIN sys.partitions AS p
ON ps.object_id = p.object_id
AND ps.index_id = p.index_id
AND ps.partition_number = p.partition_number
ORDER BY
ps.object_id,
ps.index_id,
ps.partition_number;
GO