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

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

 

相关文章:

  • 2021-12-18
  • 2022-12-23
  • 2022-02-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
猜你喜欢
  • 2021-10-12
  • 2021-06-21
  • 2021-12-11
  • 2021-12-01
相关资源
相似解决方案