優化數據庫之重建整理索引

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:表格內文; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --><!-- [endif]-->

-- 運行環境

/*

Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) Nov 24 2008 13:01:59

Copyright (c) 1988-2005 Microsoft Corporation Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)

*/

USE master

go

IF OBJECT_ID ( 'SP_RefreshIndex' , 'P' ) IS NOT NULL

DROP PROCEDURE SP_RefreshIndex

go

CREATE PROCEDURE SP_RefreshIndex

AS

IF db_id ()< 5

RETURN

PRINT N' 優化 DB:' + DB_Name ()

SET NOCOUNT ON ;

DECLARE @command varchar ( 8000);

DECLARE partitions CURSOR FOR

SELECT

CASE WHEN a. avg_fragmentation_in_percent< 30.0 THEN 'ALTER INDEX ' + d. Name + ' ON ' + c. NAME + '.' + b. NAME + ' REORGANIZE '

ELSE 'ALTER INDEX [' + d. Name + '] ON [' + c. NAME + '].[' + b. NAME + '] REBUILD' END +

CASE WHEN partitioncount> 1 THEN ' PARTITION=' + rtrim ( a. partition_number)+ ';' ELSE ';' END

FROM sys.dm_db_index_physical_stats ( DB_ID (), NULL, NULL , NULL, 'LIMITED' ) AS a

INNER JOIN sys.objects AS b ON a. object_id = b. object_id

INNER JOIN sys.schemas AS c ON c. schema_id = b. schema_id

INNER JOIN sys.indexes AS d ON d. object_id = a. object_id AND d. index_id= a. index_id

INNER JOIN ( SELECT object_id , index_id, partitioncount= COUNT (*) FROM sys.partitions GROUP BY object_id , index_id) AS e ON e. object_id = a. object_id AND e. index_id= a. index_id

WHERE a. avg_fragmentation_in_percent > 10.0 AND a. index_id > 0 AND d. Name IS NOT NULL

OPEN partitions;

FETCH NEXT FROM partitions INTO @command;

WHILE @@FETCH_STATUS = 0

BEGIN

EXEC ( @command);

FETCH NEXT FROM partitions INTO @command;

END

CLOSE partitions;

DEALLOCATE partitions;

GO

exec sp_ms_marksystemobject 'SP_RefreshIndex' -- 標記為系統存儲過程

go

EXEC sp_msforeachdb 'use [?] exec SP_RefreshIndex' -- 優化整個實例所有庫

你可能感兴趣的:(C++,c,Microsoft,C#,Go)