dbTalk Databases Forums  

DBCC DBREINDEX

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss DBCC DBREINDEX in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dave Mortenson
 
Posts: n/a

Default DBCC DBREINDEX - 07-20-2004 , 02:27 PM






Can anyone confirm this for me. If I were to run this script below. Will it
reindex all tables indexes at one time as it goes through the cursor. Or
will it reindex one table indexes at a time as it goes through the cursor.
Basically will it pause each time the DBCC command is executed?
If the first part is true where it executes all at once then what is a good
way to only do one at a time? Can you pause between executions till each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name = 'Practice' OR name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor



Reply With Quote
  #2  
Old   
Andrew J. Kelly
 
Posts: n/a

Default Re: DBCC DBREINDEX - 07-20-2004 , 03:22 PM






Dave,

If you execute DBCC DBREINDEX with only the table specified it will rebuild
ALL indexes on that table. This happens all in one big transaction for each
table. You can specify a single index to be rebuilt but as you have it here
it will rebuild all of them on each table in the cursor. You can pause in
between each table by placing a WAITFOR DELAY after the DBREINDEX command.
You really should look at using one of the information schema views as the
source of the cursor instead of the system tables directly.
INFORMATION_SCHEMA.TABLES



--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote

Quote:
Can anyone confirm this for me. If I were to run this script below. Will
it
reindex all tables indexes at one time as it goes through the cursor. Or
will it reindex one table indexes at a time as it goes through the cursor.
Basically will it pause each time the DBCC command is executed?
If the first part is true where it executes all at once then what is a
good
way to only do one at a time? Can you pause between executions till each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name = 'Practice' OR
name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor





Reply With Quote
  #3  
Old   
Dave Mortenson
 
Posts: n/a

Default Re: DBCC DBREINDEX - 07-20-2004 , 03:52 PM



OK Thanks for the information. I will use INFORMATION_SCHEMA.TABLES. I do
understand if I only specify the table name it will reindex all indexes in
table thius is what I am after. Although what I was wondering is if I use
this script and my query returns 15 table names lets say will it execute the
dbcc command on all 15 tables at the same time or will it execute the dbcc
command one at a time as it sequencially goes through the cursor. I only
want to reindex one table and all it's indexes at a time. Can you tell me if
this is true?


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote

Quote:
Dave,

If you execute DBCC DBREINDEX with only the table specified it will
rebuild
ALL indexes on that table. This happens all in one big transaction for
each
table. You can specify a single index to be rebuilt but as you have it
here
it will rebuild all of them on each table in the cursor. You can pause in
between each table by placing a WAITFOR DELAY after the DBREINDEX command.
You really should look at using one of the information schema views as the
source of the cursor instead of the system tables directly.
INFORMATION_SCHEMA.TABLES



--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:eSfnM%23obEHA.1732 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Can anyone confirm this for me. If I were to run this script below. Will
it
reindex all tables indexes at one time as it goes through the cursor. Or
will it reindex one table indexes at a time as it goes through the
cursor.
Basically will it pause each time the DBCC command is executed?
If the first part is true where it executes all at once then what is a
good
way to only do one at a time? Can you pause between executions till each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name = 'Practice' OR
name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor







Reply With Quote
  #4  
Old   
Andrew J. Kelly
 
Posts: n/a

Default Re: DBCC DBREINDEX - 07-20-2004 , 04:28 PM



Dave,

The whole point of a Cursor is to loop thru the result set one row at a
time. The cursor will only return 1 table name with each loop and since
your specifying the table name via the variable that gets filled in thru the
cursor, DBCC DBREINDEX will only work on that individual table at that time.
As a matter of fact there is no way to reindex all tables or indexes all at
once with one command. Hope that helps.

--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote

Quote:
OK Thanks for the information. I will use INFORMATION_SCHEMA.TABLES. I do
understand if I only specify the table name it will reindex all indexes in
table thius is what I am after. Although what I was wondering is if I use
this script and my query returns 15 table names lets say will it execute
the
dbcc command on all 15 tables at the same time or will it execute the dbcc
command one at a time as it sequencially goes through the cursor. I only
want to reindex one table and all it's indexes at a time. Can you tell me
if
this is true?


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote in message
news:eAa0NdpbEHA.1004 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
Dave,

