SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Intelligent Index Reorganize and Rebuild Script - v1.0


Intelligent Index Reorganize and Rebuild Script - v1.0

Author
Message
Tim Parker
Tim Parker
Right there with Babe
Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)

Group: General Forum Members
Points: 778 Visits: 460
Comments posted to this topic are about the item Intelligent Index Reorganize and Rebuild Script - v1.0

:-D
Brett Phipps
Brett Phipps
Mr or Mrs. 500
Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)

Group: General Forum Members
Points: 529 Visits: 230
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?
wesleycrockett
wesleycrockett
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 41
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
Brett Phipps
Brett Phipps
Mr or Mrs. 500
Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)

Group: General Forum Members
Points: 529 Visits: 230
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


charlesafarr
charlesafarr
SSC-Enthusiastic
SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)

Group: General Forum Members
Points: 114 Visits: 240
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)
Tim Parker
Tim Parker
Right there with Babe
Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)

Group: General Forum Members
Points: 778 Visits: 460
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.

:-D
Attachments
ReorgRebuildIndex.txt (79 views, 12.00 KB)
JRoughgarden
JRoughgarden
SSC-Addicted
SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)

Group: General Forum Members
Points: 453 Visits: 195
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



Attachments
usp_ReorgRebuildIndexOneDB.txt (52 views, 6.00 KB)
TUellner
TUellner
Mr or Mrs. 500
Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)

Group: General Forum Members
Points: 586 Visits: 1377
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 (
SELECT DISTINCT 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 (
SELECT DISTINCT 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
vinaikumargandla
vinaikumargandla
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 64
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search