重建索引

来源:互联网 发布:stc单片机选型 编辑:程序博客网 时间:2024/04/29 06:58

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 * from tbtest

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'

alter index index_id on tbTest rebuild
go

原创粉丝点击