SQL Server 调优系列进阶篇

随着数据的数据量的急剧增加,数据库的性能也会明显的有些缓慢这个时候你可以考虑下重建索引或是重新组织索引了。

DBCC SHOWCONTIG('表名') 可以查看当前表的索引碎情况。

重建索引

方法一:

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  
  
WHILE @@FETCH_STATUS = 0   
BEGIN      
  
    DBCC DBREINDEX (@name, '', 90)  
    /*--填充因子:90,建议60-90之间,100的查询性能最好,但插入数据后会导致索引页迁移会影响修改的性能.*/
    FETCH NEXT FROM authors_cursor    
    INTO @name   
END  

Close authors_cursor
Deallocate authors_cursor 

方法二:

DECLARE  tables CURSOR for select name from sysobjects where xtype='U'

DECLARE @table_name char(128)

Open tables

Fetch next from tables into @table_name

While @@fetch_status=0
Begin
    EXECUTE ('ALTER INDEX ALL ON ' + @table_name + ' REBUILD')
    Fetch next from tables into @table_name
End

Close tables
Deallocate tables
原文地址:https://www.cnblogs.com/MuNet/p/5886216.html