Link to home
Start Free TrialLog in
Avatar of habbotha
habbotha

asked on

How do I re index tables in SQL - Inherrited database

Hi there,
I have inherrited a database design with all the Tables, Procedures, Jobs etc.
A suggestion has been made that I re index the tables as some of the data content has changed.  
I am a novice to SQL - How do I re Index tables?
Thanking you
Heather
Avatar of htowntechy
htowntechy

--Script to automatically reindex all tables in a database

USE DatabaseName --Enter the name of the database you want to reindex

DECLARE @TableName varchar(255)

DECLARE TableCursor CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table'

OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
DBCC DBREINDEX(@TableName,' ',90)
FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor

DEALLOCATE TableCursor

------------------------------------------------------------------------------------------------------------------------------------------------
If you do a reorganization on a table with a clustered index, any non-clustered indexes on that same table will automatically be rebuilt.

The script will automatically reindex every index in every table of any database you select, and provide a fill factor of 90%. You can substitute any number appropriate for the fill factor in the above script.

When DBCC DBREINDEX is used to rebuild indexes, keep in mind that as the indexes on a specific table are being rebuilt, that the table becomes unavailable for use by your users.
------------------------------------------------------------------------------------------------------------------------------------------------
http://www.sql-server-performance.com/tips/rebuilding_indexes_p1.aspx
hello

try this

EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
GO
EXEC sp_updatestats
GO
Avatar of habbotha

ASKER

thanks  guys.  But where about in the SQL database do i put the scripts?
Avatar of Anthony Perkins
>>A suggestion has been made that I re index the tables as some of the data content has changed. <<
You may not know this, but there may not be any need: The index is updated everytime the data changes.  It would be a nightmare otherwise.
are you concerned about the statistics that are maintained on the indexes which exist for your tables?

then they above responses cater for that scenario...

or do you mean that the way you access the tables has changed and you want to add or change indexes?
Hi
The way i access the tables has change and some of the tables have been recreated.
Thanks Heather
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what has changed in the way the data is accessed?

do you have a problem wiyth the performance of some queryies?

can you post examples?
Thanks all for the help - I have gone for the option of reindex the database and the performance is much better.