[SQL] to find out the index information is not used or the high cost of DB

If you have not used the index, saving space and also affect performance. Therefore, we will pass through sys.dm_db_index_usage_stats to find, since DB Server starts, never used the index information.


In the "missing information DB index to find the" one can find the information DB missing index.

If you have not used that index, it will also affect the performance and save space.

Therefore, we will pass through sys.dm_db_index_usage_stats to find, since DB Server starts, never used the index information.

Through user_updates (the number of index updates because the data table has to do INSERT / UPDATE / DELETE caused) before 20 to find a greater impact on the index information, as follows,

SELECT TOP 20
	SCHEMA_NAME(o.Schema_ID) AS SchemaName
	, OBJECT_NAME(s.[object_id]) AS TableName
	, i.name AS IndexName
	, s.user_updates
	, s.system_seeks + s.system_scans + s.system_lookups
							AS [System usage]
	, s.system_seeks 
	, s.system_scans 
	, s.system_lookups
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id]
	AND s.index_id = i.index_id
INNER JOIN sys.objects o ON i.object_id = o.object_id
WHERE s.database_id = DB_ID()
	AND OBJECTPROPERTY(s.[object_id], 'IsMsShipped') = 0
	AND s.user_seeks = 0
	AND s.user_scans = 0
	AND s.user_lookups = 0
	AND i.name IS NOT NULL
ORDER BY s.user_updates DESC;

image

These are for the current DB query, if you want to find once more the words of DB, you can save all the index information DB through sp_MSforeachdb and temp table, as follows,

SELECT
	DB_NAME() AS DatabaseName
	, SCHEMA_NAME(o.Schema_ID) AS SchemaName
	, OBJECT_NAME(s.[object_id]) AS TableName
	, i.name AS IndexName
	, s.user_updates
	, s.system_seeks + s.system_scans + s.system_lookups
								AS [System usage]
	, s.system_seeks 
	, s.system_scans 
	, s.system_lookups
INTO #TempUnusedIndexes
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id]
AND s.index_id = i.index_id
INNER JOIN sys.objects o ON i.object_id = o.object_id
WHERE 1=2
 
EXEC sp_MSforeachdb 'USE [?];
INSERT INTO #TempUnusedIndexes
SELECT TOP 20
	DB_NAME() AS DatabaseName
	, SCHEMA_NAME(o.Schema_ID) AS SchemaName
	, OBJECT_NAME(s.[object_id]) AS TableName
	, i.name AS IndexName
	, s.user_updates
	, s.system_seeks + s.system_scans + s.system_lookups
							AS [System usage]
	, s.system_seeks 
	, s.system_scans 
	, s.system_lookups
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id]
	AND s.index_id = i.index_id
INNER JOIN sys.objects o ON i.object_id = o.object_id
WHERE s.database_id = DB_ID()
	AND OBJECTPROPERTY(s.[object_id], ''IsMsShipped'') = 0
	AND s.user_seeks = 0
	AND s.user_scans = 0
	AND s.user_lookups = 0
	AND i.name IS NOT NULL
ORDER BY s.user_updates DESC'

SELECT * FROM #TempUnusedIndexes ORDER BY [user_updates] DESC

DROP TABLE #TempUnusedIndexes

image

If you think DB Server since the start, most of the functions of the system are used, and then find out the above index can be deleted or deactivated, in order to save space and reduce improve performance.

The MSDN user_updates described below,
will be noted on the index inserted by the underlying data table or view, update, or delete operation maintenance level caused.
You can use this view to determine the index are applications that do rarely used.
You can also use this view to determine which indexes will generate a maintenance burden.
You might consider unloading produce maintenance burden, but not for the query, or indexes for infrequent queries.

So it can be transmitted through user_updates - user_seeks - user_scans - calculated user_lookups, you can also find out the relatively high cost of the index are those, as follows,

SELECT TOP 20
	 SCHEMA_NAME(o.Schema_ID) AS SchemaName
	, OBJECT_NAME(s.[object_id]) AS TableName
	, i.name AS IndexName
	, (s.user_updates ) AS [update usage]
	, (s.user_seeks + s.user_scans + s.user_lookups)
								AS [Retrieval usage]
	, (s.user_updates - s.user_seeks - user_scans - s.user_lookups) 
			AS [Maintenance cost]
	, s.system_seeks + s.system_scans + s.system_lookups AS [System usage]
	, s.last_user_seek
	, s.last_user_scan
	, s.last_user_lookup
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id]
	AND s.index_id = i.index_id
INNER JOIN sys.objects o ON i.object_id = o.object_id
WHERE s.database_id = DB_ID()
	AND i.name IS NOT NULL
	AND OBJECTPROPERTY(s.[object_id], 'IsMsShipped') = 0
	AND (s.user_seeks + s.user_scans + s.user_lookups) > 0
ORDER BY [Maintenance cost] DESC 

image

Reference Information

SQL Server DMVs in Action

sys.dm_db_index_usage_stats

SQL Server: Applying Filter on sp_MSforeachDB

Original: Large column  [SQL] to find out the index information is not used or the high cost of DB


Guess you like

Origin www.cnblogs.com/petewell/p/11465708.html