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索引碎片处理的描述,希望会给你带来一些帮助在此方面。

posted @ 2010-07-30 10:28  db's jim  阅读(315)  评论(0编辑  收藏  举报