经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
SQL Server 索引碎片整理
来源:cnblogs  作者:武穆逸仙  时间:2020/11/9 15:55:48  对本文有异议

索引碎片整理的四种方法:

1)删除索引并重建

2)使用 DROP_EXISTING 语句重建索引

3)使用 ALTER INDEX REBUILD 语句重建索引

4)使用 ALTER INDEX REORGANIZE 重新组织索引

  1. --1.查看碎片
  2. SELECT DB_NAME() AS DatbaseName ,
  3. SCHEMA_NAME(o.Schema_ID) AS SchemaName ,
  4. OBJECT_NAME(s.[object_id]) AS TableName ,
  5. i.name AS IndexName ,
  6. ROUND(s.avg_fragmentation_in_percent, 2) AS [Fragmentation %] ,
  7. CASE WHEN avg_fragmentation_in_percent > 30 THEN '严重碎片,索引需要重建'
  8. WHEN avg_fragmentation_in_percent >= 5
  9. AND avg_fragmentation_in_percent < 30 THEN '轻度碎片,索引需要重新组织'
  10. ELSE '正常状态'
  11. END 提示
  12. FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) s
  13. INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
  14. INNER JOIN sys.objects o ON i.object_id = O.object_id
  15. ORDER BY [Fragmentation %] DESC
  16. --2.整理碎片(建议在空闲时间运行,尤其不要在生产环境运行)
  17. SET NOCOUNT ON
  18. DECLARE @Objectid INT ,
  19. @Indexid INT ,
  20. @schemaname VARCHAR(100) ,
  21. @tablename VARCHAR(300) ,
  22. @ixname VARCHAR(500) ,
  23. @avg_fip FLOAT ,
  24. @command VARCHAR(4000)
  25. DECLARE IX_Cursor CURSOR
  26. FOR
  27. SELECT A.object_id ,
  28. A.index_id ,
  29. QUOTENAME(SS.NAME) AS schemaname ,
  30. QUOTENAME(OBJECT_NAME(B.object_id, B.database_id)) AS tablename ,
  31. QUOTENAME(A.name) AS ixname ,
  32. B.avg_fragmentation_in_percent AS avg_fip
  33. FROM sys.indexes A
  34. INNER JOIN sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL,
  35. NULL, 'LIMITED') AS B ON A.object_id = B.object_id
  36. AND A.index_id = B.index_id
  37. INNER JOIN SYS.OBJECTS OS ON A.object_id = OS.object_id
  38. INNER JOIN sys.schemas SS ON OS.schema_id = SS.schema_id
  39. WHERE B.avg_fragmentation_in_percent > 10
  40. AND B.page_count > 20
  41. AND A.index_id > 0
  42. AND A.IS_DISABLED <> 1
  43. --AND OS.name='book'
  44. ORDER BY avg_fip DESC ,
  45. tablename ,
  46. ixname
  47. OPEN IX_Cursor
  48. FETCH NEXT FROM IX_Cursor INTO @Objectid, @Indexid, @schemaname, @tablename,
  49. @ixname, @avg_fip
  50. WHILE @@FETCH_STATUS = 0
  51. BEGIN
  52. --碎片率>5%或<=30%,索引重组
  53. IF @avg_fip < 30.0
  54. SET @command = N'ALTER INDEX ' + @ixname + N' ON ' + @schemaname
  55. + N'.' + @tablename + N' REORGANIZE ';
  56. --碎片率>=30%,索引重建
  57. IF @avg_fip >= 30.0
  58. AND @Indexid = 1
  59. BEGIN
  60. IF EXISTS ( SELECT *
  61. FROM SYS.columns
  62. WHERE OBJECT_ID = @Objectid
  63. AND max_length IN ( -1, 16 ) )
  64. SET @command = N'ALTER INDEX ' + @ixname + N' ON '
  65. + @schemaname + N'.' + @tablename + N' REBUILD ';
  66. ELSE
  67. SET @command = N'ALTER INDEX ' + @ixname + N' ON '
  68. + @schemaname + N'.' + @tablename + N' REBUILD '
  69. + N' WITH (ONLINE = ON)';
  70. END
  71. IF @avg_fip >= 30.0
  72. AND @Indexid > 1
  73. BEGIN
  74. IF EXISTS ( SELECT *
  75. FROM SYS.index_columns IC
  76. INNER JOIN SYS.columns CS ON CS.OBJECT_ID = IC.OBJECT_ID
  77. AND CS.column_id = IC.column_id
  78. WHERE IC.OBJECT_ID = @Objectid
  79. AND IC.index_id = @Indexid
  80. AND CS.max_length IN ( -1, 16 ) )
  81. SET @command = N'ALTER INDEX ' + @ixname + N' ON '
  82. + @schemaname + N'.' + @tablename + N' REBUILD ';
  83. ELSE
  84. SET @command = N'ALTER INDEX ' + @ixname + N' ON '
  85. + @schemaname + N'.' + @tablename + N' REBUILD '
  86. + N' WITH (ONLINE = ON)';
  87. END
  88. --打印命令,单独执行
  89. PRINT @command
  90. --直接执行命令
  91. --EXEC(@command)
  92. FETCH NEXT FROM IX_Cursor INTO @Objectid, @Indexid, @schemaname,@tablename, @ixname, @avg_fip
  93. END
  94. CLOSE IX_Cursor
  95. DEALLOCATE IX_Cursor

 

原文链接:http://www.cnblogs.com/star8521/p/13946784.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号