If you execute DBCC DBREINDEX with only the table specified it will
rebuild
ALL indexes on that table. This happens all in one big transaction for
each
table. You can specify a single index to be rebuilt but as you have it
here
it will rebuild all of them on each table in the cursor. You can pause
in
between each table by placing a WAITFOR DELAY after the DBREINDEX
command.
You really should look at using one of the information schema views as
the
source of the cursor instead of the system tables directly.
INFORMATION_SCHEMA.TABLES



--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:eSfnM%23obEHA.1732 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Can anyone confirm this for me. If I were to run this script below.
Will
it
reindex all tables indexes at one time as it goes through the cursor.
Or
will it reindex one table indexes at a time as it goes through the
cursor.
Basically will it pause each time the DBCC command is executed?
If the first part is true where it executes all at once then what is a
good
way to only do one at a time? Can you pause between executions till
each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name = 'Practice'
OR
name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor









Reply With Quote
  #5  
Old   
Dave Mortenson
 
Posts: n/a

Default Re: DBCC DBREINDEX - 07-20-2004 , 05:19 PM



This is true. What I was trying to get at was in a query analyzer window it
takes only a few seconds to query and return all my tables. Will the cursor
also loop through all the tables in a few seconds and issue multiple dbcc
commands all at once. Or will it loop once issue the dbcc dbreindex command
wait for it to finish then loop again to the next and so on this way it wont
issue multiple commands all at once? This is what I was wondering if I
needed to pause in between loops.

THanks Andrew.


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote

Quote:
Dave,

The whole point of a Cursor is to loop thru the result set one row at a
time. The cursor will only return 1 table name with each loop and since
your specifying the table name via the variable that gets filled in thru
the
cursor, DBCC DBREINDEX will only work on that individual table at that
time.
As a matter of fact there is no way to reindex all tables or indexes all
at
once with one command. Hope that helps.

--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:%23N3F7tpbEHA.3016 (AT) tk2msftngp13 (DOT) phx.gbl...
OK Thanks for the information. I will use INFORMATION_SCHEMA.TABLES. I
do
understand if I only specify the table name it will reindex all indexes
in
table thius is what I am after. Although what I was wondering is if I
use
this script and my query returns 15 table names lets say will it execute
the
dbcc command on all 15 tables at the same time or will it execute the
dbcc
command one at a time as it sequencially goes through the cursor. I only
want to reindex one table and all it's indexes at a time. Can you tell
me
if
this is true?


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote in message
news:eAa0NdpbEHA.1004 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
Dave,

If you execute DBCC DBREINDEX with only the table specified it will
rebuild
ALL indexes on that table. This happens all in one big transaction for
each
table. You can specify a single index to be rebuilt but as you have
it
here
it will rebuild all of them on each table in the cursor. You can
pause
in
between each table by placing a WAITFOR DELAY after the DBREINDEX
command.
You really should look at using one of the information schema views as
the
source of the cursor instead of the system tables directly.
INFORMATION_SCHEMA.TABLES



--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:eSfnM%23obEHA.1732 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Can anyone confirm this for me. If I were to run this script below.
Will
it
reindex all tables indexes at one time as it goes through the
cursor.
Or
will it reindex one table indexes at a time as it goes through the
cursor.
Basically will it pause each time the DBCC command is executed?
If the first part is true where it executes all at once then what is
a
good
way to only do one at a time? Can you pause between executions till
each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name = 'Practice'
OR
name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor











Reply With Quote
  #6  
Old   
Andrew J. Kelly
 
Posts: n/a

Default Re: DBCC DBREINDEX - 07-20-2004 , 09:24 PM



OK, I see where you are going now. TSQL is a procedural language and can
not by itself run multiple paths or commands per say. Before the next
command can be executed the one before it must finish completely. This
includes commands inside a cursor. The next fetch operation will not occur
before the previous DBCC command is completed. So in that regard you don't
need a WAITFOR. Sometimes people want the system to take a breather in
between tables since this can be a very intensive operation.
Now this does not mean multiple users can not be doing stuff at the same
time. Each user has it's own threads that run independently of each other.
But within a single batch of TSQL it is essentially procedural.


--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote

Quote:
This is true. What I was trying to get at was in a query analyzer window
it
takes only a few seconds to query and return all my tables. Will the
cursor
also loop through all the tables in a few seconds and issue multiple dbcc
commands all at once. Or will it loop once issue the dbcc dbreindex
command
wait for it to finish then loop again to the next and so on this way it
wont
issue multiple commands all at once? This is what I was wondering if I
needed to pause in between loops.

