gpt4 book ai didi

sql - 哪些索引在索引使用情况统计表中没有条目?

转载 作者:行者123 更新时间:2023-12-02 21:12:25 25 4
gpt4 key购买 nike

我正在尝试查找哪些索引未使用,以便清除它们并减少 SQL Server 索引维护。

因此,我成功地根据 official documentation 提取了两个查询和其他文章( like this one ):

第一个是根据 sys.dm_db_index_usage_stats 返回未使用的索引:

DECLARE @MinimumPageCount INT = 0

SELECT object_name(IDX.[object_id]) AS [Table]
,IDX.[name] AS [Index]
,PHS.[page_count] AS [Page_Count]
,CONVERT(DECIMAL(18,2), PHS.page_count * 8 / 1024.0) AS [Total Size (MB)]
,CONVERT(DECIMAL(18,2), PHS.avg_fragmentation_in_percent) AS [Frag %]
,PRS.row_count AS [Row Count]
,CONVERT(DECIMAL(18,2), (PHS.page_count * 8.0 * 1024) / PRS.row_count) AS [Index Size/Row (Bytes)]
,US.[user_updates]
,US.[last_user_update]
FROM [sys].[dm_db_index_usage_stats] US
INNER JOIN [sys].[indexes] IDX
ON IDX.[index_id] = US.[index_id]
AND IDX.[object_id] = US.[object_id]
INNER JOIN [sys].[dm_db_index_physical_stats] (DB_ID(),NULL,NULL,NULL,NULL) AS PHS
ON PHS.[index_id] = US.[index_id]
AND PHS.[object_id] = US.[object_id]
INNER JOIN [sys].[dm_db_partition_stats] PRS
ON PRS.[index_id] = US.[index_id]
AND PRS.[object_id] = US.[object_id]
WHERE
-- showing only not used indexes
US.[user_scans] = 0
AND US.[user_seeks] = 0
AND US.[user_lookups] = 0
AND US.[system_scans] = 0
AND US.[system_seeks] = 0
AND US.[system_lookups] = 0
-- ignore indexes with less than a certain number of pages of memory
AND PHS.page_count > @MinimumPageCount
-- exclude clustered and heaps tables
AND US.[index_id] > 1
--current db only
AND US.[database_id]= DB_ID()
ORDER BY [Page_Count] DESC

第二个是提取 sys.dm_db_index_usage_stats 中没有 extries 的索引的类似信息,如下所示:

DECLARE @dbid INT =  DB_ID()

SELECT DB.Name AS [Database]
,OBJ.NAME AS [Table]
,IDX.NAME AS [Index]
,IDX.INDEX_ID
,PHS.page_count AS [Page Count]
,CONVERT(DECIMAL(18,2), PHS.page_count * 8 / 1024.0) AS [Total Index Size (MB)]
,CONVERT(DECIMAL(18,2), PHS.avg_fragmentation_in_percent) AS [Fragmentation (%)]
FROM [sys].[indexes] IDX
INNER JOIN SYS.OBJECTS OBJ
ON IDX.[object_id] = OBJ.[object_id]
LEFT JOIN [sys].[dm_db_index_physical_stats] (@dbid, NULL, NULL, NULL, NULL) PHS
ON PHS.[object_id] = IDX.[object_id]
AND PHS.[index_id] = IDX.[index_id]
INNER JOIN [sys].[databases] DB
ON DB.[database_id] = PHS.[database_id]
WHERE OBJ.[type] = 'U' -- Is User Table
AND IDX.[is_primary_key] = 0
AND IDX.[index_id] NOT IN
(
SELECT US.INDEX_ID
FROM [sys].[dm_db_index_usage_stats] US
WHERE US.[object_id] = IDX.[object_id]
AND IDX.[index_id] = US.[index_id]
AND [database_id] = @dbid
)
AND IDX.[index_id] > 1
ORDER BY PHS.[page_count] DESC
,OBJ.[name]
,IDX.[index_id]
,IDX.[name] ASC

查看上面的查询,所做的唯一过滤是基于 sys.dm_db_index_usage_stats 中是否存在特定索引的条目。

那么,是否有一个规则,如果没有条目则不使用索引?

最佳答案

您给出的第一个查询搜索未使用的索引。 sys.dm_db_index_usage_stats 函数具有字段 [user_scans]、[user_seeks]、[user_lookups]、[system_scans]、[system_seeks]、[system_lookups]。如果它们全部为 0,那么根据您自己的链接,这意味着该索引从未使用过。

第二个查询给出没有可用使用情况统计信息的索引。根据您的链接,sys.dm_db_index_usage_stats 函数的记录在每次重新启动时设置为零,并在数据库分离或关闭时删除。因此,如果自数据库分离或关闭以来从未使用过该索引,则不会有其使用情况统计行。

您已经给出了此链接,但为了完整回答,我再次给出它:http://technet.microsoft.com/en-us/library/ms188755.aspx

关于sql - 哪些索引在索引使用情况统计表中没有条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21598118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com