-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
sp_SQL2000_helpindex.sql
368 lines (313 loc) · 9.2 KB
/
sp_SQL2000_helpindex.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
sp_SQL2000_helpindex.sql
https://bornsql.ca/s/script-duplicate-index-finder/
Copyright (c) BornSQL.ca
Written by Randolph West, released under the MIT License
Last updated: 19 June 2020
Based on sp_helpindex from SQL Server 2000, this script outputs
an index structure in the same format as the 2005+ script by
Kimberly Tripp. This is written specifically for SQL Server 2000.
This script is called by sp_SQL2000_finddupes
See: https://www.sqlskills.com/blogs/kimberly/removing-duplicate-indexes/
-- June 2020: Renamed files, fixed formatting, and improved comments.
-- September 2013: Moved to GitHub.
-- May 2013: Worked around RID / UNIQUIFIER not displaying correctly.
-- August 2012: Updated copyright bits, cleaned up formatting and
-- comments.
-- March 2012: Based on SQL Server 2000 sp_helpindex with revised
-- code for columns in index levels.
*/
USE master
GO
IF OBJECTPROPERTY(OBJECT_ID('sp_SQL2000_helpindex'), 'IsProcedure') = 1
DROP PROCEDURE sp_SQL2000_helpindex
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_SQL2000_helpindex] @objname NVARCHAR(776) -- the table to check for indexes
AS
BEGIN
-- PRELIM
SET NOCOUNT ON
DECLARE @objid INT, -- the object id of the table
@indid SMALLINT, -- the index id of an index
@groupid SMALLINT, -- the filegroup id of an index
@indname sysname,
@groupname sysname,
@status INT,
@keys NVARCHAR(2126), --Length (16*max_identifierLength)+(15*2)+(16*3)
@dbname sysname
-- Check to see that the object names are local to the current database.
SELECT @dbname = PARSENAME(@objname, 3)
IF @dbname IS NOT NULL
AND @dbname <> DB_NAME()
BEGIN
RAISERROR(15250, -1, -1)
RETURN (1)
END
-- Check to see the the table exists and initialize @objid.
SELECT @objid = OBJECT_ID(@objname)
IF @objid IS NULL
BEGIN
SELECT @dbname = DB_NAME()
RAISERROR(15009, -1, -1, @objname, @dbname)
RETURN (1)
END
-- OPEN CURSOR OVER INDEXES (skip stats: bug shiloh_51196)
DECLARE ms_crs_ind CURSOR LOCAL STATIC FOR
SELECT [indid],
[groupid],
QUOTENAME(name, N']'),
[status]
FROM sysindexes
WHERE [id] = @objid
AND [indid] > 0
AND [indid] < 255
AND ([status] & 64) = 0
ORDER BY indid
OPEN ms_crs_ind
FETCH ms_crs_ind
INTO @indid,
@groupid,
@indname,
@status
-- IF NO INDEX, QUIT
IF @@FETCH_STATUS < 0
BEGIN
DEALLOCATE ms_crs_ind
RAISERROR(15472, -1, -1) --'Object does not have any indexes.'
RETURN (0)
END
-- create temp table
CREATE TABLE #spindtab
(
[index_id] INT NOT NULL,
[index_name] sysname COLLATE DATABASE_DEFAULT NOT NULL,
[stats] INT NULL,
[groupname] sysname COLLATE DATABASE_DEFAULT NOT NULL,
[index_keys] NVARCHAR(2126) COLLATE DATABASE_DEFAULT NOT NULL -- see @keys above for length descr
)
-- Now check out each index, figure out its type and keys and
-- save the info in a temporary table that we'll print out at the end.
WHILE @@FETCH_STATUS >= 0
BEGIN
-- First we'll figure out what the keys are.
DECLARE @i INT,
@thiskey NVARCHAR(131) -- 128+3
SELECT @keys = QUOTENAME(INDEX_COL(@objname, @indid, 1), N']'),
@i = 2
IF (INDEXKEY_PROPERTY(@objid, @indid, 1, 'IsDescending') = 1)
SELECT @keys = @keys + N'(-)'
SELECT @thiskey = INDEX_COL(@objname, @indid, @i)
IF (
(@thiskey IS NOT NULL)
AND (INDEXKEY_PROPERTY(@objid, @indid, @i, 'IsDescending') = 1)
)
SELECT @thiskey = @thiskey + N'(-)'
WHILE (@thiskey IS NOT NULL)
BEGIN
SELECT @keys = @keys + N', ' + QUOTENAME(@thiskey, N']'),
@i = @i + 1
SELECT @thiskey = INDEX_COL(@objname, @indid, @i)
IF (
(@thiskey IS NOT NULL)
AND (INDEXKEY_PROPERTY(@objid, @indid, @i, 'IsDescending') = 1)
)
SELECT @thiskey = @thiskey + N'(-)'
END
SELECT @groupname = groupname
FROM sysfilegroups
WHERE groupid = @groupid
-- INSERT ROW FOR INDEX
INSERT INTO #spindtab
VALUES
(@indid, @indname, @status, @groupname, @keys)
-- Next index
FETCH ms_crs_ind
INTO @indid,
@groupid,
@indname,
@status
END
DEALLOCATE ms_crs_ind
-- SET UP SOME CONSTANT VALUES FOR OUTPUT QUERY
DECLARE @empty VARCHAR(1)
SELECT @empty = ''
DECLARE @des1 VARCHAR(35), -- 35 matches spt_values
@des2 VARCHAR(35),
@des4 VARCHAR(35),
@des32 VARCHAR(35),
@des64 VARCHAR(35),
@des2048 VARCHAR(35),
@des4096 VARCHAR(35),
@des8388608 VARCHAR(35),
@des16777216 VARCHAR(35)
SELECT @des1 = [name]
FROM master.dbo.spt_values
WHERE [type] = 'I'
AND [number] = 1
SELECT @des2 = [name]
FROM master.dbo.spt_values
WHERE [type] = 'I'
AND [number] = 2
SELECT @des4 = [name]
FROM master.dbo.spt_values
WHERE [type] = 'I'
AND [number] = 4
SELECT @des32 = [name]
FROM master.dbo.spt_values
WHERE [type] = 'I'
AND [number] = 32
SELECT @des64 = [name]
FROM master.dbo.spt_values
WHERE [type] = 'I'
AND [number] = 64
SELECT @des2048 = [name]
FROM master.dbo.spt_values
WHERE [type] = 'I'
AND [number] = 2048
SELECT @des4096 = [name]
FROM master.dbo.spt_values
WHERE [type] = 'I'
AND [number] = 4096
SELECT @des8388608 = [name]
FROM master.dbo.spt_values
WHERE [type] = 'I'
AND [number] = 8388608
SELECT @des16777216 = [name]
FROM master.dbo.spt_values
WHERE [type] = 'I'
AND [number] = 16777216
-- Simple workaround to establish the tree-level columns included
DECLARE @clustered_index NVARCHAR(4000)
DECLARE @uniquifier BIT
SELECT @clustered_index = ISNULL(index_keys, 'RID'),
@uniquifier = CASE
WHEN index_keys IS NOT NULL
AND (stats & 2) = 0 THEN
1
ELSE
0
END
FROM #spindtab
WHERE index_id = 1
-- DISPLAY THE RESULTS
--(stats & 1) <> 0 = ignore_duplicate_keys
--(stats & 2) <> 0 = unique
--(stats & 4) <> 0 = ignore_duplicate_rows
--(stats & 16) <> 0 = clustered
--(stats & 32) <> 0 = hypothetical
--(stats & 64) <> 0 statistics
--(stats & 2048) <> 0 = primary_key
--(stats & 4096) <> 0 = unique_key
--(stats & 8388608) <> 0 = auto_create
--(stats & 16777216) <> 0 = stats_no_recompute
SELECT index_id,
index_name,
CONVERT( VARCHAR(210), --bits 16 off, 1, 2, 16777216 on, located on group
CASE
WHEN (stats & 16) <> 0 THEN
'clustered'
ELSE
'nonclustered'
END + CASE
WHEN (stats & 1) <> 0 THEN
', ' + @des1
ELSE
@empty
END + CASE
WHEN (stats & 2) <> 0 THEN
', ' + @des2
ELSE
@empty
END + CASE
WHEN (stats & 4) <> 0 THEN
', ' + @des4
ELSE
@empty
END + CASE
WHEN (stats & 64) <> 0 THEN
', ' + @des64
ELSE
CASE
WHEN (stats & 32) <> 0 THEN
', ' + @des32
ELSE
@empty
END
END + CASE
WHEN (stats & 2048) <> 0 THEN
', ' + @des2048
ELSE
@empty
END + CASE
WHEN (stats & 4096) <> 0 THEN
', ' + @des4096
ELSE
@empty
END + CASE
WHEN (stats & 8388608) <> 0 THEN
', ' + @des8388608
ELSE
@empty
END + CASE
WHEN (stats & 16777216) <> 0 THEN
', ' + @des16777216
ELSE
@empty
END + ' located on ' + groupname
) AS [index_description],
index_keys,
CASE
WHEN (stats & 2) = 0 /*non unique*/
AND (stats & 16) = 0 /*non-clustered*/
AND @uniquifier = 0 THEN
index_keys + ', ' + ISNULL(@clustered_index, 'RID')
WHEN (stats & 2) = 0 /*non unique*/
AND (stats & 16) = 0 /*non-clustered*/
AND @uniquifier = 1 THEN
index_keys + ', ' + ISNULL(@clustered_index, 'RID') + ', UNIQUIFIER'
WHEN (stats & 2) = 0 /*non unique*/
AND (stats & 16) <> 0 /*clustered*/
THEN
index_keys + ', UNIQUIFIER'
WHEN @clustered_index IS NOT NULL
AND @uniquifier = 0 THEN
index_keys
ELSE
index_keys + ', RID'
END AS [columns_in_tree],
CASE
WHEN (stats & 2048) <> 0 THEN
'All columns "included" - the leaf level IS the data row.'
WHEN (stats & 4096) <> 0 THEN
index_keys + ', ' + @clustered_index
WHEN (stats & 2) = 0 /*non unique*/
AND (stats & 16) = 0 /*non-clustered*/
AND @uniquifier = 0 THEN
index_keys + ', ' + ISNULL(@clustered_index, 'RID')
WHEN (stats & 2) = 0 /*non unique*/
AND (stats & 16) = 0 /*non-clustered*/
AND @uniquifier = 1 THEN
index_keys + ', ' + ISNULL(@clustered_index, 'RID') + ', UNIQUIFIER'
WHEN (stats & 2) = 0 /*non unique*/
AND (stats & 16) <> 0 /*clustered*/
THEN
'All columns "included" - the leaf level IS the data row.'
WHEN (stats & 2) <> 0 /*unique*/
AND (stats & 16) = 0 /*non-clustered*/
THEN
index_keys
ELSE
index_keys + ', RID'
END AS [columns_in_leaf]
FROM #spindtab
ORDER BY index_id
DROP TABLE #spindtab
RETURN (0) -- sp_helpindex
END
GO
EXEC sp_MS_marksystemobject 'dbo.sp_SQL2000_helpindex'
GO