Wich command DBCC can rebuild all indexes in my database?
Thanks
LeandroLSPlease see this script, which I got from www.sqlserverfaq.com
BEGIN
SET NOCOUNT ON
-- Basis of script taken from BOL
CREATE TABLE #fraglist (
ObjectName CHAR (255),
ObjectId INT,
IndexName CHAR (255),
IndexId INT,
Lvl INT,
CountPages INT,
CountRows INT,
MinRecSize INT,
MaxRecSize INT,
AvgRecSize INT,
ForRecCount INT,
Extents INT,
ExtentSwitches INT,
AvgFreeBytes INT,
AvgPageDensity INT,
ScanDensity DECIMAL,
BestCount INT,
ActualCount INT,
LogicalFrag DECIMAL,
ExtentFrag DECIMAL)
DECLARE tables_cur CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_NAME
OPEN tables_cur
DECLARE @.ObjectName sysname
FETCH NEXT FROM tables_cur INTO @.ObjectName
WHILE @.@.FETCH_STATUS = 0
BEGIN
EXEC( 'DBCC UPDATEUSAGE( 0, [' + @.ObjectName + '] ) WITH COUNT_ROWS,
NO_INFOMSGS' )
INSERT #fraglist
EXEC ( 'DBCC SHOWCONTIG( [' + @.ObjectName + '] ) WITH
TABLERESULTS, ALL_INDEXES, NO_INFOMSGS' )
FETCH NEXT FROM tables_cur INTO @.ObjectName
END
CLOSE tables_cur
DEALLOCATE tables_cur
DECLARE defrag_cur CURSOR FOR
SELECT ObjectName,
IndexName
FROM #fraglist fo
WHERE LogicalFrag > 1 -- % fragmentation
AND IndexID BETWEEN 1 AND 254
AND ( IndexID = 1
OR NOT EXISTS (
SELECT * -- If clustered index to defrag then don't
do anything else (no point).
FROM #fraglist fi
WHERE fi.ObjectId = fo.ObjectId
AND fi.IndexID = 1
)
)
DECLARE @.IndexName sysname
DECLARE @.sql varchar(1000)
OPEN defrag_cur
FETCH NEXT FROM defrag_cur INTO @.ObjectName, @.IndexName
WHILE @.@.FETCH_STATUS = 0
BEGIN
PRINT 'Processing dbreindex on ' + RTRIM( @.ObjectName ) + ',
indexname=' + @.IndexName
SET @.sql = 'DBCC DBREINDEX( ''' + RTRIM( @.ObjectName ) + ''', ' +
@.IndexName + ')'
PRINT @.sql
EXEC( @.sql )
PRINT 'Complete.'
FETCH NEXT FROM defrag_cur INTO @.ObjectName, @.IndexName
END
DEALLOCATE defrag_cur
DROP TABLE #fraglist
END
Cheers,
James Goodman MCSE, MCDBA
http://www.angelfire.com/sports/f1pictures|||Hi,
Use the below script to reindex all tables in a database,
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
PRINT "Reindexing - " + @.TableName
DBCC DBREINDEX(@.TableName)
FETCH NEXT FROM TableCursor INTO @.TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor
Thanks
Hari
MCDBA
"Leandro Loureiro dos Santos" <leandro@.email.com> wrote in message
news:#6ODkDB4DHA.1804@.TK2MSFTNGP12.phx.gbl...
quote:|||Leanardo
> Hello,
> Wich command DBCC can rebuild all indexes in my database?
> Thanks
> LeandroLS
>
Here's another approach
EXEC sp_msForEachTable @.COMMAND1= 'DBCC DBREINDEX ( "?")'
"Leandro Loureiro dos Santos" <leandro@.email.com> wrote in message
news:#6ODkDB4DHA.1804@.TK2MSFTNGP12.phx.gbl...
quote:|||Before you do this, take a moment to read the whitepaper at
> Hello,
> Wich command DBCC can rebuild all indexes in my database?
> Thanks
> LeandroLS
>
http://www.microsoft.com/technet/tr...ze/ss2kidbp.asp
which will help you determine whether rebuilding or defragging is the right
way to go, and even whether your workload will benefit from either.
Regards.
--
Paul Randal
Dev Lead, Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"Leandro Loureiro dos Santos" <leandro@.email.com> wrote in message
news:#6ODkDB4DHA.1804@.TK2MSFTNGP12.phx.gbl...
quote:
> Hello,
> Wich command DBCC can rebuild all indexes in my database?
> Thanks
> LeandroLS
>
No comments:
Post a Comment