首页 > 资讯列表 > 编程/数据库 >> 数据库操作教程

SQLServer索引维护sql语句

数据库操作教程 2022-09-23 16:57:32 转载来源: 网络整理/侵权必删

使用以下脚本查看数据库索引碎片的大小情况:复制代码代码如下:DBCCSHOWCONTIGWITHFAST,TABLERESULTS,ALL_INDEXES,NO_INFOMSGS以下使用脚本来处理维护作业:复制代码代码如下:/*Performa'USE<databasename>'toselectthedatabaseinwhichtorunthescript.*/--DeclarevariablesSETNOCOUNTON;DECLARE@tablenamevarchar(255);DECLARE@execstrvarchar(400);DECLARE@objectidint;Declare@IndexNamevarchar(500);DECLARE@indexidint;DECLARE@fragdecimal;DECLARE@maxfragdecimal;DECLARE@TmpNamevarchar(500);--Declare@TmpName=''set@TmpName=''--Decideonthemaximumfragmentationtoallowfor

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

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

标签: SQLServer 索引 维护 sql 语句


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持