我想从我的数据库中找到SP,Tables使用历史记录,因为这些是很长时间没用过的sp。是否有可用于轻松识别数据库中未使用的项目的脚本?
答案 0 :(得分:4)
以下代码将为您提供从上次SQL服务器重新启动时未使用的表和索引。
/*Unused tables and indexes*/
SELECT OBJECTNAME = OBJECT_NAME(I.OBJECT_ID), INDEXNAME = I.NAME, I.INDEX_ID
FROM SYS.INDEXES AS I
INNER JOIN SYS.OBJECTS AS O ON I.OBJECT_ID = O.OBJECT_ID
WHERE OBJECTPROPERTY(O.OBJECT_ID,'IsUserTable') = 1
AND I.INDEX_ID
NOT IN (
SELECT S.INDEX_ID
FROM SYS.DM_DB_INDEX_USAGE_STATS AS S
WHERE S.OBJECT_ID = I.OBJECT_ID
AND I.INDEX_ID = S.INDEX_ID
AND DATABASE_ID = DB_ID(db_name())
)
ORDER BY OBJECTNAME, I.INDEX_ID, INDEXNAME ASC