THanks Andrew.


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote in message
news:OD7KOCqbEHA.644 (AT) tk2msftngp13 (DOT) phx.gbl...
Dave,

The whole point of a Cursor is to loop thru the result set one row at a
time. The cursor will only return 1 table name with each loop and since
your specifying the table name via the variable that gets filled in thru
the
cursor, DBCC DBREINDEX will only work on that individual table at that
time.
As a matter of fact there is no way to reindex all tables or indexes all
at
once with one command. Hope that helps.

--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:%23N3F7tpbEHA.3016 (AT) tk2msftngp13 (DOT) phx.gbl...
OK Thanks for the information. I will use INFORMATION_SCHEMA.TABLES. I
do
understand if I only specify the table name it will reindex all
indexes
in
table thius is what I am after. Although what I was wondering is if I
use
this script and my query returns 15 table names lets say will it
execute
the
dbcc command on all 15 tables at the same time or will it execute the
dbcc
command one at a time as it sequencially goes through the cursor. I
only
want to reindex one table and all it's indexes at a time. Can you tell
me
if
this is true?


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote in message
news:eAa0NdpbEHA.1004 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
Dave,

If you execute DBCC DBREINDEX with only the table specified it will
rebuild
ALL indexes on that table. This happens all in one big transaction
for
each
table. You can specify a single index to be rebuilt but as you have
it
here
it will rebuild all of them on each table in the cursor. You can
pause
in
between each table by placing a WAITFOR DELAY after the DBREINDEX
command.
You really should look at using one of the information schema views
as
the
source of the cursor instead of the system tables directly.
INFORMATION_SCHEMA.TABLES



--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:eSfnM%23obEHA.1732 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Can anyone confirm this for me. If I were to run this script
below.
Will
it
reindex all tables indexes at one time as it goes through the
cursor.
Or
will it reindex one table indexes at a time as it goes through the
cursor.
Basically will it pause each time the DBCC command is executed?
If the first part is true where it executes all at once then what
is
a
good
way to only do one at a time? Can you pause between executions
till
each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name =
'Practice'
OR
name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor













Reply With Quote
  #7  
Old   
Dave Mortenson
 
Posts: n/a

Default Re: DBCC DBREINDEX - 07-21-2004 , 09:26 AM



Andrew Excellent that is what I wanted to know. Thank you so much. Do you
know of a way to make it print as it completes each execution rather than
all at the end? I do use the print command but like I said it will only
print all at the end.

THanks again I really appreciate the info.

Dave


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote

Quote:
OK, I see where you are going now. TSQL is a procedural language and can
not by itself run multiple paths or commands per say. Before the next
command can be executed the one before it must finish completely. This
includes commands inside a cursor. The next fetch operation will not
occur
before the previous DBCC command is completed. So in that regard you
don't
need a WAITFOR. Sometimes people want the system to take a breather in
between tables since this can be a very intensive operation.
Now this does not mean multiple users can not be doing stuff at the same
time. Each user has it's own threads that run independently of each
other.
But within a single batch of TSQL it is essentially procedural.


--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:%23Io2qeqbEHA.1408 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
This is true. What I was trying to get at was in a query analyzer window
it
takes only a few seconds to query and return all my tables. Will the
cursor
also loop through all the tables in a few seconds and issue multiple
dbcc
commands all at once. Or will it loop once issue the dbcc dbreindex
command
wait for it to finish then loop again to the next and so on this way it
wont
issue multiple commands all at once? This is what I was wondering if I
needed to pause in between loops.

THanks Andrew.


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote in message
news:OD7KOCqbEHA.644 (AT) tk2msftngp13 (DOT) phx.gbl...
Dave,

The whole point of a Cursor is to loop thru the result set one row at
a
time. The cursor will only return 1 table name with each loop and
since
your specifying the table name via the variable that gets filled in
thru
the
cursor, DBCC DBREINDEX will only work on that individual table at that
time.
As a matter of fact there is no way to reindex all tables or indexes
all
at
once with one command. Hope that helps.

