How to get Information about indexes of system views
How I can get information about indexes of system views.
I write 开发者_运维知识库this query and look at execution plan.I saw it used Index scan rather Index seek.Any one know why?
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'
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'
Returns
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
Use
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')
To get an index seek.
WHERE OBJECT_NAME(object_id)='Mytbl'
is not a seekable predicate.
Check out this post http://blog.sqlauthority.com/2009/08/24/sql-server-index-seek-vs-index-scan-diffefence-and-usage-a-simple-note/ it doesn't talk about how to get index info from system views but it does explain why reasons why an Index Scan may be used over an Index Seek.
精彩评论