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

时间:2011-03-28 09:55:58

标签: sql-server sql-server-2005 sql-server-2008

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

我写这个查询并查看执行计划。我看到它使用了索引扫描而不是索引查找。任何人都知道为什么?

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

2 个答案:

答案 0 :(得分: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'不是可寻找的谓词。

答案 1 :(得分:0)

查看这篇文章http://blog.sqlauthority.com/2009/08/24/sql-server-index-seek-vs-index-scan-diffefence-and-usage-a-simple-note/它没有讨论如何从系统视图中获取索引信息,但它确实解释了为什么索引扫描可以用于索引搜索的原因。