--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:%23N3F7tpbEHA.3016 (AT) tk2msftngp13 (DOT) phx.gbl...
OK Thanks for the information. I will use INFORMATION_SCHEMA.TABLES.
I
do
understand if I only specify the table name it will reindex all
indexes
in
table thius is what I am after. Although what I was wondering is if
I
use
this script and my query returns 15 table names lets say will it
execute
the
dbcc command on all 15 tables at the same time or will it execute
the
dbcc
command one at a time as it sequencially goes through the cursor. I
only
want to reindex one table and all it's indexes at a time. Can you
tell
me
if
this is true?


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote in message
news:eAa0NdpbEHA.1004 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
Dave,

If you execute DBCC DBREINDEX with only the table specified it
will
rebuild
ALL indexes on that table. This happens all in one big transaction
for
each
table. You can specify a single index to be rebuilt but as you
have
it
here
it will rebuild all of them on each table in the cursor. You can
pause
in
between each table by placing a WAITFOR DELAY after the DBREINDEX
command.
You really should look at using one of the information schema
views
as
the
source of the cursor instead of the system tables directly.
INFORMATION_SCHEMA.TABLES



--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:eSfnM%23obEHA.1732 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Can anyone confirm this for me. If I were to run this script
below.
Will
it
reindex all tables indexes at one time as it goes through the
cursor.
Or
will it reindex one table indexes at a time as it goes through
the
cursor.
Basically will it pause each time the DBCC command is executed?
If the first part is true where it executes all at once then
what
is
a
good
way to only do one at a time? Can you pause between executions
till
each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name =
'Practice'
OR
name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor















Reply With Quote
  #8  
Old   
Joe Horton
 
Posts: n/a

Default Re: DBCC DBREINDEX - 07-21-2004 , 09:46 AM



You could get rid of your cursor by using sp_MSforeachtable

See notes below from web site re: undocumented SP's:
http://www.mssqlcity.com/Articles/Un...000UndocSP.htm
sp_MSforeachtable
Sometimes, you need to perform the same actions for all tables in the
database. You can create cursor for this purpose, or you can also use the
sp_MSforeachtable stored procedure to accomplish the same goal with less
work.

For example, you can use the sp_MSforeachtable stored procedure to rebuild
all the indexes in a database:

EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?')"


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote

Quote:
Can anyone confirm this for me. If I were to run this script below. Will
it
reindex all tables indexes at one time as it goes through the cursor. Or
will it reindex one table indexes at a time as it goes through the cursor.
Basically will it pause each time the DBCC command is executed?
If the first part is true where it executes all at once then what is a
good
way to only do one at a time? Can you pause between executions till each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name = 'Practice' OR
name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor





Reply With Quote
  #9  
Old   
Andrew J. Kelly
 
Posts: n/a

Default Re: DBCC DBREINDEX - 07-21-2004 , 09:53 AM



That is due to the way it buffers the output and there is no easy way to do
what your asking that I know of. It is something that a lot of people ask
about but I haven't seen a good solution.

--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote

Quote:
Andrew Excellent that is what I wanted to know. Thank you so much. Do you
know of a way to make it print as it completes each execution rather than
all at the end? I do use the print command but like I said it will only
print all at the end.

THanks again I really appreciate the info.

Dave


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote in message
news:ORFRinsbEHA.636 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
OK, I see where you are going now. TSQL is a procedural language and
can
not by itself run multiple paths or commands per say. Before the next
command can be executed the one before it must finish completely. This
includes commands inside a cursor. The next fetch operation will not
occur
before the previous DBCC command is completed. So in that regard you
don't
need a WAITFOR. Sometimes people want the system to take a breather in
between tables since this can be a very intensive operation.
Now this does not mean multiple users can not be doing stuff at the same
time. Each user has it's own threads that run independently of each
other.
But within a single batch of TSQL it is essentially procedural.


--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:%23Io2qeqbEHA.1408 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
This is true. What I was trying to get at was in a query analyzer
window
it
takes only a few seconds to query and return all my tables. Will the
cursor
also loop through all the tables in a few seconds and issue multiple
dbcc
commands all at once. Or will it loop once issue the dbcc dbreindex
command
wait for it to finish then loop again to the next and so on this way
it
wont
issue multiple commands all at once? This is what I was wondering if I
needed to pause in between loops.

THanks Andrew.


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote in message
news:OD7KOCqbEHA.644 (AT) tk2msftngp13 (DOT) phx.gbl...
Dave,

