SQL Server Index Tuning - useless index

We know that a reasonable index can significantly improve performance, but also reduce the redundancy of the index database performance. With the development of our business, in the table, the table structure of the database, query the contents are likely to change. In this way, some index may no longer use, need to be deleted (because maintaining an index that is a waste of storage, and cost performance); while others will need to modify or add table index. This article gives the main way to find one to quickly determine the index no longer in use, dynamic view (DMV) query.

Useless index
First we look at how to query useless index. sys.dm_db_index_usage_stats use to record since the last restart or reset the database offline or index statistics, sys.indexes record data in all tables of index, excluding indexes fell recently used, that is not recently used index, specific script as follows:

- querying the database index is not used to
the USE WideWorldImporters;
the GO
the DECLARE @dbid the DB_ID the INT = ( 'WideWorldImporters');
the WITH CTE the AS (
the SELECT
[object_id], the index_id
the FROM the sys.indexes
the EXCEPT
the SELECT
[object_id], the index_id
the FROM SYS. dm_db_index_usage_stats
the WHERE database_id The @ = the dbid)
the SELECT
o.name tableName, i.name indexName
the FROM I the sys.indexes
the INNER the JOIN cte.index_id the ON = i.index_id the AND CTE CTE. [object_id] = I. [object_id]
the INNER the sys.objects the JOIN . O the ON I [object_id] = O [object_id].
the WHERE O [type] the iN ( 'the U-', 'V') the aND I [type]> 0;..
since we only examine table or indexed view created by the user, Finally, we only selected sys.objects the type is "U" (user-created table) and "V" (view an index created by the user). sys.indexes type = 0 in a heap, it is also excluded. Here are generated script to remove the index:

DECLARE @dbid INT=DB_ID('WideWorldImporters');
WITH cte AS(
SELECT
[object_id],index_id
FROM sys.indexes
EXCEPT
SELECT
[object_id],index_id
FROM sys.dm_db_index_usage_stats
WHERE database_id=@dbid)
SELECT
--修改主键索引删除报错的问题
CASE WHEN is_primary_key=1 THEN 'ALTER TABLE '+ o.name +' DROP CONSTRAINT '+i.name
ELSE 'DROP INDEX '+i.name+' ON '+ o.name
END
FROM sys.indexes i
INNER JOIN cte ON cte.index_id=i.index_id AND cte.[object_id]=i.[object_id]
INNER JOIN sys.objects o ON i.[object_id]=o.[object_id]
WHERE o.[type] IN ('U','V') AND i.[type]>0;
The above script corresponding to each index a table of a delete statement, of course, also be used to generate a statement following the script.

DECLARE @dbid INT=DB_ID('WideWorldImporters');
DECLARE @sql VARCHAR(MAX);
WITH cte AS(
SELECT
[object_id],index_id
FROM sys.indexes
EXCEPT
SELECT
[object_id],index_id
FROM sys.dm_db_index_usage_stats
WHERE database_id=@dbid)
SELECT @sql=(
SELECT
--修改主键索引删除报错的问题
CASE WHEN is_primary_key=1 THEN 'ALTER TABLE '+ o.name +' DROP CONSTRAINT '+i.name
ELSE 'DROP INDEX '+i.name+' ON '+ o.name
END
FROM sys.indexes i
INNER JOIN cte ON cte.index_id=i.index_id AND cte.[object_id]=i.[object_id]
INNER JOIN sys.objects o ON i.[object_id]=o.[object_id]
WHERE o.[type] IN ('U','V') AND i.[type]>0
The PATH XML the FOR ( ''), the TYPE) .Value ( '', 'NVARCHAR (MAX.)');
--Exec sp_executesql @sql
careful reader will find, above last statement (exec sp_executesql @sql) are commented out direct execution so that you can quickly delete all the most useless index. But, as we said above, after sys.dm_db_index_usage_stats record since the last restart or reset the database or offline usage statistics to the index, the index used its records may be incomplete (if we had recently restarted database services, server hosting the database or reset the dynamic view), this could lead to some useful index has to be deleted, remember, remember, remember, the generated script can not be directly executed. Insurance practice is, at least, a database service running in a month doing such a thing, if there are frequent database maintained restart the service, already used index records can be collected before the restart of the database maintenance. After several months or a year of record, you do not need to finalize the index, then delete it.
----------------
Disclaimer: This article is CSDN blogger "three empty Taoist" original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/zhoujunah/article/details/85615576

Guess you like

Origin www.cnblogs.com/footleg/p/12053802.html