sqlserver2008 查看数据库自带的索引建议

时间:2021-07-04 00:31:11
SELECT 

        [Total Cost]  = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans),0) 

        , avg_user_impact

        , TableName = statement

        , [EqualityUsage] = equality_columns 

        , [InequalityUsage] = inequality_columns

        , [Include Cloumns] = included_columns

FROM        sys.dm_db_missing_index_groups g 

INNER JOIN    sys.dm_db_missing_index_group_stats s 

       ON s.group_handle = g.index_group_handle 

INNER JOIN    sys.dm_db_missing_index_details d 

       ON d.index_handle = g.index_handle

ORDER BY [Total Cost] DESC;
SELECT

    c.name AS databasename,

    c.equality_columns,

    c.inequality_columns,

    c.included_columns,

    c.statement AS tablename,

    c.avg_total_user_cost AS ReducingTheAverageCost,

    c.avg_user_impact AS PercentageOfRevenue,

    c.last_user_seek AS TheLastTimeTheEffectAfterUse,

    c.unique_compiles

FROM (SELECT

    a.name,

    b.*

FROM   (SELECT

           d.*,

           s.avg_total_user_cost,

           s.avg_user_impact,

           s.last_user_seek,

           s.unique_compiles

       FROM   sys.dm_db_missing_index_group_stats s,

              sys.dm_db_missing_index_groups g,

              sys.dm_db_missing_index_details d

       WHERE s.group_handle = g.index_group_handle

       AND d.index_handle = g.index_handle

       AND s.avg_user_impact > 90

       --AND s.unique_compiles > 10

       --order by s.avg_user_impact desc

       ) b,

       sys.databases a

WHERE a.database_id = b.database_id) c

WHERE c.name = 'xxxxx'

ORDER BY PercentageOfRevenue DESC, unique_compiles DESC

 


/*SELECT         [Total Cost]  = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans),0)         , avg_user_impact        , TableName = statement        , [EqualityUsage] = equality_columns         , [InequalityUsage] = inequality_columns        , [Include Cloumns] = included_columnsFROM        sys.dm_db_missing_index_groups g INNER JOIN    sys.dm_db_missing_index_group_stats s        ON s.group_handle = g.index_group_handle INNER JOIN    sys.dm_db_missing_index_details d        ON d.index_handle = g.index_handleORDER BY [Total Cost] DESC;*/
/*SELECT    c.name AS databasename,    c.equality_columns,    c.inequality_columns,    c.included_columns,    c.statement AS tablename,    c.avg_total_user_cost AS ReducingTheAverageCost,    c.avg_user_impact AS PercentageOfRevenue,    c.last_user_seek AS TheLastTimeTheEffectAfterUse,    c.unique_compilesFROM (SELECT    a.name,    b.*FROM   (SELECT           d.*,           s.avg_total_user_cost,           s.avg_user_impact,           s.last_user_seek,           s.unique_compiles       FROM   sys.dm_db_missing_index_group_stats s,              sys.dm_db_missing_index_groups g,              sys.dm_db_missing_index_details d       WHERE s.group_handle = g.index_group_handle       AND d.index_handle = g.index_handle       AND s.avg_user_impact > 90       --AND s.unique_compiles > 10       --order by s.avg_user_impact desc       ) b,       sys.databases aWHERE a.database_id = b.database_id) cWHERE c.name = 'cfbb'ORDER BY PercentageOfRevenue DESC, unique_compiles DESC*/
/*
CREATE NONCLUSTERED INDEX IDX_EmailON [cfbb].[dbo].[Member]([Email])--INCLUDE([Id], [MemberId])With (online=on)*/