Sql server 如何获取有关系统视图索引的信息
如何获取有关系统视图索引的信息 我写了这个查询并查看了执行计划。我看到它使用了索引扫描而不是索引查找。有人知道为什么吗Sql server 如何获取有关系统视图索引的信息,sql-server,sql-server-2005,sql-server-2008,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' 选择对象名称(对象ID)表名、st.row\U计数 从sys.dm_db_parti
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'
选择对象名称(对象ID)表名、st.row\U计数
从sys.dm_db_partition_stats st
其中索引id<2,对象名称(对象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'
返回
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
使用
选择对象名称(对象id)表名,
圣罗尤伯爵酒店
从sys.dm_db_partition_stats st
其中索引_id<2
而object_id=object_id('Mytbl'))
获取索引搜索
其中OBJECT\u NAME(OBJECT\u id)='Mytbl'
不是一个可查找的谓词。你为什么在意?系统视图是系统视图。你不需要优化它们!是的。我想把它作为我的个人信息。+1我认为用搜索来代替扫描是完全合理的。我打赌你会得到扫描,因为没有那么多行,所以只扫描它们更有效。@RacerX-你输了。看看我的答案。
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')