SQL Server 索引维护sql语句

使用以下脚本查看数据库索引碎片的大小情况:


复制代码 代码如下:

dbcc showcontig with fast, tableresults, all_indexes, no_infomsgs

以下使用脚本来处理维护作业:


复制代码 代码如下:

/*perform a ‘use <database name>’ to select the database in which to run the script.*/

— declare variables

set nocount on;

declare @tablename varchar(255);

declare @execstr varchar(400);

declare @objectid int;

declare @indexname varchar(500);

declare @indexid int;

declare @frag decimal;

declare @maxfrag decimal;

declare @tmpname varchar(500);

— declare @tmpname =”

set @tmpname = ”

— decide on the maximum fragmentation to allow for.

select @maxfrag = 30.0;

— declare a cursor.

declare tables cursor for

select table_schema + ‘.’ + table_name

from information_schema.tables

where table_type = ‘base table’;

— create the table.

create table #fraglist (

objectname char(255),

objectid int,

indexname char(255),

indexid int,

lvl int,

countpages int,

countrows int,

minrecsize int,

maxrecsize int,

avgrecsize int,

forreccount int,

extents int,

extentswitches int,

avgfreebytes int,

avgpagedensity int,

scandensity decimal,

bestcount int,

actualcount int,

logicalfrag decimal,

extentfrag decimal);

— open the cursor.

open tables;

— loop through all the tables in the database.

fetch next

from tables

into @tablename;

while @@fetch_status = 0

begin;

— do the showcontig of all indexes of the table

insert into #fraglist

exec (‘dbcc showcontig (”’ + @tablename + ”’)

with fast, tableresults, all_indexes, no_infomsgs’);

fetch next

from tables

into @tablename;

end;

— close and deallocate the cursor.

close tables;

deallocate tables;

— declare the cursor for the list of indexes to be defragged.

declare indexes cursor for

select objectname, objectid,indexname,indexid, logicalfrag

from #fraglist

where indexproperty (objectid, indexname, ‘indexdepth’) > 0;

— open the cursor.

open indexes;

— loop through the indexes.

fetch next

from indexes

into @tablename, @objectid, @indexname,@indexid, @frag;

while @@fetch_status = 0

begin;

if @frag < @maxfrag

begin

select @execstr = ‘alter index [‘ + rtrim(@indexname) + ‘] on [‘ + rtrim(@tablename) + ‘] reorganize with ( lob_compaction = on ) ‘

print @maxfrag + ‘ ‘ + @execstr

end

else

begin

select @execstr = ‘alter index [‘ + rtrim(@indexname) + ‘] on [‘ + rtrim(@tablename) + ‘] rebuild with ( pad_index = off, statistics_norecompute = off, allow_row_locks = on, allow_page_locks = on, sort_in_tempdb = off, online = off )’

print @maxfrag + ‘ ‘ + @execstr

end

exec (@execstr);

–更新统计信息

if @tmpname<>@tablename

begin

set @tmpname=@tablename

print ‘update statistics ‘+@tablename + ‘ with fullscan ‘

exec (‘update statistics ‘+@tablename + ‘ with fullscan ‘)

end

fetch next

from indexes

into @tablename, @objectid, @indexname,@indexid, @frag;

end;

— close and deallocate the cursor.

close indexes;

deallocate indexes;

— delete the temporary table.

drop table #fraglist;

go

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