5.3 find missing indexes

5.3 find missing indexes

The benefits of the index, most people know, but also a harm index. For SQL Server in the index (or even all database management systems, and even then most of worldly things) concerned, and there is no absolute good or absolute bad. Suitable primary. Too many indexes lead to update, delete, insert performance problems. Index too little can lead to multi-query slow CPU, IO and more, a full table scan.

For the core table: all indexes do not more than six.

For ordinary table: not all indexes more than four.

For small tables: all indexes not more than three.

5.3.1 need to find the missing index table

It can always hard to create the appropriate index on the appropriate column in the production system. After SQL2008, when a query execution, SQLServer optimizer chooses the most appropriate index for execution when not find a suitable index, then the optimizer generates a suboptimal execution plan, and the information is lost indexes stored in the DMVs .

When the SQLServer service is restarted, it will store information on DMVs lost, so it is best to use normal in about 90 days to collect this information, would be more effective. But not all index, the index is probably some 90 genius used once, indexing should be appropriate.

 

sys.dm_db_missing_index_details: Returns detailed information about missing indexes.

sys.dm_db_missing_index_group_stats: return summary information for a particular index.

sys.dm_db_missing_index_groups: return on the index which lost the handle contains information which is lost in the index group.

sys.dm_db_missing_index_columns (index_Handle): returns information about the columns in the loss index.

Main Field Description

 

avg_total_user_cost group can be reduced by the index average cost user query.

After avg_user_impact achieve this missing index group, the average percentage of user queries possible gain. This value represents the realization if this missing index group, the query cost an average decline Click percentage.

user_seeks may have been used by the group recommended that the index number to find user queries caused.

user_scans may have been used by the group recommended number of scans an index of user queries caused.

index_handle: identify a specific missing index. The identifier is unique in the server

group_handle: identifying missing index group, a group index contains only one index.

5.3.2 find missing indexes

 Find the missing index tables and columns

SELECT mig.*, statement AS table_name, 

    column_id, column_name, column_usage 

FROM sys.dm_db_missing_index_details AS mid 

CROSS APPLY sys.dm_db_missing_index_columns (mid.index_handle) 

INNER JOIN sys.dm_db_missing_index_groups AS mig ON mig.index_handle = mid.index_handle 

ORDER BY mig.index_group_handle, mig.index_handle, column_id; 

 

Find a missing index up

SELECT TOP 10 * 

FROM sys.dm_db_missing_index_group_stats 

ORDER BY avg_total_user_cost * avg_user_impact * (user_seeks + user_scans)DESC; 

 

Combined Query

SELECT  avg_total_user_cost * avg_user_impact * ( user_scans + user_seeks ) AS  PossibleImprovement ,

        last_user_seek ,

        last_user_scan ,

        [statement] AS [Object] ,

        'CREATE INDEX [IDX_' + CONVERT(VARCHAR(32), GS.group_handle) + '_'

        + CONVERT(VARCHAR(32), D.index_handle) + '_'

        + REPLACE(REPLACE(REPLACE([STATEMENT], ']', ''), '[', ''), '.', '')

        + ']' + ' ON ' + [statement] + ' (' + ISNULL(equality_columns, '')

        + CASE WHEN equality_columns IS NOT NULL

                    AND inequality_columns IS NOT NULL THEN ','

               ELSE ''

          END + ISNULL(inequality_columns, '') + ')' + ISNULL(' INCLUDE ('

                                                              + included_columns

                                                              + ')', '') AS Create_Index_Syntax

FROM    sys.dm_db_missing_index_groups AS G

        INNER JOIN sys.dm_db_missing_index_group_stats AS GS ON G.index_group_handle = GS.group_handle

        INNER JOIN sys.dm_db_missing_index_details AS D ON G.index_handle = D.index_handle

ORDER BY PossibleImprovement DESC

Published 37 original articles · won praise 0 · Views 2420

Guess you like

Origin blog.csdn.net/syjhct/article/details/86559832
5.3