我寫了一個查詢,尋找未在生產環境,以刪除它們使用的索引,但我很好奇的last_user_update
列是否表明該指數完全屬於一個維護開銷,在這種情況下,最好放棄它,或者索引是否有助於插入/更新/刪除語句的執行,在這種情況下,我不想放棄它。以下是我想要使用的查詢:MSSQL:last_user_update是否意味着索引損害了性能?
DECLARE @DBInfo TABLE
(database_id int, object_id int, tablename nvarchar(200), indexname nvarchar(200), lastactivity datetime)
DECLARE @command VARCHAR(5000)
SELECT @command = 'Use [' + '?' + '] SELECT
database_id, o.object_id, o.name, i.name as indexname, max(lastactivity) as lastactivity
from (
select database_id, object_id, index_id, max(last_user_seek) as lastactivity from sys.dm_db_index_usage_stats
WHERE object_id > 1000
GROUP BY database_id, object_id, index_id
UNION ALL
select database_id, object_id, index_id, max(last_user_scan) as lastactivity from sys.dm_db_index_usage_stats
WHERE object_id > 1000
GROUP BY database_id, object_id, index_id
UNION ALL
select database_id, object_id, index_id, max(last_user_lookup) as lastactivity from sys.dm_db_index_usage_stats
WHERE object_id > 1000
GROUP BY database_id, object_id, index_id
/*UNION ALL
select database_id, object_id, index_id, max(last_user_update) as lastactivity from sys.dm_db_index_usage_stats
WHERE object_id > 1000
GROUP BY database_id, object_id, index_id*/
) a
inner join sys.objects o on a.object_id = o.object_id
inner join sys.indexes i on i.object_id = a.object_id AND i.index_id = a.index_id
where database_id = db_id()
GROUP BY database_id, o.object_id, o.name, i.name
order by lastactivity'
INSERT INTO @DBInfo
(database_id, object_id, tablename, indexname, lastactivity)
EXEC sp_MSForEachDB @command
SELECT db_name(database_id) as dbname, tablename, indexname, lastactivity FROM @DBInfo
where lastactivity < dateadd(day, -15, getdate()) or lastactivity is null
order by db_name(database_id), tablename, indexname
感謝您的評論。我想象user_updates列引發了同樣的問題:它是壞的還是好的? user_updates是否有助於數據修改的執行或傷害它? – 2009-06-01 10:38:49