SqlServer 重建索引脚本

  • 众所周知随着表的数据量不断增长,会产生很多索引的碎片。这时候需要重建索引来提高查询性能。
    USE [DBName]
    
    DECLARE @name varchar(100)
     
    DECLARE authors_cursor CURSOR FOR  
    Select [name]   from sysobjects where xtype='u' order by id
     
    OPEN authors_cursor -- 开启游标执行任务
     
    FETCH NEXT FROM authors_cursor  INTO @name  --将游标向下移1行,获取的数据放入之前定义的变量@name
     
    WHILE @@FETCH_STATUS = 0  -- 正常读取
    BEGIN    
       DBCC DBREINDEX (@name, '', 90)
       FETCH NEXT FROM authors_cursor  
       INTO @name 
    END
     
    deallocate authors_cursor  --释放游标
  • 你也可以把脚本执行计划设置到定时执行任务计划之内
原文地址:https://www.cnblogs.com/chongyao/p/13695570.html