Quantcast
Channel: SQL Server High Availability and Disaster Recovery forum
Viewing all articles
Browse latest Browse all 4689

Running rebuild index script but not working well for me.

$
0
0
USE master
GO

-- Variables to be used in the script
DECLARE @name VARCHAR(200)
DECLARE @TRNBackupPath VARCHAR(2000)

-- Declare a cursor that will return all user databases
DECLARE db_cursor CURSOR fast_forward
FOR
        SELECT  s.[name]
        FROM    sys.databases s
        WHERE   s.recovery_model_desc != 'SIMPLE'
            -- AND s.[name] NOT IN ( 'master', 'model', 'msdb', 'tempdb',  'distribution' )
                AND s.[name] IN ( 'PRODUCTION')
                AND s.[state_desc] = 'ONLINE'
                AND s.[user_access_desc] = 'MULTI_USER'
                AND s.[name] IN ( SELECT    DB_NAME(database_id)
                                  FROM      sys.database_recovery_status
                                  WHERE     last_log_backup_lsn IS NOT NULL )
        ORDER BY s.[name]

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
 -- Bring the first database name into the @name variable

WHILE @@FETCH_STATUS = 0 -- loop through each of the databases, until no records left
      BEGIN
            SET @TRNBackupPath = 'Z:\DBLogBackup\' + @name + '\'
--        SET @TRNBackupPath = 'NUL'
        -- Rebuild Indexes
            EXEC master.dbo.dba_indexDefrag_sp @executeSQL = 1, @printCommands = 1, @debugMode = 1, @printFragmentation = 1, @forceRescan = 1,
                @maxDopRestriction = 8, @minPageCount = 8, @maxPageCount = NULL, @minFragmentation = 10,   -- usually 10
                @rebuildThreshold = 15, -- usually 30
                @defragDelay = '00:00:02', @defragOrderColumn = 'page_count', @defragSortOrder = 'DESC', @excludeMaxPartition = 1, @timeLimit = 300, -- in minutes
                @database = @name, @jobname = 'TransactionLogs.Subplan_1', @PctLog = 75, @Path = @TRNBackupPath, @UseSQLAgent = 1, @FillFactor = 90

            FETCH NEXT FROM db_cursor INTO @name -- Return the database
      END

CLOSE db_cursor
DEALLOCATE db_cursor


GO

i run this script to defrag my tables/indexes,

but when i run i see the log below

Undusting the cogs and starting up...
Beginning validation...
Your selected parameters are...
            Defrag indexes with fragmentation greater than 10;
            Rebuild indexes with fragmentation greater than 15;
            You DO want the commands to be executed automatically;
            You want to defrag indexes in DESC order of the PAGE_COUNT value;
            You have specified a time limit of 300 minutes;
            The myNIC_PROD_East database will be defragged;
            ALL tables will be defragged;
            We WILL be rescanning indexes;
            The scan will be performed in LIMITED mode;
            You want to limit defrags to indexes with more than 8 pages;
            Indexes will be defragged ONLINE;
            Indexes will be sorted in TEMPDB;
            Defrag operations will utilize 8 processors;
            You DO want to print the ALTER INDEX commands;
            You DO want to output fragmentation levels;
            You want to wait 00:00:02 (hh:mm:ss) between defragging indexes;
            You want to run in DEBUG mode.
Grabbing a list of our databases...
Looping through our list of databases and checking for fragmentation...
  working on PRODUCTION...
Looping through our list... there are 649 indexes to defrag!
  Picking an index to beat into shape...
  Looking up the specifics for our index...
  Grabbing the partition count...
  Seeing if there are any LOBs to be handled...
  Checking for indexes that do not allow page locks...
  Building our SQL statements...
Executing: ALTER INDEX [IX_Giant_column] ON [PRODUCTION].[dbo].[GIANT_Table] REBUILD WITH (ONLINE = ON, FILLFACTOR = 90, SORT_IN_TempDB = ON, MAXDOP = 8)
Query was cancelled by user.

and it seems like that it starts from Giant Table which is the biggest not sure why is it missing rest of the tables, and this cause time outs in application and i end up killing the process.

Please adevise.

Regards


k



Viewing all articles
Browse latest Browse all 4689

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>