The whole point of a Cursor is to loop thru the result set one row
at
a
time. The cursor will only return 1 table name with each loop and
since
your specifying the table name via the variable that gets filled in
thru
the
cursor, DBCC DBREINDEX will only work on that individual table at
that
time.
As a matter of fact there is no way to reindex all tables or indexes
all
at
once with one command. Hope that helps.

--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:%23N3F7tpbEHA.3016 (AT) tk2msftngp13 (DOT) phx.gbl...
OK Thanks for the information. I will use
INFORMATION_SCHEMA.TABLES.
I
do
understand if I only specify the table name it will reindex all
indexes
in
table thius is what I am after. Although what I was wondering is
if
I
use
this script and my query returns 15 table names lets say will it
execute
the
dbcc command on all 15 tables at the same time or will it execute
the
dbcc
command one at a time as it sequencially goes through the cursor.
I
only
want to reindex one table and all it's indexes at a time. Can you
tell
me
if
this is true?


"Andrew J. Kelly" <sqlmvpnooospam (AT) shadhawk (DOT) com> wrote in message
news:eAa0NdpbEHA.1004 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
Dave,

If you execute DBCC DBREINDEX with only the table specified it
will
rebuild
ALL indexes on that table. This happens all in one big
transaction
for
each
table. You can specify a single index to be rebuilt but as you
have
it
here
it will rebuild all of them on each table in the cursor. You
can
pause
in
between each table by placing a WAITFOR DELAY after the
DBREINDEX
command.
You really should look at using one of the information schema
views
as
the
source of the cursor instead of the system tables directly.
INFORMATION_SCHEMA.TABLES



--
Andrew J. Kelly SQL MVP


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:eSfnM%23obEHA.1732 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Can anyone confirm this for me. If I were to run this script
below.
Will
it
reindex all tables indexes at one time as it goes through the
cursor.
Or
will it reindex one table indexes at a time as it goes through
the
cursor.
Basically will it pause each time the DBCC command is
executed?
If the first part is true where it executes all at once then
what
is
a
good
way to only do one at a time? Can you pause between executions
till
each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name =
'Practice'
OR
name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor

















Reply With Quote
  #10  
Old   
Andrew J. Kelly
 
Posts: n/a

Default Re: DBCC DBREINDEX - 07-21-2004 , 09:54 AM



Quote:
You could get rid of your cursor by using sp_MSforeachtable
sp_MSforeachtable uses a cursor to achieve this so your not really getting
rid of the cursor.


--
Andrew J. Kelly SQL MVP


"Joe Horton" <horj235 at lni dot wa dot gov> wrote

Quote:
You could get rid of your cursor by using sp_MSforeachtable

See notes below from web site re: undocumented SP's:
http://www.mssqlcity.com/Articles/Un...000UndocSP.htm
sp_MSforeachtable
Sometimes, you need to perform the same actions for all tables in the
database. You can create cursor for this purpose, or you can also use the
sp_MSforeachtable stored procedure to accomplish the same goal with less
work.

For example, you can use the sp_MSforeachtable stored procedure to rebuild
all the indexes in a database:

EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?')"


"Dave Mortenson" <dmortenson (AT) dentrix (DOT) com> wrote in message
news:eSfnM%23obEHA.1732 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Can anyone confirm this for me. If I were to run this script below. Will
it
reindex all tables indexes at one time as it goes through the cursor. Or
will it reindex one table indexes at a time as it goes through the
cursor.
Basically will it pause each time the DBCC command is executed?
If the first part is true where it executes all at once then what is a
good
way to only do one at a time? Can you pause between executions till each
execution is done?

THanks for the help.

DECLARE @TableName varchar(255)
DECLARE ReindexTableCursor CURSOR FOR
SELECT name FROM sysobjects WHERE xtype = 'U' AND (name = 'Practice' OR
name
= 'ID_Master')
ORDER BY name

OPEN ReindexTableCursor

-- Perform the first fetch.
FETCH NEXT FROM ReindexTableCursor INTO @TableName

-- Check @@FETCH_STATUS to see if there are any rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
PRINT 'Reindexing' + ' ' + @TableName
DBCC DBREINDEX (@TableName )
FETCH NEXT FROM ReindexTableCursor into @TableName
END

CLOSE ReindexTableCursor
DEALLOCATE ReindexTableCursor







Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.