有誰知道一個好的腳本來獲取SQL Server 2005的所有非聚集索引?獲取所有非聚集索引
4
A
回答
13
對於在特定數據庫中所有非聚集索引:
select object_name(object_id) as tablename, *
from sys.indexes where type_desc = 'NONCLUSTERED'
這將返回系統和非系統表。或者:
select t.name as tablename, i.*
from sys.indexes i, sys.tables t
where i.object_id = t.object_id
and i.type_desc = 'NONCLUSTERED'
僅限用戶地非聚集索引。
1
可以使用
exec sp_helpindex 'tablename'
你可以插入表中該得到一個表中的所有指標,並刪除其中INDEX_DESCRIPTION與「CLUSTERED」開始的所有行
這裏是一個腳本對數據庫中的所有指標
declare @EmptyString varchar(1)
select @EmptyString = ''
-- 35 is the lenght of the name field of the master.dbo.spt_values table
declare @IgnoreDuplicateKeys varchar(35),
@Unique varchar(35),
@IgnoreDuplicateRows varchar(35),
@Clustered varchar(35),
@Hypotethical varchar(35),
@Statistics varchar(35),
@PrimaryKey varchar(35),
@UniqueKey varchar(35),
@AutoCreate varchar(35),
@StatsNoRecompute varchar(35)
select @IgnoreDuplicateKeys = name from master.dbo.spt_values
where type = 'I' and number = 1 --ignore duplicate keys
select @Unique = name from master.dbo.spt_values
where type = 'I' and number = 2 --unique
select @IgnoreDuplicateRows = name from master.dbo.spt_values
where type = 'I' and number = 4 --ignore duplicate rows
select @Clustered = name from master.dbo.spt_values
where type = 'I' and number = 16 --clustered
select @Hypotethical = name from master.dbo.spt_values
where type = 'I' and number = 32 --hypotethical
select @Statistics = name from master.dbo.spt_values
where type = 'I' and number = 64 --statistics
select @PrimaryKey = name from master.dbo.spt_values
where type = 'I' and number = 2048 --primary key
select @UniqueKey = name from master.dbo.spt_values
where type = 'I' and number = 4096 --unique key
select @AutoCreate = name from master.dbo.spt_values
where type = 'I' and number = 8388608 --auto create
select @StatsNoRecompute = name from master.dbo.spt_values
where type = 'I' and number = 16777216 --stats no recompute
select o.name,
i.name,
'index description' = convert(varchar(210), --bits 16 off, 1, 2, 16777216 on
case when (i.status & 16)<>0 then @Clustered else 'non'[email protected] end
+ case when (i.status & 1)<>0 then ', '[email protected] else @EmptyString end
+ case when (i.status & 2)<>0 then ', '[email protected] else @EmptyString end
+ case when (i.status & 4)<>0 then ', '[email protected] else @EmptyString end
+ case when (i.status & 64)<>0 then ', '[email protected] else
case when (i.status & 32)<>0 then ', '[email protected] else @EmptyString end end
+ case when (i.status & 2048)<>0 then ', '[email protected] else @EmptyString end
+ case when (i.status & 4096)<>0 then ', '[email protected] else @EmptyString end
+ case when (i.status & 8388608)<>0 then ', '[email protected] else @EmptyString end
+ case when (i.status & 16777216)<>0 then ', '[email protected] else @EmptyString end),
'index column 1' = index_col(o.name,indid, 1),
'index column 2' = index_col(o.name,indid, 2),
'index column 3' = index_col(o.name,indid, 3)
from sysindexes i, sysobjects o
where i.id = o.id and
indid > 0 and indid < 255 --all the clustered (=1), non clusterd (>1 and <251), and text or image (=255)
and o.type = 'U' --user table
--ignore the indexes for the autostat
and (i.status & 64) = 0 --index with duplicates
and (i.status & 8388608) = 0 --auto created index
and (i.status & 16777216)= 0 --stats no recompute
order by o.name
劇本是由朱塞佩Dimauro在http://www.devx.com/vb2themax/Tip/18617
相關問題
- 1. 禁用所有非聚集索引
- 2. 聚集索引和非聚集索引
- 3. SQL非聚集索引
- 4. 加入非聚集索引
- 5. 將聚集索引轉換爲非聚集索引?
- 6. 非聚集索引功能相對於聚集索引尋求
- 7. 複合聚集索引和非聚集索引在sql server 2005
- 8. 同時具有非聚集索引和聚集索引的好處
- 9. 非聚集索引和非聚集索引的區別是什麼
- 10. 通過羣集或非聚集索引
- 11. 獲取位集中所有位集的所有索引
- 12. 聚簇索引和非聚簇索引
- 13. 插入記錄與聚集索引或沒有聚集索引
- 14. 具有非聚簇索引但沒有聚簇索引
- 15. 是否唯一的非聚集索引和非聚集索引之間有什麼不同
- 16. 包含非聚集索引的列
- 17. 非聚集索引減慢插入?
- 18. 創建db2非聚集索引
- 19. 非聚集索引被忽略
- 20. sql server中的非聚集索引
- 21. 過濾非聚集索引異常behaivior
- 22. 兩個SQL非聚集索引創建
- 23. SQL Server的非聚集索引設計
- 24. SQL Server 2008非聚集索引
- 25. 在視圖中創建聚集/非聚集索引
- 26. 非主鍵列上的聚簇索引或非聚簇索引?
- 27. 羣集索引和非聚簇索引 - SQL Server和Oracle?
- 28. 非聚集索引與覆蓋索引之間的區別
- 29. 通過非聚集索引訪問索引
- 30. 缺少索引(影響97):創建非聚集索引
這不值得一個downmod。 – 2009-10-27 20:33:48
原來的答案呢 – super9 2009-10-27 20:37:35