Intelligent Index Reorganize and Rebuild Script - v1.0

  • Comments posted to this topic are about the item Intelligent Index Reorganize and Rebuild Script - v1.0

    😀

  • I'm running this but I'm getting

    (0 row(s) affected)

    Msg 102, Level 15, State 1, Line 11

    Incorrect syntax near ')'.

    I'm trying to track it down but it's a little complicated, and it's still running so I don't want to mess with it until it's finished. Any ideas on what could cause this?

  • Is there a way to run this against only a single database within the SQL Server? When I ran it, it ran against all 12 databases being hosted by the instance.

    Thank you,

    Wes Crockett

  • I pulled the following code below from the script so I could try to debug my issue. You could use it to build the statements and then just run the ones you want.

    Use YOURDATABASE

    GO

    SELECT avg_fragmentation_in_percent,

    CASE WHEN avg_fragmentation_in_percent BETWEEN 5 AND 30

    THEN 'ALTER INDEX [' + name + '] ON ' + (SELECT TOP 1 TABLE_SCHEMA

    FROM INFORMATION_SCHEMA.TABLES

    WHERE TABLE_NAME = OBJECT_NAME(b.[OBJECT_ID])

    AND TABLE_TYPE = 'BASE TABLE')

    + '.[' + OBJECT_NAME(b.[OBJECT_ID]) + '] REORGANIZE ;'

    WHEN avg_fragmentation_in_percent > 30

    THEN 'ALTER INDEX [' + name + '] ON ' + (SELECT TOP 1 TABLE_SCHEMA

    FROM INFORMATION_SCHEMA.TABLES

    WHERE TABLE_NAME = OBJECT_NAME(b.[OBJECT_ID])

    AND TABLE_TYPE = 'BASE TABLE')

    + '.[' + OBJECT_NAME(b.[OBJECT_ID]) + '] REBUILD WITH (FILLFACTOR = 90) ;'

    END AS Index_Statement

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

    JOIN sys.indexes AS b

    ON a.object_id = b.object_id

    AND a.index_id = b.index_id

    WHERE avg_fragmentation_in_percent > 5

    AND index_type_desc <> 'HEAP'

    AND page_count > 640

    ORDER BY avg_fragmentation_in_percent DESC

  • Tim,

    Executing the Stored Proc on 2008R2 gives this error....

    (0 row(s) affected)

    (0 row(s) affected)

    (0 row(s) affected)

    (0 row(s) affected)

    (0 row(s) affected)

    (0 row(s) affected)

    (0 row(s) affected)

    (0 row(s) affected)

    Msg 102, Level 15, State 1, Line 11

    Incorrect syntax near '('.

    (0 row(s) affected)

    (0 row(s) affected)

    (0 row(s) affected)

    (0 row(s) affected)

    (1 row(s) affected)

  • Hi everyone,

    Take a look at the attached file, which has the SQL script in known good formatted state. I'm guessing there may have been an issue with the code formatting in the article versus what I actually have in my SQL script.

    Please try the attached script and let me know if this resolves the issue.

    The attached script name is ReorgRebuildIndex.txt. Of course you will need to change the extension back to a .SQL for it to run in SSMS.

    Tim P.

    😀

  • Hi all,

    I borrowed the guts of what Timothy Parker did and created a version that works on one database only, so there is no outer cursor loop. I also added two output parameters, a try/catch, and renamed some object to suit my conventions.

    Thanks, Timothy

    Jeff Roughgarden

  • Hi Tim,

    I added a check to your code to skip off-line databases. Here is the original section of code:

    --Begin our maintenance tasks.

    DECLARE curDatabase CURSOR FOR

    --This statement filters out system databases and databases with open cursors.

    SELECT NAME

    FROM sys.sysdatabases

    WHERE DBID > 4 AND dbid NOT IN (

    SELECTDISTINCT PRO.dbid

    FROM sys.dm_exec_cursors(0) CURS INNER JOIN sys.sysprocesses PRO

    ON CURS.session_ID = PRO.spid

    WHERE is_open =1

    )

    AND dbid NOT IN (

    SELECT DISTINCT SP.[dbid]

    FROM sys.sysprocesses SP CROSS APPLY sys.dm_exec_sql_text(sql_handle)

    WHERE [TEXT] LIKE 'FETCH%'

    )

    ORDER BY NAME

    OPEN curDatabase

    And here is the code with the added check.

    --Begin our maintenance tasks.

    DECLARE curDatabase CURSOR FOR

    --This statement filters out system databases and databases with open cursors.

    -- 2014-04-08 Tom Uellner: Added check for sys.databases [state] > 0 (database must be online)

    SELECT NAME

    FROM sys.sysdatabases

    WHERE DBID > 4 AND dbid NOT IN (

    SELECTDISTINCT PRO.dbid

    FROM sys.dm_exec_cursors(0) CURS INNER JOIN sys.sysprocesses PRO

    ON CURS.session_ID = PRO.spid

    WHERE is_open =1

    )

    AND dbid NOT IN (

    SELECT DISTINCT SP.[dbid]

    FROM sys.sysprocesses SP CROSS APPLY sys.dm_exec_sql_text(sql_handle)

    WHERE [TEXT] LIKE 'FETCH%'

    )

    AND dbid NOT IN (

    SELECT database_id

    FROM sys.databases

    WHERE [state] > 0

    )

    ORDER BY NAME

    OPEN curDatabase

    -Tom

  • Can anyone provide me the error free code to reorganize and rebuild indexes depending on frag %

    to create job.

    I am getting errors when i ran the provided code:

    Msg 154, Level 15, State 1, Procedure ReorgRebuildIndex, Line 76

    a USE database statement is not allowed in a procedure, function or trigger.

    Msg 156, Level 15, State 1, Procedure ReorgRebuildIndex, Line 79

    Incorrect syntax near the keyword 'ALTER'.

    Msg 102, Level 15, State 1, Procedure ReorgRebuildIndex, Line 79

    Incorrect syntax near ''.

    Msg 102, Level 15, State 1, Procedure ReorgRebuildIndex, Line 80

    Incorrect syntax near 'BASE'.

    Msg 102, Level 15, State 1, Procedure ReorgRebuildIndex, Line 83

    Incorrect syntax near ''.

    Msg 102, Level 15, State 1, Procedure ReorgRebuildIndex, Line 84

    Incorrect syntax near 'BASE'.

    Msg 319, Level 15, State 1, Procedure ReorgRebuildIndex, Line 85

    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

    Msg 154, Level 15, State 1, Procedure ReorgRebuildIndex, Line 109

    a USE database statement is not allowed in a procedure, function or trigger.

    Msg 105, Level 15, State 1, Procedure ReorgRebuildIndex, Line 110

    Thanks

Viewing 9 posts - 1 through 8 (of 8 total)

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