SQL Server索引碎片处理的实际操作流程

今天我们要向大家讲解的是SQL Server索引碎片处理的实际操作流程,我们大家都知道SQL Server数据库随着实际使用时间的增长,会让人觉得越来越慢,这个与你平时没有合理的维护计划有关系,定期处理索引碎片是一个必不可少的工作内容之一。

具体信息参考msdn

http://msdn.microsoft.com/zh-cn/library/ms189858.ASPx 我工作中碰到一张表,有320万记录,数据表占用空间800多兆,所有索引碎片大于80%,甚至有100%,索引占用空间500兆,重新生成索引后占用空间减小到200多兆。 一个可以在SQL2005中测试的脚本

 

drop database db_index_test 建立测试环境  
create database db_index_test  
go  
use db_index_test  
go  
create table tbTest(rownum int identity(1,1),id varchar(100),date datetime)  
go  
create index index_id on tbTest(id) go 

插入测试数据,并适当删除一部分数据

declare @i int  
set @i=1 
while @i<10 
begin  
insert into tbTest(id,date)  
select newid(),getdate() from syscolumns  
delete from tbTest where rownum%2=0 
set @i=@i+1  
end  
go  

检查索引

 
 
SELECT avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats (DB_ID(),   
OBJECT_ID(N’tbTest’),  NULL, NULL, NULL) AS a JOIN sys.indexes AS b ON a.  
object_id = b.object_id AND a.index_id = b.index_id where name=’index_id’

go 重建索引

alter index index_id on tbTest rebuild go 

检查索引

 
 
SELECT avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats (DB_ID(),   
OBJECT_ID(N’tbTest’), NULL, NULL, NULL) AS a JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.  
index_id = b.index_id where name=’index_id’ 删除测试环境 go use master go drop database db_index_test  
go  

在sql的客户端工具SQL Server Management Studio中也可以手动检查并重建索引。以上的相关内容就是对讲解SQL Server索引碎片处理的介绍,望你能有所收获。

SQLServer中的索引碎片处理
 

上述的相关内容就是对SQL Server索引碎片处理的描述,希望会给你带来一些帮助在此方面。

转载于:https://www.cnblogs.com/aji88/archive/2010/07/30/1788491.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值