Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Re-index all databases

Author  Topic 

fralo
Posting Yak Master

161 Posts

Posted - 2011-04-17 : 16:06:26
Hi all,

I'm researching how to re-index all databases. I have found two suggestions. First, this one theoretically handles every table for every database which would be very convenient if it works.

DECLARE @SQL varchar(8000) -- if you use nvarchar for whack table names, change this to 4000
SET @SQL = ''

SELECT @SQL = @SQL + 'EXEC ' + NAME + '..sp_MSforeachtable @command1=''DBCC DBREINDEX (''''*'''')'', @replacechar=''*''' + Char(13)
FROM MASTER..Sysdatabases where NAME !='tempdb'

PRINT @SQL
EXEC (@SQL)

The second one uses a looping mechanism and runs for only one database. I would have to re-run for every DB.

But my major problem right now is just trying to run this statement prior to doing the reindex.

select * from sys.tables.

It won't recognize sys.tables!!!

Any idea of what's wrong? Any suggestions as well on reindexing, and whether the code I have above should work.

Thanks so much for your help.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-17 : 16:39:00
Use this to reindex: http://weblogs.sqlteam.com/tarad/archive/2009/11/03/DefragmentingRebuilding-Indexes-in-SQL-Server-2005-and-2008Again.aspx

And use this to loop through it: http://weblogs.sqlteam.com/tarad/archive/2007/11/28/60417.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2011-04-17 : 16:49:48
Thank you so much Tara! So all I have to do is just run the code in your second link above and it will reindex all tables within all databases on the server?

Would I have to change anything within it to fit my particular needs, or is it ready to go as is?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-17 : 16:53:36
You'll need to set the parameters accordingly, such as minimum fragmentation should be set to 0 as well as minimum row count.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2011-04-17 : 16:58:56
Oh, I just realized that I need to run both of your links, don't I? I originally thought you were giving me a choice between the two. The first one compiles the proc on the database, and the second utilizes it.

I greatly appreciate your help Tara. Thanks a lot.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-17 : 17:02:06
Yes, you need both.

You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Jahanzaib
Posting Yak Master

115 Posts

Posted - 2011-04-18 : 16:52:59
For Automate Index Rebuilding manual or schedule in a agent job

http://aureus-salah.com/2010/08/05/sql-server-automate-index-rebuilding/



Regards,

Syed Jahanzaib Bin Hassan
MCTS,MCITP,OCA,OCP,OCE,SCJP,IBMCDBA

My Blog
www.aureus-salah.com
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-18 : 17:05:02
Do not use code like that in an Agent job. That code needs a lot of work.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -