索引使用狀況

感謝承修提供了一些關於索引使用的查詢

SELECT

    ObjectName      = object_schema_name(idx.object_id) + ‘.’ + object_name(idx.object_id) 

    ,IndexName      = idx.name

    ,IndexType      = CASE

                        WHEN is_unique = 1 THEN ‘UNIQUE ‘

                        ELSE " END + idx.type_desc 

    ,User_Seeks     = us.user_seeks 

    ,User_Scans     = us.user_scans 

    ,User_Lookups   = us.user_lookups 

    ,User_Updates   = us.user_updates 

FROM sys.indexes idx 

LEFT JOIN sys.dm_db_index_usage_stats us 

    ON idx.object_id = us.object_id 

    AND idx.index_id = us.index_id 

    AND us.database_id = db_id() 

WHERE object_schema_name(idx.object_id) != ‘sys’

ORDER BY us.user_seeks + us.user_scans + us.user_lookups DESC

 

SELECT
  migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure,
  ‘CREATE INDEX [missing_index_’ + CONVERT (varchar, mig.index_group_handle) + ‘_’ + CONVERT (varchar, mid.index_handle)
  + ‘_’ + LEFT (PARSENAME(mid.statement, 1), 32) + ‘]’
  + ‘ ON ‘ + mid.statement
  + ‘ (‘ + ISNULL (mid.equality_columns,")
    + CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ‘,’ ELSE " END
    + ISNULL (mid.inequality_columns, “)
  + ‘)’
  + ISNULL (‘ INCLUDE (‘ + mid.included_columns + ‘)’, “) AS create_index_statement,
  migs.*, mid.database_id, mid.[object_id]
FROM sys.dm_db_missing_index_groups mig
INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle
–WHERE migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) > 10
ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC

 

 

SELECT
  ‘CREATE INDEX [missing_index_’ + CONVERT (VARCHAR, mig.index_group_handle) + ‘_’ + CONVERT (VARCHAR, mid.index_handle)
  + ‘_’ + LEFT (PARSENAME(mid.STATEMENT, 1), 32) + ‘]’
  + ‘ ON ‘ + mid.STATEMENT
  + ‘ (‘ + ISNULL (mid.equality_columns,")
    + CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ‘,’ ELSE " END
    + ISNULL (mid.inequality_columns, “)
  + ‘)’
  + ISNULL (‘ INCLUDE (‘ + mid.included_columns + ‘)’, “) AS create_index_statement,
  migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure,
  migs.avg_user_impact AS [%Increase],
  db_name(mid.database_id) AS DatabaseName,
  object_name(mid.[object_id]) AS TableName
FROM sys.dm_db_missing_index_groups mig
INNER JOIN sys.dm_db_missing_index_group_stats migs
    ON migs.group_handle = mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details mid
    ON mig.index_handle = mid.index_handle
WHERE
    mid.database_id = DB_ID() –<——-Run from the db you want to query
    AND CONVERT (decimal (28,1),
        migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans)) > 10
ORDER BY improvement_measure DESC

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: