当前位置:   article > 正文

Sql缺失索引查询,自动创建执行语句

Sql缺失索引查询,自动创建执行语句

 

 试图查询确实的索引

  1. CREATE VIEW [dbo].[vw_Index_MissingIndex]
  2. AS
  3. SELECT '[' + d.name + ']' as DBName,[dbo].[fn_Index_CreateIndexName](mid.equality_columns,mid.Inequality_columns,mid.index_handlE) AS ID,
  4. REPLACE(mid.equality_columns,',',' ASC,') AS equality_columns,
  5. REPLACE(mid.Inequality_columns,',',' ASC,') AS Inequality_columns,
  6. mid.Included_columns,
  7. mid.[statement]
  8. FROM sys.dm_db_missing_index_details as mid
  9. INNER JOIN sys.databases d
  10. on d.database_id = mid.database_id
  11. GO

给索引命名 

  1. CREATE FUNCTION [dbo].[fn_Index_CreateIndexName] (@equality_columns NVARCHAR(4000), @Inequality_columns NVARCHAR(max), @index_handlE INT) RETURNS VARCHAR(max)
  2. AS
  3. BEGIN
  4. declare @IndexName NVARCHAR(MAX)
  5. SET @IndexName = ISNULL(@equality_columns,@Inequality_columns)
  6. SET @IndexName = LTRIM(REPLACE(@IndexName,'[','_'))
  7. SET @IndexName = RTRIM(REPLACE(@IndexName,']','_'))
  8. SET @IndexName = REPLACE(@IndexName,',','')
  9. SET @IndexName = REPLACE(@IndexName,'_ _','_')
  10. IF LEN(@IndexName) > 120
  11. BEGIN
  12. SET @IndexName = SUBSTRING(@IndexName,0,120)
  13. END
  14. SET @IndexName = @IndexName + CAST(@index_handlE AS NVARCHAR(15))
  15. RETURN @IndexName
  16. END
  17. GO

创建索引语句,按查询表方式返回

  1. /*注意@DBNAME 为[数据库名字]*/
  2. create PROCEDURE [dbo].[usp_Index_MissingIndexCreationStatements](@DBNAME VARCHAR(100))
  3. AS
  4. DECLARE @IndexCreationPlaceholder_Start AS NVARCHAR(MAX)
  5. DECLARE @IndexCreationPlaceholder_End AS NVARCHAR(MAX)
  6. -- PREPARE PLACEHOLDER
  7. SET @IndexCreationPlaceholder_Start = 'IF NOT EXISTS (SELECT * FROM {2}.sys.indexes WHERE [name] = ''IX_{0}'' )
  8. BEGIN
  9. CREATE NONCLUSTERED INDEX [IX_{0}] ON {1}';
  10. SET @IndexCreationPlaceholder_End = ' WITH (PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF,ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  11. ON [PRIMARY]
  12. END;' + char(13) + char(10)
  13. -- STATEMENT CREATION
  14. SELECT DBName,CASE
  15. WHEN NOT mid.equality_columns IS NULL AND NOT mid.Inequality_columns IS NULL THEN
  16. REPLACE(REPLACE(REPLACE(@IndexCreationPlaceholder_Start,'{0}', mid.ID),
  17. '{1}',mid.[statement]),'{2}',mid.DBName)
  18. + '( ' + COALESCE(mid.equality_columns,'') + ' ASC,' +
  19. COALESCE(mid.Inequality_columns,'') + ' ASC )' +
  20. COALESCE('INCLUDE ( ' + mid.Included_columns + ' ) ','')
  21. + @IndexCreationPlaceholder_End
  22. WHEN mid.equality_columns IS NULL AND NOT mid.Inequality_columns IS NULL THEN
  23. REPLACE(REPLACE(REPLACE(@IndexCreationPlaceholder_Start,'{0}', mid.ID),'{1}',mid.[statement]),'{2}',mid.DBName)
  24. + ' ( ' + COALESCE(mid.Inequality_columns,'') + ' ASC ) ' +
  25. COALESCE('INCLUDE ( ' + mid.Included_columns + ' ) ','')
  26. + @IndexCreationPlaceholder_End
  27. WHEN NOT mid.equality_columns IS NULL AND mid.Inequality_columns IS NULL THEN
  28. REPLACE(REPLACE(REPLACE(@IndexCreationPlaceholder_Start,'{0}', mid.ID),'{1}',mid.[statement]),'{2}',mid.DBName)
  29. + ' ( ' + COALESCE(mid.equality_columns,'') + ' ASC) '
  30. +COALESCE('INCLUDE ( ' + mid.Included_columns + ' ) ','')
  31. + @IndexCreationPlaceholder_End
  32. ELSE NULL
  33. END AS Index_Creation_Statement,
  34. ' DROP INDEX [IX_' + mid.ID + '] ON ' + mid.[statement] + char(13) + char(10) AS Index_Drop_Statement FROM [dbo].[vw_Index_MissingIndex] AS mid
  35. WHERE DBName = @DBNAME
  36. GO

创建索引,直接打印为日志,复制日志直接执行

  1. create PROCEDURE [dbo].[usp_Index_MissingIndexCreationStatements_PRINT](@DBNAME VARCHAR(100))
  2. AS
  3. DECLARE @IndexCreationPlaceholder_Start AS NVARCHAR(MAX)
  4. DECLARE @IndexCreationPlaceholder_End AS NVARCHAR(MAX)
  5. -- PREPARE PLACEHOLDER
  6. SET @IndexCreationPlaceholder_Start = 'IF NOT EXISTS (SELECT * FROM {2}.sys.indexes WHERE [name] = ''IX_{0}'' )
  7. BEGIN
  8. CREATE NONCLUSTERED INDEX [IX_{0}] ON {1}
  9. ';
  10. SET @IndexCreationPlaceholder_End = '
  11. WITH (PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF,ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  12. ON [PRIMARY]
  13. END;' + char(13) + char(10)
  14. -- STATEMENT CREATION
  15. declare @Index_Creation_Statement varchar(max)
  16. declare @Index_Drop_Statement varchar(max)
  17. SELECT CASE
  18. WHEN NOT mid.equality_columns IS NULL AND NOT mid.Inequality_columns IS NULL THEN
  19. REPLACE(REPLACE(REPLACE(@IndexCreationPlaceholder_Start,'{0}', mid.ID),
  20. '{1}',mid.[statement]),'{2}',mid.DBName)
  21. + '( ' + COALESCE(mid.equality_columns,'') + ' ASC,
  22. ' +
  23. COALESCE(mid.Inequality_columns,'') + ' ASC ) ' + char(13) + char(10) + '
  24. ' +
  25. COALESCE('INCLUDE ( ' + mid.Included_columns + ' ) ','')
  26. + @IndexCreationPlaceholder_End
  27. WHEN mid.equality_columns IS NULL AND NOT mid.Inequality_columns IS NULL THEN
  28. REPLACE(REPLACE(REPLACE(@IndexCreationPlaceholder_Start,'{0}', mid.ID),'{1}',mid.[statement]),'{2}',mid.DBName)
  29. + ' ( ' + COALESCE(mid.Inequality_columns,'') + ' ASC ) ' +
  30. COALESCE('INCLUDE ( ' + mid.Included_columns + ' ) ','')
  31. + @IndexCreationPlaceholder_End
  32. WHEN NOT mid.equality_columns IS NULL AND mid.Inequality_columns IS NULL THEN
  33. REPLACE(REPLACE(REPLACE(@IndexCreationPlaceholder_Start,'{0}', mid.ID),'{1}',mid.[statement]),'{2}',mid.DBName)
  34. + ' ( ' + COALESCE(mid.equality_columns,'') + ' ASC) '
  35. +COALESCE('INCLUDE ( ' + mid.Included_columns + ' ) ','')
  36. + @IndexCreationPlaceholder_End
  37. ELSE NULL
  38. END AS Index_Creation_Statement,
  39. ' DROP INDEX [IX_' + mid.ID + '] ON ' + mid.[statement] + char(13) + char(10) AS Index_Drop_Statement INTO #TEST_INDEX_TMP FROM [dbo].[vw_Index_MissingIndex] AS mid
  40. WHERE DBName = @DBNAME
  41. DECLARE INDEX_CURSOR CURSOR FOR SELECT * FROM #TEST_INDEX_TMP
  42. OPEN INDEX_CURSOR
  43. fetch NEXT FROM INDEX_CURSOR into @Index_Creation_Statement,@Index_Drop_Statement
  44. print 'use' + @DBNAME
  45. print 'Go'
  46. while @@FETCH_STATUS = 0
  47. begin
  48. print @Index_Creation_Statement
  49. print 'Go'
  50. fetch NEXT FROM INDEX_CURSOR into @Index_Creation_Statement,@Index_Drop_Statement;
  51. end
  52. close INDEX_CURSOR
  53. deallocate INDEX_CURSOR
  54. SELECT * FROM #TEST_INDEX_TMP
  55. TRUNCATE TABLE #TEST_INDEX_TMP;
  56. DROP TABLE #TEST_INDEX_TMP;
  57. GO

执行

exec [usp_Index_MissingIndexCreationStatements_PRINT] '[TEST]'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/409508
推荐阅读
相关标签
  

闽ICP备14008679号