重建数据库表所有索引

来源:互联网 发布:中国货币供给量数据 编辑:程序博客网 时间:2024/06/08 04:46

/***********************
重建数据库表所有索引
2006-06-12

先选择要修复的数据库
***********************/

DECLARE @name nvarchar(255)

--所有用户表游标
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, '', 70)

    -- Get the next author.
    FETCH NEXT FROM authors_cursor INTO @name
END

CLOSE authors_cursor
DEALLOCATE authors_cursor

go

 

/***********************
重建数据库表所有索引(SQL2005之后版本)
2012-06-12

先选择要修复的数据库
***********************/

DECLARE @name nvarchar(255)
--所有用户表游标
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
 print '正在重建表['+@name+']索引'   
    --修复数据表索引
 exec ('ALTER INDEX  ALL ON ['+@name+'] REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON,STATISTICS_NORECOMPUTE = ON)');
    -- Get the next author.
    FETCH NEXT FROM authors_cursor INTO @name
END

CLOSE authors_cursor
DEALLOCATE authors_cursor

go

/***********************
重建指定表的索引
2006-06-12

先选择要修复的数据库
***********************/

--第一步:查看是否需要维护,查看扫描密度/Scan Density是否为100%
declare @table_id int
set @table_id=object_id('表名')
dbcc showcontig(@table_id)

--第二步:重构表索引
dbcc dbreindex('表名',pk_索引名,100)

--重做第一步,如发现扫描密度/Scan Density还是小于100%则重构表的所有索引
--并不一定能达100%。
dbcc dbreindex('表名','',100)

0 0
原创粉丝点击