question

ramesh 1 avatar image
ramesh 1 asked

sql server 2005/2008 index list

how can list all the indexes in a databases. EXEC sp_helpindex 'tablename' this gives list of index present in a single table, but i am in need of all the indexes , present in a database
indexlist
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Cyborg avatar image
Cyborg answered
You can use sys.indexes or sysindexes

SELECT T.Name, I.name FROM sys.indexes I
INNER JOIN Sys.tables t
ON t.object_id = I.Object_ID
WHERE index_id >= 1 -- skip heep
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image Fatherjack ♦♦ commented ·
+1 but I would have put *** in for a HEAP as they need attention at some point :)
3 Likes 3 ·
Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
+1 for a start - but what about indexes on views?
1 Like 1 ·
Matt Whitfield avatar image
Matt Whitfield answered
This query will give you pretty much everything that sp_helpindex does - but better seeing as * It doesn't use a cursor like sp_helpindex does internally * It shows index keys and included columns as well Edit -> joined to `sys.objects` to ignore `is_ms_shipped` = 1 So... here's the code: SELECT QUOTENAME(OBJECT_SCHEMA_NAME([i].[object_id])) + '.' + QUOTENAME(OBJECT_NAME([i].[object_id])) AS [object_name], ISNULL(QUOTENAME([i].[name]), '**HEAP**') AS [index_name], CASE WHEN [i].[index_id] = 1 THEN 'clustered' ELSE 'nonclustered' END + CASE WHEN [i].[ignore_dup_key] 0 THEN ', ignore duplicate keys' ELSE '' END + CASE WHEN [i].[is_unique] 0 THEN ', unique' ELSE '' END + CASE WHEN [i].[is_hypothetical] 0 THEN ', hypothetical' ELSE '' END + CASE WHEN [i].[is_primary_key] 0 THEN ', primary key' ELSE '' END + CASE WHEN [i].[is_unique_constraint] 0 THEN ', unique key' ELSE '' END + ' located on ' + [ds].[name] AS [index_description], stuff((SELECT ',' + [c].[name] FROM [sys].[index_columns] AS [ic] INNER JOIN [sys].[columns] AS [c] ON [c].[column_id] = [ic].[column_id] AND [c].[object_id] = [ic].[object_id] WHERE [ic].[index_id] = [i].[index_id] AND [ic].[object_id] = [i].[object_id] AND [key_ordinal] > 0 ORDER BY [ic].[index_column_id] FOR XML PATH ('')), 1, 1, '') AS keys, stuff((SELECT ',' + [c].[name] FROM [sys].[index_columns] AS [ic] INNER JOIN [sys].[columns] AS [c] ON [c].[column_id] = [ic].[column_id] AND [c].[object_id] = [ic].[object_id] WHERE [ic].[index_id] = [i].[index_id] AND [ic].[object_id] = [i].[object_id] AND [key_ordinal] = 0 ORDER BY [ic].[index_column_id] FOR XML PATH ('')), 1, 1, '') AS includes FROM [sys].[indexes] AS [i] INNER JOIN [sys].[data_spaces] AS [ds] ON [ds].[data_space_id] = [i].[data_space_id] INNER JOIN [sys].[objects] [o] ON [o].[object_id] = [i].[object_id] WHERE [o].[is_ms_shipped] = 0
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image Fatherjack ♦♦ commented ·
mmmm, gotta love those system indexes !! may be add WHERE OBJECTPROPERTY([i].[object_id],'isusertable')=1 ?? +1 for having asterisks in the HEAPS though. ;)
1 Like 1 ·
Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
@Fatherjack - went for is_ms_shipped so as not to exclude views... edited...
1 Like 1 ·
Cyborg avatar image Cyborg commented ·
:) I missed indexes on views
0 Likes 0 ·
Fatherjack avatar image Fatherjack ♦♦ commented ·
ah, even better. another +1 if I could
0 Likes 0 ·
Fatherjack avatar image
Fatherjack answered
Its not pretty but you could use EXEC [sys].[sp_MSforeachtable] @command1 = N'EXEC sp_helpindex ''?'''
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.