2011-03-28 57 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