转自: http://www.sqlskills.com/BLOGS/KIMBERLY/post/Updates-%28fixes%29-to-sp_helpindex2.aspx
For SQL Server 2005, here's your new sp_helpindex2 script: sp_helpindex2_2005.zip (2.89 KB)
And, here's a simple test script for 2005:
DROP TABLE tbl1
GO
CREATE TABLE tbl1( c1 int, c2 int, c3 int, c4 int)
GO
CREATE INDEX ix_1 ON tbl1(c1) INCLUDE (c2)
CREATE INDEX ix_2 ON tbl1(c1)
CREATE INDEX ix_3 ON tbl1(c1) INCLUDE (c2, c3)
CREATE INDEX ix_4 ON tbl1(c1, c3) INCLUDE (c2)
CREATE INDEX ix_5 ON tbl1(c3) INCLUDE (c1, c2, c4)
CREATE INDEX ix_6 ON tbl1(c1, c2) INCLUDE (c3, c4)
go
sp_helpindex2 tbl1
go
index_name |
index_description |
index_keys |
included_columns |
ix_1 |
nonclustered located on fg1 |
c1 |
c2 |
ix_2 |
nonclustered located on fg1 |
c1 |
NULL |
ix_3 |
nonclustered located on fg1 |
c1 |
c2, c3 |
ix_4 |
nonclustered located on fg1 |
c1, c3 |
c2 |
ix_5 |
nonclustered located on fg1 |
c3 |
c1, c2, c4 |
ix_6 |
nonclustered located on fg1 |
c1, c2 |
c3, c4 |
For SQL Server 2008, here's your new sp_helpindex2 script: sp_helpindex2_2008.zip (2.84 KB)
And, here's a simple test script for 2008:
DROP TABLE tbl1
GO
CREATE TABLE tbl1( c1 int, c2 int, c3 int, c4 int)
CREATE INDEX ix_1 ON tbl1(c1) INCLUDE (c2)
CREATE INDEX ix_2 ON tbl1(c1)
CREATE INDEX ix_3 ON tbl1(c1) INCLUDE (c2, c3)
CREATE INDEX ix_4 ON tbl1(c1, c3) INCLUDE (c2)
CREATE INDEX ix_5 ON tbl1(c3) INCLUDE (c1, c2, c4)
CREATE INDEX ix_6 ON tbl1(c1, c2) INCLUDE (c3, c4)
CREATE INDEX ix_1f ON tbl1(c1) INCLUDE (c2)
WHERE c3 IS NOT NULL
CREATE INDEX ix_2f ON tbl1(c1)
WHERE c4 > 2
CREATE INDEX ix_3f ON tbl1(c1) INCLUDE (c2, c3)
WHERE c4 > 2 AND c1 < 50 AND c2 = 12
CREATE INDEX ix_4f ON tbl1(c1, c3) INCLUDE (c2)
WHERE c4 IS NOT NULL AND c1 = 12
CREATE INDEX ix_5f ON tbl1(c3) INCLUDE (c1, c2, c4)
WHERE c1 > 5
CREATE INDEX ix_6f ON tbl1(c1, c2) INCLUDE (c3, c4)
WHERE c4 < 20
go
sp_helpindex2 tbl1
go
index_name |
index_description |
index_keys |
included_columns |
filter_definition |
ix_1 |
nonclustered located on PRIMARY |
c1 |
c2 |
NULL |
ix_1f |
nonclustered located on PRIMARY |
c1 |
c2 |
([c3] IS NOT NULL) |
ix_2 |
nonclustered located on PRIMARY |
c1 |
c2 |
NULL |
ix_2f |
nonclustered located on PRIMARY |
c1 |
c2 |
([c4]>(2)) |
ix_3 |
nonclustered located on PRIMARY |
c1 |
c2, c3 |
NULL |
ix_3f |
nonclustered located on PRIMARY |
c1 |
c2, c3 |
([c4]>(2) AND [c1]<(50) AND [c2]=(12)) |
ix_4 |
nonclustered located on PRIMARY |
c1, c3 |
c2 |
NULL |
ix_4f |
nonclustered located on PRIMARY |
c1, c3 |
c2 |
([c4] IS NOT NULL AND [c1]=(12)) |
ix_5 |
nonclustered located on PRIMARY |
c3 |
c1, c2, c4 |
NULL |
ix_5f |
nonclustered located on PRIMARY |
c3 |
c1, c2, c4 |
([c1]>(5)) |
ix_6 |
nonclustered located on PRIMARY |
c1, c2 |
c3, c4 |
NULL |
ix_6f |
nonclustered located on PRIMARY |
c1, c2 |
c3, c4 |
([c4]<(20)) |
If ObjectProperty(Object_ID('dbo.dba_indexLookup_sp'),
N'IsProcedure') Is Null
Begin
Execute ('Create Procedure dbo.dba_indexLookup_sp
As Print ''Hello World!''')
RaisError('Procedure dbo.dba_indexLookup_sp created.', 10, 1);
End;
Go
Set ANSI_Nulls On;
Set Ansi_Padding On;
Set Ansi_Warnings On;
Set ArithAbort On;
Set Concat_Null_Yields_Null On;
Set NoCount On;
Set Numeric_RoundAbort Off;
Set Quoted_Identifier On;
Go
Alter Procedure dbo.dba_indexLookup_sp
/* Declare Parameters */
@tableName varchar(128) = Null
As
/***********************************************************************
Name: dba_indexLookup_sp
Author: Michelle F. Ufford
Purpose: Retrieves index information for the specified table.
Notes: If the tableName is left null, it will return index
information for all tables and indexes.
Called by: DBA
Date User Description
--------------------------------------------------------------------
2008-10-28 MFU Initial Release
************************************************************************
Exec dbo.dba_indexLookup_sp
@tableName = 'myTableName';
***********************************************************************/
Set NoCount On;
Set XACT_Abort On;
Begin
Declare @objectID int;
If @tableName Is Not Null
Set @objectID = Object_ID(@tableName);
With indexCTE(partition_scheme_name
, partition_function_name
, data_space_id)
As (
Select sps.name
, spf.name
, sps.data_space_id
From sys.partition_schemes As sps
Join sys.partition_functions As spf
On sps.function_id = spf.function_id
)
Select st.name As 'table_name'
, IsNull(ix.name, '') As 'index_name'
, ix.object_id
, ix.index_id
, Cast(
Case When ix.index_id = 1
Then 'clustered'
When ix.index_id =0
Then 'heap'
Else 'nonclustered' End
+ Case When ix.ignore_dup_key <> 0
Then ', ignore duplicate keys'
Else '' End
+ Case When ix.is_unique <> 0
Then ', unique'
Else '' End
+ Case When ix.is_primary_key <> 0
Then ', primary key' Else '' End As varchar(210)
) As 'index_description'
, IsNull(Replace( Replace( Replace(
(
Select c.name As 'columnName'
From sys.index_columns As sic
Join sys.columns As c
On c.column_id = sic.column_id
And c.object_id = sic.object_id
Where sic.object_id = ix.object_id
And sic.index_id = ix.index_id
And is_included_column = 0
Order By sic.index_column_id
For XML Raw)
, '"/><row columnName="', ', ')
, '<row columnName="', '')
, '"/>', ''), '')
As 'indexed_columns'
, IsNull(Replace( Replace( Replace(
(
Select c.name As 'columnName'
From sys.index_columns As sic
Join sys.columns As c
On c.column_id = sic.column_id
And c.object_id = sic.object_id
Where sic.object_id = ix.object_id
And sic.index_id = ix.index_id
And is_included_column = 1
Order By sic.index_column_id
For XML Raw)
, '"/><row columnName="', ', ')
, '<row columnName="', '')
, '"/>', ''), '')
As 'included_columns'
, IsNull(cte.partition_scheme_name, '')
As 'partition_scheme_name'
, Count(partition_number) As 'partition_count'
, Sum(rows) As 'row_count'
From sys.indexes As ix
Join sys.partitions As sp
On ix.object_id = sp.object_id
And ix.index_id = sp.index_id
Join sys.tables As st
On ix.object_id = st.object_id
Left Join indexCTE As cte
On ix.data_space_id = cte.data_space_id
Where ix.object_id = IsNull(@objectID, ix.object_id)
Group By st.name
, IsNull(ix.name, '')
, ix.object_id
, ix.index_id
, Cast(
Case When ix.index_id = 1
Then 'clustered'
When ix.index_id =0
Then 'heap'
Else 'nonclustered' End
+ Case When ix.ignore_dup_key <> 0
Then ', ignore duplicate keys'
Else '' End
+ Case When ix.is_unique <> 0
Then ', unique'
Else '' End
+ Case When ix.is_primary_key <> 0
Then ', primary key' Else '' End As varchar(210)
)
, IsNull(cte.partition_scheme_name, '')
, IsNull(cte.partition_function_name, '')
Order By table_name
, index_id;
Set NoCount Off;
Return 0;
End
Go