在线整理索引碎片脚本
2008-08-06 19:19
260 查看
/**************************************
作用:在线整理索引碎片
时间:2008-5-15
说明:
根据微软的示例改编
适用于无法停机重建索引的情况,
在线整理索引碎片,并更新表的统计信息以增强效果。
另外可以自定义需要整理的索引的逻辑碎片程度,扫描密度,页数
必须要在数据库访问较少的时候执行
***************************************/
CREATE PROC [dbo].[SYS_IndexDrag]
AS
BEGIN
SET NOCOUNT ON
DECLARE @tablename VARCHAR (128)
DECLARE @execstr VARCHAR (255)
DECLARE @objectid INT
DECLARE @indexid INT
DECLARE @frag DECIMAL
DECLARE @maxfrag DECIMAL--逻辑碎片
DECLARE @MaxScanDensity DECIMAL--扫描密度
DECLARE @Page INT --8k页数,设定一个限度对一定大的索引进行整理
DECLARE @TmpName VARCHAR(150)
-- Decide on the maximum fragmentation to allow
SELECT @maxfrag = 30.0
SELECT @MaxScanDensity=70.0
SELECT @Page=400
-- Declare cursor
DECLARE tables CURSOR FOR
SELECT 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 cursor for list of indexes to be defragged
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
FROM #fraglist
WHERE (LogicalFrag >= @maxfrag OR [ScanDensity]<=@MaxScanDensity) AND [CountPages]>=@page
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
-- Open the cursor
OPEN indexes
-- loop through the indexes
FETCH NEXT
FROM indexes
INTO @tablename, @objectid, @indexid, @frag
WHILE @@FETCH_STATUS = 0
BEGIN
--在线整理碎片
PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@tablename) + ',
' + RTRIM(@indexid) + ') - fragmentation currently '
+ RTRIM(CONVERT(varchar(15),@frag)) + '%'
SELECT @execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@objectid) + ',
' + RTRIM(@indexid) + ')'
EXEC (@execstr)
--更新统计信息
IF @TmpName<>@tablename
BEGIN
SET @tmpName=@tableName
EXEC('UPDATE STATISTICS '+@TableName)
END
FETCH NEXT
FROM indexes
INTO @tablename, @objectid, @indexid, @frag
END
-- Close and deallocate the cursor
CLOSE indexes
DEALLOCATE indexes
-- Delete the temporary table
DROP TABLE #fraglist
END
GO
作用:在线整理索引碎片
时间:2008-5-15
说明:
根据微软的示例改编
适用于无法停机重建索引的情况,
在线整理索引碎片,并更新表的统计信息以增强效果。
另外可以自定义需要整理的索引的逻辑碎片程度,扫描密度,页数
必须要在数据库访问较少的时候执行
***************************************/
CREATE PROC [dbo].[SYS_IndexDrag]
AS
BEGIN
SET NOCOUNT ON
DECLARE @tablename VARCHAR (128)
DECLARE @execstr VARCHAR (255)
DECLARE @objectid INT
DECLARE @indexid INT
DECLARE @frag DECIMAL
DECLARE @maxfrag DECIMAL--逻辑碎片
DECLARE @MaxScanDensity DECIMAL--扫描密度
DECLARE @Page INT --8k页数,设定一个限度对一定大的索引进行整理
DECLARE @TmpName VARCHAR(150)
-- Decide on the maximum fragmentation to allow
SELECT @maxfrag = 30.0
SELECT @MaxScanDensity=70.0
SELECT @Page=400
-- Declare cursor
DECLARE tables CURSOR FOR
SELECT 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 cursor for list of indexes to be defragged
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
FROM #fraglist
WHERE (LogicalFrag >= @maxfrag OR [ScanDensity]<=@MaxScanDensity) AND [CountPages]>=@page
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
-- Open the cursor
OPEN indexes
-- loop through the indexes
FETCH NEXT
FROM indexes
INTO @tablename, @objectid, @indexid, @frag
WHILE @@FETCH_STATUS = 0
BEGIN
--在线整理碎片
PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@tablename) + ',
' + RTRIM(@indexid) + ') - fragmentation currently '
+ RTRIM(CONVERT(varchar(15),@frag)) + '%'
SELECT @execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@objectid) + ',
' + RTRIM(@indexid) + ')'
EXEC (@execstr)
--更新统计信息
IF @TmpName<>@tablename
BEGIN
SET @tmpName=@tableName
EXEC('UPDATE STATISTICS '+@TableName)
END
FETCH NEXT
FROM indexes
INTO @tablename, @objectid, @indexid, @frag
END
-- Close and deallocate the cursor
CLOSE indexes
DEALLOCATE indexes
-- Delete the temporary table
DROP TABLE #fraglist
END
GO
相关文章推荐
- SQLSERVER索引碎片整理脚本
- 微软MSDN提供的索引碎片整理脚本
- SQL Server 2005:索引碎片整理脚本(网摘)
- SQL Server 2005:索引碎片整理脚本
- 索引碎片情况,表行数,批整理脚本
- SQL Server 2005:索引碎片整理脚本
- Sql server 日记 (索引整理脚本2(在线整理))
- SQL索引碎片整理脚本
- SQL Server 2005:索引碎片整理脚本
- 提升SQL Server速度整理索引碎片
- 索引碎片的整理
- [5月6日的脚本] 检测指定数据库的索引碎片百分比 (SQL)
- [转] SQL Server 2005索引碎片整理SQL语句
- MSSQL数据库表索引碎片整理优化性能
- 索引碎片整理
- Microsoft SQL Server 2000 索引碎片整理最佳实践
- Microsoft SQL Server 2000 索引碎片整理最佳实践(下)
- MSSQL数据库表索引碎片整理优化性能
- 索引碎片整理
- 计算索引碎片的一个脚本