首页数据库SQL SERVER 2008 R2 重建索引的方法

SQL SERVER 2008 R2 重建索引的方法

时间2024-02-28 21:27:03发布访客分类数据库浏览746
导读:收集整理的这篇文章主要介绍了SQL SERVER 2008 R2 重建索引的方法,觉得挺不错的,现在分享给大家,也给大家做个参考。 参考Sys.dm_db_index_physical_...
收集整理的这篇文章主要介绍了SQL SERVER 2008 R2 重建索引的方法,觉得挺不错的,现在分享给大家,也给大家做个参考。

参考Sys.dm_db_index_physical_stats

检查索引碎片情况

1.SELECT2.OBJECT_NamE(object_id) as objectname,3.object_id AS objectid,4.index_id AS indexid,5.partITion_number AS partitionnum,6.avg_fragmentation_in_PErcent AS fra7.From sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, ‘LIMITED')8.WHERE avg_fragmentation_in_percent >
     10.0 AND index_id >
     0;
    9. 10.使用脚本中的 sys.dm_db_index_physical_stats 重新生成或重新组织索引 (来源于联机帮助)11. 12.SET NOCOUNT ON;
    13.DECLARE @objectid int;
    14.DECLARE @indexid int;
    15.DECLARE @partitioncount Bigint;
    16.DECLARE @schemaname nvArchar(130);
    17.DECLARE @objectname nVARchar(130);
    18.DECLARE @indexname nvarchar(130);
    19.DECLARE @partitionnum bigint;
    20.DECLARE @partitions bigint;
    21.DECLARE @frag float;
    22.DECLARE @command nvarchar(4000);
    23.– Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function24.– and convert object and index IDs to names.25.SELECT26.object_id AS objectid,27.index_id AS indexid,28.partition_number AS partitionnum,29.avg_fragmentation_in_percent AS frag30.INTO #work_to_do31.FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, ‘LIMITED')32.WHERE avg_fragmentation_in_percent >
     10.0 AND index_id >
     0;
    33.– Declare the cursor for the list of partitions to be PRocessed.34.DECLARE partitions CURSOR FOR SELECT * FROM #work_to_do;
    35.– Open the cursor.36.OPEN partitions;
    37.– Loop through the partitions.38.WHILE (1=1)39.BEGIN;
    40.FETCH NEXT41.FROM partitions42.INTO @objectid, @indexid, @partitionnum, @frag;
    43.IF @@FETCH_STATUS  0 brEAK;
    44.SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name)45.FROM sys.objects AS o46.JOIN sys.schemas as s ON s.schema_id = o.schema_id47.WHERE o.object_id = @objectid;
    48.SELECT @indexname = QUOTENAME(name)49.FROM sys.indexes50.WHERE object_id = @objectid AND index_id = @indexid;
    51.SELECT @partitioncount = count (*)52.FROM sys.partitions53.WHERE object_id = @objectid AND index_id = @indexid;
    54.– 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.55.IF @frag  30.056.SET @command = N‘ALTER INDEX ‘ + @indexname + N‘ ON ‘ + @schemaname + N‘.' + @objectname + N‘ REORGANIZE';
    57.IF @frag >
    = 30.058.SET @command = N‘ALTER INDEX ‘ + @indexname + N‘ ON ‘ + @schemaname + N‘.' + @objectname + N‘ REBUILD';
    59.IF @partitioncount >
     160.SET @command = @command + N‘ PARTITION=' + CAST(@partitionnum AS nvarchar(10));
    61.EXEC (@command);
    62.PRINT N‘Executed: ‘ + @command;
    63.END;
    64.– Close and deallocate the cursor.65.CLOSE partitions;
    66.DEALLOCATE partitions;
    67.– Drop the temporary table.68.DROP TABLE #work_to_do;
    69.GO
您可能感兴趣的文章:
  • sqlserver索引的原理及索引建立的注意事项小结
  • SQL Server 索引介绍
  • SQL Server 聚集索引和非聚集索引的区别分析
  • SQLSERVER全文目录全文索引的使用方法和区别讲解
  • SQLSERVER 创建索引实现代码
  • SQLSERVER聚集索引和主键(Primary Key)的误区认识
  • sqlserver 索引的一些总结
  • SQL Server全文索引服务
  • 提升SQL Server速度 整理索引碎片
  • SqlServer索引的原理与应用详解

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: SQL SERVER 2008 R2 重建索引的方法
本文地址: https://pptw.com/jishu/631964.html
sql server 2008数据库连接字符串大全 SQL Server 2008 R2安装配置方法图文教程

游客 回复需填写必要信息