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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值