-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25_Index_Walkthrough_1.sql
More file actions
194 lines (173 loc) · 5.61 KB
/
25_Index_Walkthrough_1.sql
File metadata and controls
194 lines (173 loc) · 5.61 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**************************************************************
* SQL Server 2022: Page Usage and Index Analysis Tutorial
* Description: This script demonstrates how to enable advanced
* options to view detailed page usage and index
* structure using DBCC IND, DBCC SHOWCONTIG, and
* DBCC PAGE commands. It covers:
* - Creating a database and table (Heap and then with
* clustered and non-clustered indexes).
* - Inserting rows to observe page splitting behavior.
* - Analyzing page usage and fragmentation.
* - Examining specific pages using DBCC PAGE.
**************************************************************/
-------------------------------------------------
-- Region: 0. Enabling Advanced Options
-------------------------------------------------
/*
Enable advanced options to view details like page usage.
This allows the usage of DBCC IND and DBCC PAGE commands.
*/
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
GO
-------------------------------------------------
-- Region: 1. Database and Table Setup
-------------------------------------------------
/*
1.1 Create a new database and switch to its context.
*/
CREATE DATABASE IndexDemoDB;
GO
USE IndexDemoDB;
GO
/*
1.2 Create an empty table with a fixed-sized column.
The table is initially a heap (no clustered index).
*/
CREATE TABLE HeapTable (
ID INT NOT NULL PRIMARY KEY NONCLUSTERED, -- Primary key is nonclustered to emphasize the heap structure
FixedColumn CHAR(2000) NOT NULL
);
GO
-------------------------------------------------
-- Region: 2. Initial Page Usage Analysis (Empty Table)
-------------------------------------------------
/*
2.1 Analyze the storage structure of the empty table.
- DBCC IND shows allocation units and their types.
- DBCC SHOWCONTIG displays page usage and fragmentation.
*/
DBCC IND('IndexDemoDB', 'HeapTable', -1);
DBCC SHOWCONTIG('HeapTable');
GO
-------------------------------------------------
-- Region: 3. Inserting Data and Analyzing Page Splits
-------------------------------------------------
/*
3.1 Insert enough rows to fill the first page.
Assuming each row is ~2004 bytes (2000 for FixedColumn + overhead),
approximately 4 rows will fill an 8 KB page.
*/
INSERT INTO HeapTable (ID, FixedColumn)
VALUES
(1, REPLICATE('A', 2000)),
(2, REPLICATE('B', 2000)),
(3, REPLICATE('C', 2000)),
(4, REPLICATE('D', 2000));
GO
/*
3.2 Analyze page usage after inserting 4 rows (first page filled).
*/
DBCC IND('IndexDemoDB', 'HeapTable', -1);
DBCC SHOWCONTIG('HeapTable');
GO
/*
3.3 Insert another row to create a new page.
*/
INSERT INTO HeapTable (ID, FixedColumn)
VALUES (5, REPLICATE('E', 2000));
GO
/*
3.4 Analyze page usage; now there should be two pages.
*/
DBCC IND('IndexDemoDB', 'HeapTable', -1);
DBCC SHOWCONTIG('HeapTable');
GO
-------------------------------------------------
-- Region: 4. Adding a Clustered Index and Analyzing Behavior
-------------------------------------------------
/*
4.1 Create a clustered index on the table.
This will physically reorder the data pages based on the key.
*/
CREATE CLUSTERED INDEX CIX_HeapTable ON HeapTable(ID);
GO
/*
4.2 Analyze the table after adding a clustered index.
*/
DBCC IND('IndexDemoDB', 'HeapTable', -1);
DBCC SHOWCONTIG('HeapTable');
GO
/*
4.3 Insert additional rows to observe page behavior with a clustered index.
*/
INSERT INTO HeapTable (ID, FixedColumn)
VALUES
(6, REPLICATE('F', 2000)),
(7, REPLICATE('G', 2000)),
(8, REPLICATE('H', 2000)),
(9, REPLICATE('I', 2000));
GO
/*
4.4 Check page usage after the additional inserts.
*/
DBCC IND('IndexDemoDB', 'HeapTable', -1);
DBCC SHOWCONTIG('HeapTable');
GO
-------------------------------------------------
-- Region: 5. Adding a Non-Clustered Index and Further Analysis
-------------------------------------------------
/*
5.1 Create a non-clustered index on the FixedColumn.
Non-clustered indexes are stored separately from the data.
*/
CREATE NONCLUSTERED INDEX NCIX_HeapTable ON HeapTable(FixedColumn);
GO
/*
5.2 Analyze the structure after adding the non-clustered index.
*/
DBCC IND('IndexDemoDB', 'HeapTable', -1);
DBCC SHOWCONTIG('HeapTable');
GO
/*
5.3 Insert additional rows and observe changes.
*/
INSERT INTO HeapTable (ID, FixedColumn)
VALUES
(10, REPLICATE('J', 2000)),
(11, REPLICATE('K', 2000));
GO
/*
5.4 Final analysis of page usage and index structure.
*/
DBCC IND('IndexDemoDB', 'HeapTable', -1);
DBCC SHOWCONTIG('HeapTable');
GO
-------------------------------------------------
-- Region: 6. Deep Dive: Using DBCC PAGE for Page Details
-------------------------------------------------
/*
6.1 OPTIONAL: Use DBCC PAGE to examine details of a specific page.
First, enable trace flag 3604 to direct output to the query results.
Replace <PageID> with an actual PageID from DBCC IND output.
*/
DBCC TRACEON(3604); -- Enable output to query results
-- Example: Examine page details (adjust File ID and Page ID as appropriate)
DBCC PAGE('IndexDemoDB', 1, 1, 3); -- (DatabaseID, FileID, PageID, Output Level)
GO
DBCC TRACEOFF(3604); -- Disable trace flag
GO
-------------------------------------------------
-- Region: 7. Cleanup
-------------------------------------------------
/*
Clean up: Optionally, drop the database if no longer needed.
Uncomment the following line to drop the database.
*/
-- DROP DATABASE IndexDemoDB;
GO
-------------------------------------------------
-- End of Script
-------------------------------------------------