SQL Server 索引维护sql语句

本文提供了一段SQL Server数据库脚本,用于检查并整理数据库中的索引碎片。脚本首先通过DBCC SHOWCONTIG命令获取所有表的索引碎片情况,并创建临时表存储这些信息。接着遍历所有表,针对不同碎片比例采取重组或重建索引的操作,并更新统计信息。

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

复制代码代码如下:

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 

转载于:https://www.cnblogs.com/zcm123/p/3828834.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值