2011-03-28 61 views
1

如何获取有关系统视图索引的信息。如何获取有关系统视图索引的信息

我写这个查询,看执行plan.I看到它使用索引扫描,而索引seek.Any一知道为什么?

SELECT OBJECT_NAME(OBJECT_ID) TableName, st.row_count 
FROM sys.dm_db_partition_stats st 
WHERE index_id < 2 AND OBJECT_NAME(OBJECT_ID)='Mytbl' 

enter image description here

+0

你为什么在意?系统视图是系统视图。这不像你需要优化它们! – 2011-03-28 09:57:34

+0

是的,我想要我的个人信息。 – Arian 2011-03-28 10:17:25

+1

+1我认为想用扫描替换扫描是完全合理的。 – 2011-03-28 11:35:41

回答

2
select i.name as idx_name, 
     i.type_desc, 
     i.is_unique, 
     ac.name as col_name, 
     c.key_ordinal, 
     c.is_descending_key, 
     c.is_included_column 
from sys.indexes i 
     join sys.all_objects a 
     on a.object_id = i.object_id 
     join sys.index_columns c 
     on c.object_id = i.object_id 
      and c.index_id = i.index_id 
     join sys.all_columns ac 
     on ac.object_id = i.object_id 
      and c.column_id = ac.column_id 
where a.name = 'sysidxstats' 

返回

idx_name   type_desc  is_unique col_name key_ordinal is_descending_key is_included_column 
----------------- --------------- --------- ----------- ----------- ----------------- ------------------ 
clst    CLUSTERED  1   id   1   0     0 
clst    CLUSTERED  1   indid  2   0     0 
nc    NONCLUSTERED 1   name  1   0     0 
nc    NONCLUSTERED 1   id   2   0     0 

使用

SELECT OBJECT_NAME(object_id) TableName, 
     st.row_count 
FROM sys.dm_db_partition_stats st 
WHERE index_id < 2 
     AND object_id = OBJECT_ID('Mytbl') 

为了得到一个索引查找。

PLAN WITH SEEK

WHERE OBJECT_NAME(object_id)='Mytbl'不是可搜索谓语。

+0

+1,构建得非常好的答案 – Lamak 2011-03-28 13:02:51