Log file getting filled up

  • Hi everyone,

    I've placed the log file of my database on a separate disk(32 GB). It's allowed to auto grow. I take log backups every 15 minutes. Re-index job runs every Sunday and it fills up the log file and the database gets into the situation where nothing could be done. What I'm not able to understand is why aren't the log backups working. Shouldn't they truncate the log everytime the log is backed up?

    Could you please provide me with steps to avoid this situation?

    Thank you.

  • A fast san can fill up 30 GB of log in less than 15 minutes.

    Sounds like you are doing everything right.

    I would check these 2 options out :

    What's the biggest table getting reindexed? The log cannot be backed up untill the transaction is over, so maybe the backup is not able to clear out the log as you think it is.

    You could also consider doing a log backup every 2-5 minutes during that operation.

    Also make sure you don't blindly reindex everything. Select only what needs reindexing.

    I use this script and it works very well :

    http://sqlfool.com/2010/04/index-defrag-script-v4-0/

    I tweaked the default settings a little bit for my environement. I use 10% minimum fragmentation and 9% for rebuild so I never reorganize. I've found out that rebuilds are faster on my system and generated a less noise in the logs.

    Moreoever when running the job daily the amount of work to do is very well spread out so it takes 5 - 10 less times daily.

    2 final things that helped : The db has a default fill factor of 100%. I started monitoring the execution logs and moved the FF down to 85% for every index that was updated more than once a week.

    After 3-4 months of doing so I then move the FF of every other indexes to 85% (the actual used space was around 93% so it wasn't a big deal... like 5% increase in DB size).

    Since then I get 2-3 indexes rebuild daily on average. The job now runs regularly in less than 5 minutes. So I could probably run it during the day if I had to.

    Hope this helps!

  • Ninja's_RGR'us (6/15/2011)


    A fast san can fill up 30 GB of log in less than 15 minutes.

    Sounds like you are doing everything right.

    I would check these 2 options out :

    What's the biggest table getting reindexed? The log cannot be backed up untill the transaction is over, so maybe the backup is not able to clear out the log as you think it is.

    You could also consider doing a log backup every 2-5 minutes during that operation.

    Also make sure you don't blindly reindex everything. Select only what needs reindexing.

    I use this script and it works very well :

    http://sqlfool.com/2010/04/index-defrag-script-v4-0/

    I tweaked the default settings a little bit for my environement. I use 10% minimum fragmentation and 9% for rebuild so I never reorganize. I've found out that rebuilds are faster on my system and generated a less noise in the logs.

    Moreoever when running the job daily the amount of work to do is very well spread out so it takes 5 - 10 less times daily.

    2 final things that helped : The db has a default fill factor of 100%. I started monitoring the execution logs and moved the FF down to 85% for every index that was updated more than once a week.

    After 3-4 months of doing so I then move the FF of every other indexes to 85% (the actual used space was around 93% so it wasn't a big deal... like 5% increase in DB size).

    Since then I get 2-3 indexes rebuild daily on average. The job now runs regularly in less than 5 minutes. So I could probably run it during the day if I had to.

    Hope this helps!

    :w00t: Awesome info Ninja's! I am just starting to take snaps of index stats at the new shop (no baselines to be had, heaps everywhere, found tons of NC indexes with 99+% frag :-P) and am putting a process together for them to keep their indexes healthy. I had bits and pieces of what you described covered from previous gigs but I'll be following your lead on this one. Thanks for posting!

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Ya I read the same info about heaps (if you were not the teacher about that topic). It would certainly be a good idea to find all the tables without clustered index, pick out the narrowest column of that table and then do a create clustered index WITH DROP_IMMEDIATE (no that's not a real option, but you get the idea).

    That would be a hell of a nice addition to Michelle's script.

    That and bringing back the update stats portion of it (was taken out because of a bug, and then she had her baby girl so that's been on hold for over 1 year now).

    For stats I have a big enough window to do a daily full scan of all stats (wether or not they need it). Since I get little to no index rebuilds I don't care about that 0.01% overlap.

    I've been meaning to get this working smarter but never had a chance.

  • I just got done reformatting the proc with SQL Prompt...next stop, comb through it line by line so I understand what it's doing before unleashing it on the world 😀

    If I see anything I think is worth tightening up or if I do any of my own "enhancements" I'll post back.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • You can do a blank run with @execute = 0 and @Print = 1.

    You can try with maybe just the 75% and 1000 pages and then go down gradually to 30% and then 10% if you can.

    AFAIK the script is smart enough to not rescan the indexes for fragmentation if it runs only x minutes after the previous run. I don't know exactly what rule is applied there but there's something to avoid the frag scan hit in there when it can.

  • Hi Guys,

    I've some problem with running script. I've created a separate DB for the script tables "UtilityDB".

    When I run a script it's back to me with "Procedure dba_indexDefrag_sp dropped" and did nothing.

    Hope for your help.

    Thanks

    Pav

  • That creates the script objects. You need to call the sp for it to run.

  • Hi,

    THanks for your fast reply first!

    I know, I'm using enabled EXECUTE dbo.dba_indexDefrag_sp @executeSQL = 1 statement in a script.

    I'm sure I'm doing something wrong, but can't find what exactly. Sorry for may be stupid questions, but I'm new in sql.

    Pav

  • pavel 74570 (10/13/2011)


    Hi,

    THanks for your fast reply first!

    I know, I'm using enabled EXECUTE dbo.dba_indexDefrag_sp @executeSQL = 1 statement in a script.

    I'm sure I'm doing something wrong, but can't find what exactly. Sorry for may be stupid questions, but I'm new in sql.

    Pav

    Are you sure you have fragmentation anywhere?

  • I have one index with >100k pages and 98% of avg fragmentation.

  • Do you have a time limit?

    DB or objet exclusion?

    Worse comes to worse I can always give you my version of the code.

  • @timeLimit = NULL

    I didn't define any exclusions.

    Can you please post your version of code?

    Thanks

  • This has been in prod for over 1 year without any issues. It's based on version 4.0 of the script (4.1 is out now).

    I've tweaked the settings for my environement and I've also done 1-2 upgrades for better loging.

    Tables

    USE [master]

    GO

    /****** Objet : Table [dbo].[dba_indexDefragStatus] Date de génération du script : 10/13/2011 11:12:17 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE TABLE [dbo].[dba_indexDefragStatus](

    [databaseID] [int] NOT NULL,

    [databaseName] [nvarchar](128) NULL,

    [objectID] [int] NOT NULL,

    [indexID] [int] NOT NULL,

    [partitionNumber] [smallint] NOT NULL,

    [fragmentation] [float] NULL,

    [page_count] [int] NULL,

    [range_scan_count] [bigint] NULL,

    [schemaName] [nvarchar](128) NULL,

    [objectName] [nvarchar](128) NULL,

    [indexName] [nvarchar](128) NULL,

    [scanDate] [datetime] NULL,

    [defragDate] [datetime] NULL,

    [printStatus] [bit] NULL DEFAULT ((0)),

    [exclusionMask] [int] NULL DEFAULT ((0)),

    CONSTRAINT [PK_indexDefragStatus_v40] PRIMARY KEY CLUSTERED

    (

    [databaseID] ASC,

    [objectID] ASC,

    [indexID] ASC,

    [partitionNumber] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    USE [master]

    GO

    /****** Objet : Table [dbo].[dba_indexDefragLog] Date de génération du script : 10/13/2011 11:12:14 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[dba_indexDefragLog](

    [indexDefrag_id] [int] IDENTITY(1,1) NOT NULL,

    [databaseID] [int] NOT NULL,

    [databaseName] [nvarchar](128) NOT NULL,

    [objectID] [int] NOT NULL,

    [objectName] [nvarchar](128) NOT NULL,

    [indexID] [int] NOT NULL,

    [indexName] [nvarchar](128) NOT NULL,

    [partitionNumber] [smallint] NOT NULL,

    [fragmentation] [float] NOT NULL,

    [page_count] [int] NOT NULL,

    [dateTimeStart] [datetime] NOT NULL,

    [dateTimeEnd] [datetime] NULL,

    [durationSeconds] [int] NULL,

    [sqlStatement] [varchar](4000) NULL,

    [errorMessage] [varchar](1000) NULL,

    [schemaName] [nvarchar](128) NULL,

    CONSTRAINT [PK_indexDefragLog_v40] PRIMARY KEY CLUSTERED

    (

    [indexDefrag_id] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF

    USE [master]

    GO

    /****** Objet : Table [dbo].[dba_indexDefragExclusion] Date de génération du script : 10/13/2011 11:12:10 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE TABLE [dbo].[dba_indexDefragExclusion](

    [databaseID] [int] NOT NULL,

    [databaseName] [nvarchar](128) NOT NULL,

    [objectID] [int] NOT NULL,

    [objectName] [nvarchar](128) NOT NULL,

    [indexID] [int] NOT NULL,

    [indexName] [nvarchar](128) NOT NULL,

    [exclusionMask] [int] NOT NULL,

    CONSTRAINT [PK_indexDefragExclusion_v40] PRIMARY KEY CLUSTERED

    (

    [databaseID] ASC,

    [objectID] ASC,

    [indexID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    SP

    USE [master]

    GO

    /****** Objet : StoredProcedure [dbo].[dba_indexDefrag_sp] Date de génération du script : 10/13/2011 11:11:45 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    ALTER Procedure [dbo].[dba_indexDefrag_sp]

    /* Declare Parameters */

    @minFragmentation float = 10.0

    /* in percent, will not defrag if fragmentation less than specified */

    , @rebuildThreshold float = 0.0

    /* in percent, greater than @rebuildThreshold will result in rebuild instead of reorg */

    , @executeSQL bit = 1

    /* 1 = execute; 0 = print command only */

    , @defragOrderColumn nvarchar(20) = 'range_scan_count'

    /* Valid options are: range_scan_count, fragmentation, page_count */

    , @defragSortOrder nvarchar(4) = 'DESC'

    /* Valid options are: ASC, DESC */

    , @timeLimit int = 480 /* defaulted to 8 hours */

    /* Optional time limitation; expressed in minutes */

    , @database varchar(128) = Null

    /* Option to specify a database name; null will return all */

    , @databaseExclude varchar(8000) = NULL

    /* Option to specify a database name; null will return all */

    , @tableName varchar(4000) = Null -- databaseName.schema.tableName

    /* Option to specify a table name; null will return all */

    , @forceRescan bit = 1

    /* Whether or not to force a rescan of indexes; 1 = force, 0 = use existing scan, if available */

    , @scanMode varchar(10) = N'FULL' --from LIMITED on may 6th 2011 change #2

    /* Options are LIMITED, SAMPLED, and DETAILED */

    , @minPageCount int = 1000 --from 1000 on may 6th 2011, cancelled, caused more work than usefull

    /* MS recommends > 1 extent (8 pages) */

    , @maxPageCount int = Null

    /* NULL = no limit */

    , @excludeMaxPartition bit = 0

    /* 1 = exclude right-most populated partition; 0 = do not exclude; see notes for caveats */

    , @onlineRebuild bit = 0

    /* 1 = online rebuild; 0 = offline rebuild; only in Enterprise */

    , @sortInTempDB bit = 1

    /* 1 = perform sort operation in TempDB; 0 = perform sort operation in the index's database */

    , @maxDopRestriction tinyint = Null

    /* Option to restrict the number of processors for the operation; only in Enterprise */

    , @printCommands bit = 1

    /* 1 = print commands; 0 = do not print commands */

    , @printFragmentation bit = 0

    /* 1 = print fragmentation prior to defrag;

    0 = do not print */

    , @defragDelay char(8) = '00:00:05'

    /* time to wait between defrag commands */

    , @debugMode bit = 0

    /* display some useful comments to help determine if/where issues occur */

    As

    /*********************************************************************************

    Name: dba_indexDefrag_sp

    Author: Michelle Ufford, http://sqlfool.com

    Purpose: Defrags one or more indexes for one or more databases

    Notes:

    CAUTION: TRANSACTION LOG SIZE SHOULD BE MONITORED CLOSELY WHEN DEFRAGMENTING.

    DO NOT RUN UNATTENDED ON LARGE DATABASES DURING BUSINESS HOURS.

    @minFragmentation defaulted to 10%, will not defrag if fragmentation

    is less than that

    @rebuildThreshold defaulted to 30% as recommended by Microsoft in BOL;

    greater than 30% will result in rebuild instead

    @executeSQL 1 = execute the SQL generated by this proc;

    0 = print command only

    @defragOrderColumn Defines how to prioritize the order of defrags. Only

    used if @executeSQL = 1.

    Valid options are:

    range_scan_count = count of range and table scans on the

    index; in general, this is what benefits

    the most from defragmentation

    fragmentation = amount of fragmentation in the index;

    the higher the number, the worse it is

    page_count = number of pages in the index; affects

    how long it takes to defrag an index

    @defragSortOrder The sort order of the ORDER BY clause.

    Valid options are ASC (ascending) or DESC (descending).

    @timeLimit Optional, limits how much time can be spent performing

    index defrags; expressed in minutes.

    NOTE: The time limit is checked BEFORE an index defrag

    is begun, thus a long index defrag can exceed the

    time limitation.

    @database Optional, specify specific database name to defrag;

    If not specified, all non-system databases will

    be defragged.

    @tableName Specify if you only want to defrag indexes for a

    specific table, format = databaseName.schema.tableName;

    if not specified, all tables will be defragged.

    @forceRescan Whether or not to force a rescan of indexes. If set

    to 0, a rescan will not occur until all indexes have

    been defragged. This can span multiple executions.

    1 = force a rescan

    0 = use previous scan, if there are indexes left to defrag

    @scanMode Specifies which scan mode to use to determine

    fragmentation levels. Options are:

    LIMITED - scans the parent level; quickest mode,

    recommended for most cases.

    SAMPLED - samples 1% of all data pages; if less than

    10k pages, performs a DETAILED scan.

    DETAILED - scans all data pages. Use great care with

    this mode, as it can cause performance issues.

    @minPageCount Specifies how many pages must exist in an index in order

    to be considered for a defrag. Defaulted to 8 pages, as

    Microsoft recommends only defragging indexes with more

    than 1 extent (8 pages).

    NOTE: The @minPageCount will restrict the indexes that

    are stored in dba_indexDefragStatus table.

    @maxPageCount Specifies the maximum number of pages that can exist in

    an index and still be considered for a defrag. Useful

    for scheduling small indexes during business hours and

    large indexes for non-business hours.

    NOTE: The @maxPageCount will restrict the indexes that

    are defragged during the current operation; it will not

    prevent indexes from being stored in the

    dba_indexDefragStatus table. This way, a single scan

    can support multiple page count thresholds.

    @excludeMaxPartition If an index is partitioned, this option specifies whether

    to exclude the right-most populated partition. Typically,

    this is the partition that is currently being written to in

    a sliding-window scenario. Enabling this feature may reduce

    contention. This may not be applicable in other types of

    partitioning scenarios. Non-partitioned indexes are

    unaffected by this option.

    1 = exclude right-most populated partition

    0 = do not exclude

    @onlineRebuild 1 = online rebuild;

    0 = offline rebuild

    @sortInTempDB Specifies whether to defrag the index in TEMPDB or in the

    database the index belongs to. Enabling this option may

    result in faster defrags and prevent database file size

    inflation.

    1 = perform sort operation in TempDB

    0 = perform sort operation in the index's database

    @maxDopRestriction Option to specify a processor limit for index rebuilds

    @printCommands 1 = print commands to screen;

    0 = do not print commands

    @printFragmentation 1 = print fragmentation to screen;

    0 = do not print fragmentation

    @defragDelay Time to wait between defrag commands; gives the

    server a little time to catch up

    @debugMode 1 = display debug comments; helps with troubleshooting

    0 = do not display debug comments

    Called by: SQL Agent Job or DBA

    ----------------------------------------------------------------------------

    DISCLAIMER:

    This code and information are provided "AS IS" without warranty of any kind,

    either expressed or implied, including but not limited to the implied

    warranties or merchantability and/or fitness for a particular purpose.

    ----------------------------------------------------------------------------

    LICENSE:

    This index defrag script is free to download and use for personal, educational,

    and internal corporate purposes, provided that this header is preserved.

    Redistribution or sale of this index defrag script, in whole or in part, is

    prohibited without the author's express written consent.

    ----------------------------------------------------------------------------

    Date InitialsVersion Description

    ----------------------------------------------------------------------------

    2007-12-18 MFU 1.0 Initial Release

    2008-10-17 MFU 1.1 Added @defragDelay, CIX_temp_indexDefragList

    2008-11-17 MFU 1.2 Added page_count to log table

    , added @printFragmentation option

    2009-03-17 MFU 2.0 Provided support for centralized execution

    , consolidated Enterprise & Standard versions

    , added @debugMode, @maxDopRestriction

    , modified LOB and partition logic

    2009-06-18 MFU 3.0 Fixed bug in LOB logic, added @scanMode option

    , added support for stat rebuilds (@rebuildStats)

    , support model and msdb defrag

    , added columns to the dba_indexDefragLog table

    , modified logging to show "in progress" defrags

    , added defrag exclusion list (scheduling)

    2009-08-28 MFU 3.1 Fixed read_only bug for database lists

    2010-04-20 MFU 4.0 Added time limit option

    , added static table with rescan logic

    , added parameters for page count & SORT_IN_TEMPDB

    , added try/catch logic and additional debug options

    , added options for defrag prioritization

    , fixed bug for indexes with allow_page_lock = off

    , added option to exclude right-most partition

    , removed @rebuildStats option

    , refer to http://sqlfool.com for full release notes

    *********************************************************************************

    Example of how to call this script:

    Exec dbo.dba_indexDefrag_sp

    @executeSQL = 1

    , @printCommands = 1

    , @debugMode = 1

    , @printFragmentation = 1

    , @forceRescan = 1

    , @maxDopRestriction = 1

    , @minPageCount = 8

    , @maxPageCount = Null

    , @minFragmentation = 1

    , @rebuildThreshold = 30

    , @defragDelay = '00:00:05'

    , @defragOrderColumn = 'page_count'

    , @defragSortOrder = 'DESC'

    , @excludeMaxPartition = 1

    , @timeLimit = Null;

    *********************************************************************************/

    Set NoCount On;

    Set XACT_Abort On;

    Set Quoted_Identifier On;

    Begin

    Begin Try

    /* Just a little validation... */

    If @minFragmentation Is Null

    Or @minFragmentation Not Between 0.00 And 100.0

    Set @minFragmentation = 10.0;

    If @rebuildThreshold Is Null

    Or @rebuildThreshold Not Between 0.00 And 100.0

    Set @rebuildThreshold = 30.0;

    If @defragDelay Not Like '00:[0-5][0-9]:[0-5][0-9]'

    Set @defragDelay = '00:00:05';

    If @defragOrderColumn Is Null

    Or @defragOrderColumn Not In ('range_scan_count', 'fragmentation', 'page_count')

    Set @defragOrderColumn = 'range_scan_count';

    If @defragSortOrder Is Null

    Or @defragSortOrder Not In ('ASC', 'DESC')

    Set @defragSortOrder = 'DESC';

    If @scanMode Not In ('LIMITED', 'SAMPLED', 'DETAILED')

    Set @scanMode = 'LIMITED';

    If @debugMode Is Null

    Set @debugMode = 0;

    If @forceRescan Is Null

    Set @forceRescan = 0;

    If @sortInTempDB Is Null

    Set @sortInTempDB = 1;

    If @debugMode = 1 RaisError('Undusting the cogs and starting up...', 0, 42) With NoWait;

    /* Declare our variables */

    Declare @objectID int

    , @databaseID int

    , @databaseName nvarchar(128)

    , @indexID int

    , @partitionCount bigint

    , @schemaName nvarchar(128)

    , @objectName nvarchar(128)

    , @indexName nvarchar(128)

    , @partitionNumber smallint

    , @fragmentation float

    , @pageCount int

    , @sqlCommand nvarchar(4000)

    , @rebuildCommand nvarchar(200)

    , @dateTimeStart datetime

    , @dateTimeEnd datetime

    , @containsLOB bit

    , @editionCheck bit

    , @debugMessage nvarchar(4000)

    , @updateSQL nvarchar(4000)

    , @partitionSQL nvarchar(4000)

    , @partitionSQL_Param nvarchar(1000)

    , @LOB_SQL nvarchar(4000)

    , @LOB_SQL_Param nvarchar(1000)

    , @indexDefrag_id int

    , @startDateTime datetime

    , @endDateTime datetime

    , @getIndexSQL nvarchar(4000)

    , @getIndexSQL_Param nvarchar(4000)

    , @allowPageLockSQL nvarchar(4000)

    , @allowPageLockSQL_Param nvarchar(4000)

    , @allowPageLocks int

    , @excludeMaxPartitionSQL nvarchar(4000);

    /* Initialize our variables */

    Select @startDateTime = GetDate()

    , @endDateTime = DateAdd(minute, @timeLimit, GetDate());

    /* Create our temporary tables */

    Create Table #databaseList

    (

    databaseID int

    , databaseName varchar(128)

    , scanStatus bit

    );

    Create Table #processor

    (

    [index] int

    , Name varchar(128)

    , Internal_Value int

    , Character_Value int

    );

    Create Table #maxPartitionList

    (

    databaseID int

    , objectID int

    , indexID int

    , maxPartition int

    );

    If @debugMode = 1 RaisError('Beginning validation...', 0, 42) With NoWait;

    /* Make sure we're not exceeding the number of processors we have available */

    Insert Into #processor

    Execute xp_msver 'ProcessorCount';

    If @maxDopRestriction Is Not Null And @maxDopRestriction > (Select Internal_Value From #processor)

    Select @maxDopRestriction = Internal_Value

    From #processor;

    /* Check our server version; 1804890536 = Enterprise, 610778273 = Enterprise Evaluation, -2117995310 = Developer */

    If (Select ServerProperty('EditionID')) In (1804890536, 610778273, -2117995310)

    Set @editionCheck = 1 -- supports online rebuilds

    Else

    Set @editionCheck = 0; -- does not support online rebuilds

    /* Output the parameters we're working with */

    If @debugMode = 1

    Begin

    Select @debugMessage = 'Your selected parameters are...

    Defrag indexes with fragmentation greater than ' + Cast(@minFragmentation As varchar(10)) + ';

    Rebuild indexes with fragmentation greater than ' + Cast(@rebuildThreshold As varchar(10)) + ';

    You' + Case When @executeSQL = 1 Then ' DO' Else ' DO NOT' End + ' want the commands to be executed automatically;

    You want to defrag indexes in ' + @defragSortOrder + ' order of the ' + UPPER(@defragOrderColumn) + ' value;

    You have' + Case When @timeLimit Is Null Then ' not specified a time limit;' Else ' specified a time limit of '

    + Cast(@timeLimit As varchar(10)) End + ' minutes;

    ' + Case When @database Is Null Then 'ALL databases' Else 'The ' + @database + ' database' End + ' will be defragged;

    ' + Case When @tableName Is Null Then 'ALL tables' Else 'The ' + @tableName + ' table' End + ' will be defragged;

    We' + Case When Exists(Select Top 1 * From dbo.dba_indexDefragStatus Where defragDate Is Null)

    And @forceRescan <> 1 Then ' WILL NOT' Else ' WILL' End + ' be rescanning indexes;

    The scan will be performed in ' + @scanMode + ' mode;

    You want to limit defrags to indexes with' + Case When @maxPageCount Is Null Then ' more than '

    + Cast(@minPageCount As varchar(10)) Else

    ' between ' + Cast(@minPageCount As varchar(10))

    + ' and ' + Cast(@maxPageCount As varchar(10)) End + ' pages;

    Indexes will be defragged' + Case When @editionCheck = 0 Or @onlineRebuild = 0 Then ' OFFLINE;' Else ' ONLINE;' End + '

    Indexes will be sorted in' + Case When @sortInTempDB = 0 Then ' the DATABASE' Else ' TEMPDB;' End + '

    Defrag operations will utilize ' + Case When @editionCheck = 0 Or @maxDopRestriction Is Null

    Then 'system defaults for processors;'

    Else Cast(@maxDopRestriction As varchar(2)) + ' processors;' End + '

    You' + Case When @printCommands = 1 Then ' DO' Else ' DO NOT' End + ' want to print the ALTER INDEX commands;

    You' + Case When @printFragmentation = 1 Then ' DO' Else ' DO NOT' End + ' want to output fragmentation levels;

    You want to wait ' + @defragDelay + ' (hh:mm:ss) between defragging indexes;

    You want to run in' + Case When @debugMode = 1 Then ' DEBUG' Else ' SILENT' End + ' mode.';

    RaisError(@debugMessage, 0, 42) With NoWait;

    End;

    If @debugMode = 1 RaisError('Grabbing a list of our databases...', 0, 42) With NoWait;

    /* Retrieve the list of databases to investigate */

    Insert Into #databaseList

    Select database_id

    , name

    , 0 -- not scanned yet for fragmentation

    From sys.databases

    Where name = IsNull(@database, name)

    And [name] Not In ('master', 'tempdb')-- exclude system databases

    And [name] Not In (SELECT Item COLLATE DATABASE_DEFAULT FROM [PROD-FORDIA].dbo.DelimitedSplit8K(@databaseExclude, ',') WHERE Item IS NOT NULL)

    And [state] = 0 -- state must be ONLINE

    And is_read_only = 0; -- cannot be read_only

    /* Check to see if we have indexes in need of defrag; otherwise, re-scan the database(s) */

    If Not Exists(Select Top 1 * From dbo.dba_indexDefragStatus Where defragDate Is Null)

    Or @forceRescan = 1

    Begin

    /* Truncate our list of indexes to prepare for a new scan */

    Truncate Table dbo.dba_indexDefragStatus;

    If @debugMode = 1 RaisError('Looping through our list of databases and checking for fragmentation...', 0, 42) With NoWait;

    /* Loop through our list of databases */

    While (Select Count(*) From #databaseList Where scanStatus = 0) > 0

    Begin

    Select Top 1 @databaseID = databaseID

    From #databaseList

    Where scanStatus = 0;

    Select @debugMessage = ' working on ' + DB_Name(@databaseID) + '...';

    If @debugMode = 1

    RaisError(@debugMessage, 0, 42) With NoWait;

    /* Determine which indexes to defrag using our user-defined parameters */

    Insert Into dbo.dba_indexDefragStatus

    (

    databaseID

    , databaseName

    , objectID

    , indexID

    , partitionNumber

    , fragmentation

    , page_count

    , range_scan_count

    , scanDate

    )

    Select

    ps.database_id As 'databaseID'

    , QuoteName(DB_Name(ps.database_id)) As 'databaseName'

    , ps.object_id As 'objectID'

    , ps.index_id As 'indexID'

    , ps.partition_number As 'partitionNumber'

    , Sum(ps.avg_fragmentation_in_percent) As 'fragmentation'

    , Sum(ps.page_count) As 'page_count'

    , os.range_scan_count

    , GetDate() As 'scanDate'

    From sys.dm_db_index_physical_stats(@databaseID, Object_Id(@tableName), Null , Null, @scanMode) As ps

    Join sys.dm_db_index_operational_stats(@databaseID, Object_Id(@tableName), Null , Null) as os

    On ps.database_id = os.database_id

    And ps.object_id = os.object_id

    and ps.index_id = os.index_id

    And ps.partition_number = os.partition_number

    Where avg_fragmentation_in_percent >= @minFragmentation

    And ps.index_id > 0 -- ignore heaps

    And ps.page_count > @minPageCount

    And ps.index_level = 0 -- leaf-level nodes only, supports @scanMode

    Group By ps.database_id

    , QuoteName(DB_Name(ps.database_id))

    , ps.object_id

    , ps.index_id

    , ps.partition_number

    , os.range_scan_count

    Option (MaxDop 2);

    /* Do we want to exclude right-most populated partition of our partitioned indexes? */

    If @excludeMaxPartition = 1

    Begin

    Set @excludeMaxPartitionSQL = '

    Select ' + Cast(@databaseID As varchar(10)) + ' As [databaseID]

    , [object_id]

    , index_id

    , Max(partition_number) As [maxPartition]

    From ' + DB_Name(@databaseID) + '.sys.partitions

    Where partition_number > 1

    And [rows] > 0

    Group By object_id

    , index_id;';

    Insert Into #maxPartitionList

    Execute sp_executesql @excludeMaxPartitionSQL;

    End;

    /* Keep track of which databases have already been scanned */

    Update #databaseList

    Set scanStatus = 1

    Where databaseID = @databaseID;

    End

    /* We don't want to defrag the right-most populated partition, so

    delete any records for partitioned indexes where partition = Max(partition) */

    If @excludeMaxPartition = 1

    Begin

    Delete ids

    From dbo.dba_indexDefragStatus As ids

    Join #maxPartitionList As mpl

    On ids.databaseID = mpl.databaseID

    And ids.objectID = mpl.objectID

    And ids.indexID = mpl.indexID

    And ids.partitionNumber = mpl.maxPartition;

    End;

    /* Update our exclusion mask for any index that has a restriction on the days it can be defragged */

    Update ids

    Set ids.exclusionMask = ide.exclusionMask

    From dbo.dba_indexDefragStatus As ids

    Join dbo.dba_indexDefragExclusion As ide

    On ids.databaseID = ide.databaseID

    And ids.objectID = ide.objectID

    And ids.indexID = ide.indexID;

    End

    Select @debugMessage = 'Looping through our list... there are ' + Cast(Count(*) As varchar(10)) + ' indexes to defrag!'

    From dbo.dba_indexDefragStatus

    Where defragDate Is Null

    And page_count Between @minPageCount And IsNull(@maxPageCount, page_count);

    If @debugMode = 1 RaisError(@debugMessage, 0, 42) With NoWait;

    /* Begin our loop for defragging */

    While (Select Count(*)

    From dbo.dba_indexDefragStatus

    Where (

    (@executeSQL = 1 And defragDate Is Null)

    Or (@executeSQL = 0 And defragDate Is Null And printStatus = 0)

    )

    And exclusionMask & Power(2, DatePart(weekday, GetDate())-1) = 0

    And page_count Between @minPageCount And IsNull(@maxPageCount, page_count)) > 0

    Begin

    /* Check to see if we need to exit our loop because of our time limit */

    If IsNull(@endDateTime, GetDate()) < GetDate()

    Begin

    RaisError('Our time limit has been exceeded!', 11, 42) With NoWait;

    End;

    If @debugMode = 1 RaisError(' Picking an index to beat into shape...', 0, 42) With NoWait;

    /* Grab the index with the highest priority, based on the values submitted;

    Look at the exclusion mask to ensure it can be defragged today */

    Set @getIndexSQL = N'

    Select Top 1

    @objectID_Out = objectID

    , @indexID_Out = indexID

    , @databaseID_Out = databaseID

    , @databaseName_Out = databaseName

    , @fragmentation_Out = fragmentation

    , @partitionNumber_Out = partitionNumber

    , @pageCount_Out = page_count

    From dbo.dba_indexDefragStatus

    Where defragDate Is Null '

    + Case When @executeSQL = 0 Then 'And printStatus = 0' Else '' End + '

    And exclusionMask & Power(2, DatePart(weekday, GetDate())-1) = 0

    And page_count Between @p_minPageCount and IsNull(@p_maxPageCount, page_count)

    Order By + ' + @defragOrderColumn + ' ' + @defragSortOrder;

    Set @getIndexSQL_Param = N'@objectID_Out int OutPut

    , @indexID_Out int OutPut

    , @databaseID_Out int OutPut

    , @databaseName_Out nvarchar(128) OutPut

    , @fragmentation_Out int OutPut

    , @partitionNumber_Out int OutPut

    , @pageCount_Out int OutPut

    , @p_minPageCount int

    , @p_maxPageCount int';

    Execute sp_executesql @getIndexSQL

    , @getIndexSQL_Param

    , @p_minPageCount = @minPageCount

    , @p_maxPageCount = @maxPageCount

    , @objectID_Out = @objectID OutPut

    , @indexID_Out = @indexID OutPut

    , @databaseID_Out = @databaseID OutPut

    , @databaseName_Out = @databaseName OutPut

    , @fragmentation_Out = @fragmentation OutPut

    , @partitionNumber_Out = @partitionNumber OutPut

    , @pageCount_Out = @pageCount OutPut;

    If @debugMode = 1 RaisError(' Looking up the specifics for our index...', 0, 42) With NoWait;

    /* Look up index information */

    Select @updateSQL = N'Update ids

    Set schemaName = QuoteName(s.name)

    , objectName = QuoteName(o.name)

    , indexName = QuoteName(i.name)

    From dbo.dba_indexDefragStatus As ids

    Inner Join ' + @databaseName + '.sys.objects As o

    On ids.objectID = o.object_id

    Inner Join ' + @databaseName + '.sys.indexes As i

    On o.object_id = i.object_id

    And ids.indexID = i.index_id

    Inner Join ' + @databaseName + '.sys.schemas As s

    On o.schema_id = s.schema_id

    Where o.object_id = ' + Cast(@objectID As varchar(10)) + '

    And i.index_id = ' + Cast(@indexID As varchar(10)) + '

    And i.type > 0

    And ids.databaseID = ' + Cast(@databaseID As varchar(10));

    Execute sp_executesql @updateSQL;

    /* Grab our object names */

    Select @objectName = objectName

    , @schemaName = schemaName

    , @indexName = indexName

    From dbo.dba_indexDefragStatus

    Where objectID = @objectID

    And indexID = @indexID

    And databaseID = @databaseID;

    If @debugMode = 1 RaisError(' Grabbing the partition count...', 0, 42) With NoWait;

    /* Determine if the index is partitioned */

    Select @partitionSQL = 'Select @partitionCount_OUT = Count(*)

    From ' + @databaseName + '.sys.partitions

    Where object_id = ' + Cast(@objectID As varchar(10)) + '

    And index_id = ' + Cast(@indexID As varchar(10)) + ';'

    , @partitionSQL_Param = '@partitionCount_OUT int OutPut';

    Execute sp_executesql @partitionSQL, @partitionSQL_Param, @partitionCount_OUT = @partitionCount OutPut;

    If @debugMode = 1 RaisError(' Seeing if there are any LOBs to be handled...', 0, 42) With NoWait;

    /* Determine if the table contains LOBs */

    Select @LOB_SQL = ' Select @containsLOB_OUT = Count(*)

    From ' + @databaseName + '.sys.columns With (NoLock)

    Where [object_id] = ' + Cast(@objectID As varchar(10)) + '

    And (system_type_id In (34, 35, 99)

    Or max_length = -1);'

    /* system_type_id --> 34 = image, 35 = text, 99 = ntext

    max_length = -1 --> varbinary(max), varchar(max), nvarchar(max), xml */

    , @LOB_SQL_Param = '@containsLOB_OUT int OutPut';

    Execute sp_executesql @LOB_SQL, @LOB_SQL_Param, @containsLOB_OUT = @containsLOB OutPut;

    If @debugMode = 1 RaisError(' Checking for indexes that do not allow page locks...', 0, 42) With NoWait;

    /* Determine if page locks are allowed; for those indexes, we need to always rebuild */

    Select @allowPageLockSQL = 'Select @allowPageLocks_OUT = Count(*)

    From ' + @databaseName + '.sys.indexes

    Where object_id = ' + Cast(@objectID As varchar(10)) + '

    And index_id = ' + Cast(@indexID As varchar(10)) + '

    And Allow_Page_Locks = 0;'

    , @allowPageLockSQL_Param = '@allowPageLocks_OUT int OutPut';

    Execute sp_executesql @allowPageLockSQL, @allowPageLockSQL_Param, @allowPageLocks_OUT = @allowPageLocks OutPut;

    If @debugMode = 1 RaisError(' Building our SQL statements...', 0, 42) With NoWait;

    /* If there's not a lot of fragmentation, or if we have a LOB, we should reorganize */

    If (@fragmentation < @rebuildThreshold Or @containsLOB >= 1 Or @partitionCount > 1)

    And @allowPageLocks = 0

    Begin

    Set @sqlCommand = N'Alter Index ' + @indexName + N' On ' + @databaseName + N'.'

    + @schemaName + N'.' + @objectName + N' ReOrganize';

    /* If our index is partitioned, we should always reorganize */

    If @partitionCount > 1

    Set @sqlCommand = @sqlCommand + N' Partition = '

    + Cast(@partitionNumber As nvarchar(10));

    End

    /* If the index is heavily fragmented and doesn't contain any partitions or LOB's,

    or if the index does not allow page locks, rebuild it */

    Else If (@fragmentation >= @rebuildThreshold Or @allowPageLocks <> 0)

    And IsNull(@containsLOB, 0) != 1 And @partitionCount <= 1

    Begin

    /* Set online rebuild options; requires Enterprise Edition */

    If @onlineRebuild = 1 And @editionCheck = 1

    Set @rebuildCommand = N' Rebuild With (Online = On';

    Else

    Set @rebuildCommand = N' Rebuild With (Online = Off';

    /* Set sort operation preferences */

    If @sortInTempDB = 1

    Set @rebuildCommand = @rebuildCommand + N', Sort_In_TempDB = On';

    Else

    Set @rebuildCommand = @rebuildCommand + N', Sort_In_TempDB = Off';

    /* Set processor restriction options; requires Enterprise Edition */

    If @maxDopRestriction Is Not Null And @editionCheck = 1

    Set @rebuildCommand = @rebuildCommand + N', MaxDop = ' + Cast(@maxDopRestriction As varchar(2)) + N')';

    Else

    Set @rebuildCommand = @rebuildCommand + N')';

    Set @sqlCommand = N'Alter Index ' + @indexName + N' On ' + @databaseName + N'.'

    + @schemaName + N'.' + @objectName + @rebuildCommand;

    End

    Else

    /* Print an error message if any indexes happen to not meet the criteria above */

    If @printCommands = 1 Or @debugMode = 1

    RaisError('We are unable to defrag this index.', 0, 42) With NoWait;

    /* Are we executing the SQL? If so, do it */

    If @executeSQL = 1

    Begin

    Set @debugMessage = 'Executing: ' + @sqlCommand;

    /* Print the commands we're executing if specified to do so */

    If @printCommands = 1 Or @debugMode = 1

    RaisError(@debugMessage, 0, 42) With NoWait;

    /* Grab the time for logging purposes */

    Set @dateTimeStart = GetDate();

    /* Log our actions */

    Insert Into dbo.dba_indexDefragLog

    (

    databaseID

    , databaseName

    , objectID

    , objectName

    , indexID

    , indexName

    , partitionNumber

    , fragmentation

    , page_count

    , dateTimeStart

    , sqlStatement

    , schemaName

    )

    Select

    @databaseID

    , @databaseName

    , @objectID

    , @objectName

    , @indexID

    , @indexName

    , @partitionNumber

    , @fragmentation

    , @pageCount

    , @dateTimeStart

    , @sqlCommand

    , @schemaName;

    Set @indexDefrag_id = Scope_Identity();

    /* Wrap our execution attempt in a try/catch and log any errors that occur */

    Begin Try

    /* Execute our defrag! */

    Execute sp_executesql @sqlCommand;

    Set @dateTimeEnd = GetDate();

    /* Update our log with our completion time */

    Update dbo.dba_indexDefragLog

    Set dateTimeEnd = @dateTimeEnd

    , durationSeconds = DateDiff(second, @dateTimeStart, @dateTimeEnd)

    Where indexDefrag_id = @indexDefrag_id;

    End Try

    Begin Catch

    /* Update our log with our error message */

    Update dbo.dba_indexDefragLog

    Set dateTimeEnd = GetDate()

    , durationSeconds = -1

    , errorMessage = Error_Message()

    Where indexDefrag_id = @indexDefrag_id;

    If @debugMode = 1

    RaisError(' An error has occurred executing this command! Please review the dba_indexDefragLog table for details.'

    , 0, 42) With NoWait;

    End Catch

    /* Just a little breather for the server */

    WaitFor Delay @defragDelay;

    Update dbo.dba_indexDefragStatus

    Set defragDate = GetDate()

    , printStatus = 1

    Where databaseID = @databaseID

    And objectID = @objectID

    And indexID = @indexID

    And partitionNumber = @partitionNumber;

    End

    Else

    /* Looks like we're not executing, just printing the commands */

    Begin

    If @debugMode = 1 RaisError(' Printing SQL statements...', 0, 42) With NoWait;

    If @printCommands = 1 Or @debugMode = 1

    Print IsNull(@sqlCommand, 'error!');

    Update dbo.dba_indexDefragStatus

    Set printStatus = 1

    Where databaseID = @databaseID

    And objectID = @objectID

    And indexID = @indexID

    And partitionNumber = @partitionNumber;

    End

    End

    /* Do we want to output our fragmentation results? */

    If @printFragmentation = 1

    Begin

    If @debugMode = 1 RaisError(' Displaying a summary of our action...', 0, 42) With NoWait;

    Select databaseID

    , databaseName

    , objectID

    , objectName

    , indexID

    , indexName

    , partitionNumber

    , fragmentation

    , page_count

    , range_scan_count

    From dbo.dba_indexDefragStatus

    Where defragDate >= @startDateTime

    Order By defragDate;

    End;

    End Try

    Begin Catch

    Set @debugMessage = Error_Message() + ' (Line Number: ' + Cast(Error_Line() As varchar(10)) + ')';

    Print @debugMessage;

    End Catch;

    /* When everything is said and done, make sure to get rid of our temp table */

    Drop Table #databaseList;

    Drop Table #processor;

    Drop Table #maxPartitionList;

    If @debugMode = 1 RaisError('DONE! Thank you for taking care of your indexes! :)', 0, 42) With NoWait;

    Set NoCount Off;

    Return 0

    End

    Job :

    USE [msdb]

    GO

    /****** Objet : Job [Index defrag smarter] Date de génération du script : 10/13/2011 11:12:28 ******/

    BEGIN TRANSACTION

    DECLARE @ReturnCode INT

    SELECT @ReturnCode = 0

    /****** Objet : JobCategory [Database Maintenance] Date de génération du script : 10/13/2011 11:12:28 ******/

    IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)

    BEGIN

    EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'

    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

    END

    DECLARE @jobId BINARY(16)

    EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Index defrag smarter',

    @enabled=1,

    @notify_level_eventlog=0,

    @notify_level_email=0,

    @notify_level_netsend=0,

    @notify_level_page=0,

    @delete_level=0,

    @description=N'Reindex / reorganize selectivement selon le % de fragmentation. Un historique est gardé pour un jour éliminer du travail moins utile.',

    @category_name=N'Database Maintenance',

    @owner_login_name=N'sa', @job_id = @jobId OUTPUT

    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

    /****** Objet : Step [Defrag all user dbs] Date de génération du script : 10/13/2011 11:12:29 ******/

    EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Defrag all user dbs',

    @step_id=1,

    @cmdexec_success_code=0,

    @on_success_action=1,

    @on_success_step_id=0,

    @on_fail_action=2,

    @on_fail_step_id=0,

    @retry_attempts=0,

    @retry_interval=0,

    @os_run_priority=0, @subsystem=N'TSQL',

    @command=N'exec dbo.dba_indexDefrag_sp @databaseExclude = ''PROD-FORDIA_Upgrade_Nav_Client''',

    @database_name=N'master',

    @flags=20

    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

    EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1

    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

    EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'defrag all user dbs',

    @enabled=1,

    @freq_type=4,

    @freq_interval=1,

    @freq_subday_type=1,

    @freq_subday_interval=0,

    @freq_relative_interval=0,

    @freq_recurrence_factor=0,

    @active_start_date=20101130,

    @active_end_date=99991231,

    @active_start_time=33000,

    @active_end_time=235959

    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

    EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'

    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

    COMMIT TRANSACTION

    GOTO EndSave

    QuitWithRollback:

    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION

    EndSave:

  • Wow, looks great! Comments are very usefull as well!

    I'll tweak it a bit today and will try tomorrow.

    Thanks once more!

Viewing 15 posts - 1 through 15 (of 19 total)

You must be logged in to reply to this topic. Login to reply