您的位置:首页 > 数据库

SQL Server调优系列进阶篇 - 如何重建数据库索引

2015-05-13 18:35 393 查看
随着数据的数据量的急剧增加,数据库的性能也会明显的有些缓慢这个时候你可以考虑下重建索引或是重新组织索引了。

DBCC SHOWCONTIG('表名') 可以查看当前表的索引碎情况。

重建索引

方法一:

DECLARE @name varchar(100)

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, '', 90)
/*--填充因子:90,建议60-90之间,100的查询性能最好,但插入数据后会导致索引页迁移会影响修改的性能.*/
FETCH NEXT FROM authors_cursor
INTO @name
END

Close authors_cursor
Deallocate authors_cursor


方法二:

DECLARE  tables CURSOR for select name from sysobjects where xtype='U'

DECLARE @table_name char(128)

Open tables

Fetch next from tables into @table_name

While @@fetch_status=0
Begin
EXECUTE ('ALTER INDEX ALL ON ' + @table_name + ' REBUILD')
Fetch next from tables into @table_name
End

Close tables
Deallocate tables
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: