SQL Server索引碎片處理的實(shí)際操作流程
今天我們要向大家講解的是SQL Server索引碎片處理的實(shí)際操作流程,我們大家都知道SQL Server數(shù)據(jù)庫隨著實(shí)際使用時(shí)間的增長,會讓人覺得越來越慢,這個(gè)與你平時(shí)沒有合理的維護(hù)計(jì)劃有關(guān)系,定期處理索引碎片是一個(gè)必不可少的工作內(nèi)容之一。
具體信息參考msdn
http://msdn.microsoft.com/zh-cn/library/ms189858.ASPx 我工作中碰到一張表,有320萬記錄,數(shù)據(jù)表占用空間800多兆,所有索引碎片大于80%,甚至有100%,索引占用空間500兆,重新生成索引后占用空間減小到200多兆。 一個(gè)可以在SQL2005中測試的腳本
- drop database db_index_test 建立測試環(huán)境
- 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
插入測試數(shù)據(jù),并適當(dāng)刪除一部分?jǐn)?shù)據(jù)
- 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’ 刪除測試環(huán)境 go use master go drop database db_index_test
- go
在sql的客戶端工具SQL Server Management Studio中也可以手動(dòng)檢查并重建索引。以上的相關(guān)內(nèi)容就是對講解SQL Server索引碎片處理的介紹,望你能有所收獲。
上述的相關(guān)內(nèi)容就是對SQL Server索引碎片處理的描述,希望會給你帶來一些幫助在此方面。
【編輯推薦】