Define heavily fragmented indexes and rebuild for a single database / table SQL Server 2005/2008

I am looking for a script that will identify highly fragmented indexes for one database and, possibly, one table, it seems to me, sys.dm_db_index_physical_statsquite resource-intensive.

Can I also turn off the table? I read that it is possible somewhere?

+3
source share
3 answers

Here is one from MSDN:

--This example shows a simple way to defragment all indexes in a database that is fragmented above a declared threshold.

/*Perform a 'USE <database name>' to select the database in which to run the script.*/
-- Declare variables
SET NOCOUNT ON
DECLARE @tablename VARCHAR (128)
DECLARE @execstr   VARCHAR (255)
DECLARE @objectid  INT
DECLARE @indexid   INT
DECLARE @frag      DECIMAL
DECLARE @maxfrag   DECIMAL

-- Decide on the maximum fragmentation to allow
SELECT @maxfrag = 30.0

-- 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
      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)

   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
GO
+6
source

run this query:

SELECT  OBJECT_NAME(DMV.object_id) AS TABLE_NAME ,
    SI.NAME AS INDEX_NAME ,
    avg_fragmentation_in_percent AS FRAGMENT_PERCENT ,
    DMV.record_count
FROM    sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'SAMPLED')
    AS DMV
    LEFT OUTER JOIN SYS.INDEXES AS SI ON DMV.OBJECT_ID = SI.OBJECT_ID
                                         AND DMV.INDEX_ID = SI.INDEX_ID
WHERE   avg_fragmentation_in_percent > 10
    AND index_type_desc IN ( 'CLUSTERED INDEX', 'NONCLUSTERED INDEX' )
    AND DMV.record_count >= 2000
ORDER BY TABLE_NAME DESC

And all indexes that have FRAGMENT_PERCENTmore than 30% of the most tunable.

+1
source

Frag > Target. 1 , 2  ( .: -)

EXECUTE master.sys.sp_MSforeachdb "USE [?]; SELECT 'use' + DB_NAME (db_id()) + ';' SELECT 'ALTER INDEX ['       + SI.NAME + '] ON [' + OBJECT_NAME (DMV.object_id) + '] REBUILD WITH (ONLINE = ON);'       + '- :' + convert (varchar (40), avg_fragmentation_in_percent)   FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, SAMPLED) DMV   LEFT OUTER JOIN SYS.INDEXES AS SI ON DMV.OBJECT_ID = SI.OBJECT_ID                                         DMV.INDEX_ID = SI.INDEX_ID   WHERE avg_fragmentation_in_percent > 29     AND index_type_desc IN (" CLUSTERED INDEX "," ")      DMV.record_count >= 2000   ORDER BY OBJECT_NAME (DMV.object_id), SI.NAME"

-1

Source: https://habr.com/ru/post/1784690/


All Articles