Last active
September 16, 2020 04:57
-
-
Save mizuneko/715c6071f4a51e1cc5875faa5bbcd105 to your computer and use it in GitHub Desktop.
[不足しているINDEX調査] SQLServerが起動してから、運用している間に不足していると判断されたINDEXを取得します。#SQLServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 不足しているINDEXを調査します。 | |
-- SQLServerが起動してから、運用している間に不足していると判断されたINDEXです。 | |
-- INDEXを追加する場合は、挿入、更新、削除のクエリで処理速度が悪化するため | |
-- システム全体の影響を確認した上で採用するか判断してください。 | |
SELECT user_seeks * avg_total_user_cost * (avg_user_impact * 0.01) AS index_advantage, | |
migs.last_user_seek AS [前回のseek日時], | |
mid.statement AS [テーブル名], | |
migs.unique_compiles, | |
migs.user_seeks, | |
migs.avg_total_user_cost AS [平均コスト], | |
N'USE [' + dbs.name + N']' + NCHAR(13) + NCHAR(10) | |
+ N'GO' + NCHAR(13) + NCHAR(10) | |
+ N'CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]' + NCHAR(13) + NCHAR(10) | |
+ N'ON ' + mid.statement + N' (' + ISNULL(mid.equality_columns, N'') | |
+ IIF(mid.equality_columns IS NULL, N'', IIF(mid.inequality_columns IS NULL, N'', N',')) | |
+ ISNULL(mid.inequality_columns, N'') + N')' + NCHAR(13) + NCHAR(10) | |
+ IIF(mid.included_columns IS NULL, N'', N'INCLUDE (' + ISNULL(mid.included_columns, N'') + N')' + NCHAR(13) + NCHAR(10)) | |
+ N'GO' AS [不足しているINDEX], | |
migs.avg_user_impact AS [コストの向上率] | |
FROM sys.dm_db_missing_index_group_stats AS migs WITH (NOLOCK) | |
INNER JOIN sys.dm_db_missing_index_groups AS mig WITH (NOLOCK) | |
ON migs.group_handle = mig.index_group_handle | |
INNER JOIN sys.dm_db_missing_index_details AS mid WITH (NOLOCK) | |
ON mig.index_handle = mid.index_handle | |
INNER JOIN sys.databases AS dbs WITH (NOLOCK) | |
ON mid.database_id = dbs.database_id | |
ORDER BY index_advantage DESC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment