Home > Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, TSQL, Vishal Pawar > BI SQL # 145 : SQL Server DBA Scripts : Defrag all indexes within a database

BI SQL # 145 : SQL Server DBA Scripts : Defrag all indexes within a database

Hi Folks,

In this article we are going to cover How to Defrag all indexes within a database.

In this post we are going to discuss following points:

  • Problem Statement of SQL Script:
  • Description of SQL Script:
  • SQL Script Code
  • SQL Script Output Screenshot
  • User Level to execute

Problem Statement of SQL Script:

Defrag all indexes within a database.

Description of SQL Script:

This Transact-SQL statements select’s all defragmented indexes in descending order of the fragmentation grade and where the grad is higher then a defined minimum percentage value.

SQL Script Code

DECLARE @TableName SYSNAME
    ,@IndexID SMALLINT
    ,@IndexName SYSNAME;
DECLARE @MinFragPer FLOAT
    ,@FragScale AS VARCHAR(5);

-- Filter for fragmentation grade in percent. 
SET @MinFragPer = 10.0;

-- Declaration of a cursor with all concered indexes 
-- exceed the defined min fragmentation grade. 
DECLARE Fragment CURSOR LOCAL
FOR
SELECT SCH.NAME + '.' + OBJ.NAME AS TableName
    ,IDX.index_id AS IndexID
    ,IDX.NAME AS IndexName
    ,CONVERT(VARCHAR(5), ROUND(FRG.
            avg_fragmentation_in_percent, 2)) AS FragScale
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 
        NULL) AS FRG
INNER JOIN sys.objects AS OBJ ON FRG.object_id = OBJ.object_id
INNER JOIN sys.schemas AS SCH ON OBJ.schema_id = SCH.schema_id
INNER JOIN sys.indexes AS IDX ON FRG.index_id = IDX.index_id
    AND FRG.object_id = IDX.object_id
WHERE IDX.index_id > 0 -- no Heaps 
    AND FRG.avg_fragment_size_in_pages > 1 -- at least one page 
    AND FRG.avg_fragmentation_in_percent > @MinFragPer
ORDER BY FRG.fragment_count DESC
    ,FRG.page_count DESC
    ,FRG.avg_fragment_size_in_pages DESC;

-- Open cursor with indexes & table names. 
OPEN Fragment;

FETCH NEXT
FROM Fragment
INTO @TableName
    ,@IndexID
    ,@IndexName
    ,@FragScale;

WHILE @@FETCH_STATUS = 0
BEGIN
    -- Info print out. 
    PRINT @Tablename + ' - ' + @IndexName + '(' + CONVERT(VARCHAR(10)
            , @IndexID) + ') = ' + @FragScale + ' %';

    DBCC INDEXDEFRAG (
            0
            ,@TableName
            ,@IndexID
            )
    WITH NO_INFOMSGS;

    FETCH NEXT
    FROM Fragment
    INTO @TableName
        ,@IndexID
        ,@IndexName
        ,@FragScale;
END;

-- Close and dealloc cursor. 
CLOSE Fragment;

DEALLOCATE Fragment;

SQL Script Output Screenshot

image

User Level to execute

    400

    Hope you will like How to Defrag all indexes within a database.

    If you really like reading my blog and understood at least few thing then please don’t forget to subscribe my blog.

If you want daily link and analysis or interesting link go to following website which will give @ your inbox please subscribe our following link resource blog :

Link Resource Website

For More information related to BI World visit my Mentalist Blog

SQL Server Mentalist >> SQL Learning Blog

Business Intelligence Mentalist >> BI World

Infographic Mentalist >> Image worth explaining thousand Words

Microsoft Mentalist >> MVC,ASP.NET, WCF & LinQ

DBA Mentalist >>Advance SQL Server Blog

Microsoft BI Mentalist >> MS BI Development Update

Connect With me on

| FaceBook |Twitter | linkedIn| Google+ | WordPress | RSS |

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: