索引建议

SELECT TOP 10
        database_name = DB_NAME(details.database_id) ,
        schema_name = SCHEMA_NAME(tb.schema_id) ,
        object_name = tb.name ,
        avg_estimated_impact = dm_migs.avg_user_impact * ( dm_migs.user_seeks
                                                           + dm_migs.user_scans ) ,
        last_user_seek = dm_migs.last_user_seek ,
        create_index = 'CREATE INDEX [IX_' + OBJECT_NAME(details.object_id,
                                                         details.database_id)
        + '_' + REPLACE(REPLACE(REPLACE(ISNULL(details.equality_columns, ''),
                                        ', ', '_'), '[', ''), ']', '')
        + CASE WHEN details.equality_columns IS NOT NULL
                    AND details.inequality_columns IS NOT NULL THEN '_'
               ELSE ''
          END + REPLACE(REPLACE(REPLACE(ISNULL(details.inequality_columns, ''),
                                        ', ', '_'), '[', ''), ']', '') + ']'
        + ' ON ' + details.statement + ' (' + ISNULL(details.equality_columns,
                                                     '')
        + CASE WHEN details.equality_columns IS NOT NULL
                    AND details.inequality_columns IS NOT NULL THEN ','
               ELSE ''
          END + ISNULL(details.inequality_columns, '') + ')'
        + ISNULL(' INCLUDE (' + details.included_columns + ')', '')
FROM    sys.dm_db_missing_index_groups AS dm_mig WITH ( NOLOCK )
        INNER JOIN sys.dm_db_missing_index_group_stats AS dm_migs WITH ( NOLOCK ) ON dm_migs.group_handle = dm_mig.index_group_handle
        INNER JOIN sys.dm_db_missing_index_details AS details WITH ( NOLOCK ) ON dm_mig.index_handle = details.index_handle
        INNER JOIN sys.tables AS tb WITH ( NOLOCK ) ON details.object_id = tb.object_id
WHERE   details.database_id = DB_ID()
ORDER BY avg_estimated_impact DESC;
GO

 

posted @ 2020-06-17 11:22  山顶洞外人  阅读(139)  评论(0编辑  收藏  举报