Showing posts with label databases. Show all posts
Showing posts with label databases. Show all posts

Friday, March 30, 2012

Index Tuning Wizard

Hi

I tried running ITW against one of our databases, and it came up with a
number of suggestions, but also filled the Application Log with messages
like:-

The description for Event ID ( 4 ) in Source ( ITW ) could not be found. It
contains the following insertion string(s):
Error in Parsing Event:
declare @.P1 nvarchar(1)
declare @.P2 nvarchar(1)
declare @.P3 nvarchar(1)
declare @.P4 nvarchar(1)
EXEC "dbo"."sp_get_RAO_indexW2K2" @.P1 , @.P2 , @.P3 , @.P4 .

Does anyone know if this is serious (or indeed what it means at all)?

There were no apparent errors in the ITW run.

Chloe CrowderHi

This is not a stored procedure I recognise or can find. Are you running ITW
from a script or profile? If yes then does it exist there?

Also, if you are using procedure names "sp_" SQLServer will be expecting
them to be in the master database, and will check it first, before the
current database for your stored procedure. By calling your procedure
something other than "sp_" you should improve performance.

John

"Chloe Crowder" <chloe.crowder@.bl.uk> wrote in message
news:bfrc2o$abe$1@.south.jnrs.ja.net...
> Hi
> I tried running ITW against one of our databases, and it came up with a
> number of suggestions, but also filled the Application Log with messages
> like:-
> The description for Event ID ( 4 ) in Source ( ITW ) could not be found.
It
> contains the following insertion string(s):
> Error in Parsing Event:
> declare @.P1 nvarchar(1)
> declare @.P2 nvarchar(1)
> declare @.P3 nvarchar(1)
> declare @.P4 nvarchar(1)
> EXEC "dbo"."sp_get_RAO_indexW2K2" @.P1 , @.P2 , @.P3 , @.P4 .
> Does anyone know if this is serious (or indeed what it means at all)?
> There were no apparent errors in the ITW run.
> Chloe Crowder

Index tables in database

I have several development SQL Server 2000 databases that
need to index and I would like to know criteria's for
reindexing tables and frequencies.
Please help me resolve this issue.
Thank You,
DeanIt's hard without specifics, but you need to know your
application and data usage to come up with your optimal
indexing scheme. That's the rule you have to follow.
Beyond that, it's all specific to your environment.
>--Original Message--
>I have several development SQL Server 2000 databases that
>need to index and I would like to know criteria's for
>reindexing tables and frequencies.
>Please help me resolve this issue.
>Thank You,
>Dean
>.
>|||You can look for statement DBCC SHOWCONTIG.
Ronald
"Dean" <anonymous@.discussions.microsoft.com> wrote in message
news:035c01c3afa7$025ca0d0$a301280a@.phx.gbl...
> I have several development SQL Server 2000 databases that
> need to index and I would like to know criteria's for
> reindexing tables and frequencies.
> Please help me resolve this issue.
> Thank You,
> Dean|||You can utilize the "Index Tuning Wizard", located in the toolbar of SQL Server as "Run a wizard". To carry out this task need some querys that force the database and tables, this tool helped yourself to determine the criteria to create, modify and to erase index. This is a begin .... you need more information
Tinokio(H2
MCDBA

Friday, March 23, 2012

Index Question

Can anyone explain me why there are (_wa) indexes in my
databases when "real" indexes exists. Meaning: I have an
automatic index "showing" an index that has already been
created... any explanation?The '_wa' indexes are statistics. They are essential for the SQL optimizer
to decide on a good, efficient query plan.
--
Geoff N. Hiten
Microsoft SQL Server MVP
Senior Database Administrator
Careerbuilder.com
I support the Professional Association for SQL Server
www.sqlpass.org
":)" <anonymous@.discussions.microsoft.com> wrote in message
news:82ae01c431e6$21bd4df0$a301280a@.phx.gbl...
> Can anyone explain me why there are (_wa) indexes in my
> databases when "real" indexes exists. Meaning: I have an
> automatic index "showing" an index that has already been
> created... any explanation?sql

Wednesday, March 21, 2012

Index on MSDB

I have a query that looks at the backup tables for any databases on the
server that have not been backed up in the last n days. This server has been
running for 3-4 years doing nightly backups and transaction log backups all
during the day. I couldn't find a system procedure or any of the schema
views I needed so I and running these against the system tables
(specifically msdb..BackUpSet, msdb..BackupMediaFamily, and
master..sysdatabases). The 2 MSDB tables I'm using have large numbers of
rows & the query is doing clustered index scans.
My question is what is the danger of adding my own indexes on these tables
to make the query more efficient?
They will be non-clustered indexes on a single column. I've tried it on one
of our development servers with a HUGE improvement in the query plan &
performance. I know that a service pack or hotfix may delete the indexes or
prevent the sp from installing. I'll drop the index before any sp / hotfix
is installed in case the columns are no longer supported.
(as a follow-up to this, I have a script that needs the indexes to mimic the
procedure sp_delete_backuphistory. I have so much old useless data in the
tables from the last 3 years, this system procedure is useless the way it's
currently written using a cursor.)
The machines are:
WIN2000 sp4
SQL2K sp3a Standard with 2GB RAM
Thanks!If you are very sure about the impact of the new non clustered index that yo
u
are planning to add and tested it well, u can go ahead and add it. I sugges
t
that you take a Backup of your MSDB database before adding the index and be
prepared to drop the index if you have to approach Microsoft PSS for any
technical support (they may call this as un supported scenario). Again
monitor your Server for any issues after you add the index at least for
couple of days.
Thanks
"cw" wrote:

> I have a query that looks at the backup tables for any databases on the
> server that have not been backed up in the last n days. This server has be
en
> running for 3-4 years doing nightly backups and transaction log backups al
l
> during the day. I couldn't find a system procedure or any of the schema
> views I needed so I and running these against the system tables
> (specifically msdb..BackUpSet, msdb..BackupMediaFamily, and
> master..sysdatabases). The 2 MSDB tables I'm using have large numbers of
> rows & the query is doing clustered index scans.
> My question is what is the danger of adding my own indexes on these table
s
> to make the query more efficient?
> They will be non-clustered indexes on a single column. I've tried it on on
e
> of our development servers with a HUGE improvement in the query plan &
> performance. I know that a service pack or hotfix may delete the indexes
or
> prevent the sp from installing. I'll drop the index before any sp / hotfix
> is installed in case the columns are no longer supported.
> (as a follow-up to this, I have a script that needs the indexes to mimic t
he
> procedure sp_delete_backuphistory. I have so much old useless data in the
> tables from the last 3 years, this system procedure is useless the way it'
s
> currently written using a cursor.)
> The machines are:
> WIN2000 sp4
> SQL2K sp3a Standard with 2GB RAM
> Thanks!
>
>sql

Index on MSDB

I have a query that looks at the backup tables for any databases on the
server that have not been backed up in the last n days. This server has been
running for 3-4 years doing nightly backups and transaction log backups all
during the day. I couldn't find a system procedure or any of the schema
views I needed so I and running these against the system tables
(specifically msdb..BackUpSet, msdb..BackupMediaFamily, and
master..sysdatabases). The 2 MSDB tables I'm using have large numbers of
rows & the query is doing clustered index scans.
My question is what is the danger of adding my own indexes on these tables
to make the query more efficient?
They will be non-clustered indexes on a single column. I've tried it on one
of our development servers with a HUGE improvement in the query plan &
performance. I know that a service pack or hotfix may delete the indexes or
prevent the sp from installing. I'll drop the index before any sp / hotfix
is installed in case the columns are no longer supported.
(as a follow-up to this, I have a script that needs the indexes to mimic the
procedure sp_delete_backuphistory. I have so much old useless data in the
tables from the last 3 years, this system procedure is useless the way it's
currently written using a cursor.)
The machines are:
WIN2000 sp4
SQL2K sp3a Standard with 2GB RAM
Thanks!
If you are very sure about the impact of the new non clustered index that you
are planning to add and tested it well, u can go ahead and add it. I suggest
that you take a Backup of your MSDB database before adding the index and be
prepared to drop the index if you have to approach Microsoft PSS for any
technical support (they may call this as un supported scenario). Again
monitor your Server for any issues after you add the index at least for
couple of days.
Thanks
"cw" wrote:

> I have a query that looks at the backup tables for any databases on the
> server that have not been backed up in the last n days. This server has been
> running for 3-4 years doing nightly backups and transaction log backups all
> during the day. I couldn't find a system procedure or any of the schema
> views I needed so I and running these against the system tables
> (specifically msdb..BackUpSet, msdb..BackupMediaFamily, and
> master..sysdatabases). The 2 MSDB tables I'm using have large numbers of
> rows & the query is doing clustered index scans.
> My question is what is the danger of adding my own indexes on these tables
> to make the query more efficient?
> They will be non-clustered indexes on a single column. I've tried it on one
> of our development servers with a HUGE improvement in the query plan &
> performance. I know that a service pack or hotfix may delete the indexes or
> prevent the sp from installing. I'll drop the index before any sp / hotfix
> is installed in case the columns are no longer supported.
> (as a follow-up to this, I have a script that needs the indexes to mimic the
> procedure sp_delete_backuphistory. I have so much old useless data in the
> tables from the last 3 years, this system procedure is useless the way it's
> currently written using a cursor.)
> The machines are:
> WIN2000 sp4
> SQL2K sp3a Standard with 2GB RAM
> Thanks!
>
>

Index on MSDB

I have a query that looks at the backup tables for any databases on the
server that have not been backed up in the last n days. This server has been
running for 3-4 years doing nightly backups and transaction log backups all
during the day. I couldn't find a system procedure or any of the schema
views I needed so I and running these against the system tables
(specifically msdb..BackUpSet, msdb..BackupMediaFamily, and
master..sysdatabases). The 2 MSDB tables I'm using have large numbers of
rows & the query is doing clustered index scans.
My question is what is the danger of adding my own indexes on these tables
to make the query more efficient?
They will be non-clustered indexes on a single column. I've tried it on one
of our development servers with a HUGE improvement in the query plan &
performance. I know that a service pack or hotfix may delete the indexes or
prevent the sp from installing. I'll drop the index before any sp / hotfix
is installed in case the columns are no longer supported.
(as a follow-up to this, I have a script that needs the indexes to mimic the
procedure sp_delete_backuphistory. I have so much old useless data in the
tables from the last 3 years, this system procedure is useless the way it's
currently written using a cursor.)
The machines are:
WIN2000 sp4
SQL2K sp3a Standard with 2GB RAM
Thanks!If you are very sure about the impact of the new non clustered index that you
are planning to add and tested it well, u can go ahead and add it. I suggest
that you take a Backup of your MSDB database before adding the index and be
prepared to drop the index if you have to approach Microsoft PSS for any
technical support (they may call this as un supported scenario). Again
monitor your Server for any issues after you add the index at least for
couple of days.
Thanks
"cw" wrote:
> I have a query that looks at the backup tables for any databases on the
> server that have not been backed up in the last n days. This server has been
> running for 3-4 years doing nightly backups and transaction log backups all
> during the day. I couldn't find a system procedure or any of the schema
> views I needed so I and running these against the system tables
> (specifically msdb..BackUpSet, msdb..BackupMediaFamily, and
> master..sysdatabases). The 2 MSDB tables I'm using have large numbers of
> rows & the query is doing clustered index scans.
> My question is what is the danger of adding my own indexes on these tables
> to make the query more efficient?
> They will be non-clustered indexes on a single column. I've tried it on one
> of our development servers with a HUGE improvement in the query plan &
> performance. I know that a service pack or hotfix may delete the indexes or
> prevent the sp from installing. I'll drop the index before any sp / hotfix
> is installed in case the columns are no longer supported.
> (as a follow-up to this, I have a script that needs the indexes to mimic the
> procedure sp_delete_backuphistory. I have so much old useless data in the
> tables from the last 3 years, this system procedure is useless the way it's
> currently written using a cursor.)
> The machines are:
> WIN2000 sp4
> SQL2K sp3a Standard with 2GB RAM
> Thanks!
>
>

Monday, March 12, 2012

Index Maint DBCC assistance.

I am working with multiple servers, and numerous databases. I have
some large databases, some tables with 100's of millions of rows.
Some of the DB's are 24/7 so recreating the indexes is not really an
option. I am trying to come up with a "smart" index plan. I have
seen many examples of this, and I have one that does seem to work,
except that the DBCC showcontig seems to take forever! I am using a
variation on the following. The DBCC Showcontig seems to lock the
table, is this true?
Is there a faster way to get the DBCC fragmentation information?
Is it really worth the work identifying and defragmenting those
indexes that fall below a threshold or should I just defrag every
index?
Any other help on a "smart" maint plan for index maint would be
appreciated!
OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
I am on SQL 2000, some at SP3a / SP4.
Sample of code below:
http://msdn2.microsoft.com/en-us/library/ms175008.aspx
-- Declare variables
SET NOCOUNT ON;
DECLARE @.tablename varchar(128);
DECLARE @.execstr varchar(255);
DECLARE @.objectid int;
DECLARE @.indexid int;
DECLARE @.frag decimal;
DECLARE @.maxfrag decimal;
-- Decide on the maximum fragmentation to allow for.
SELECT @.maxfrag = 30.0;
-- Declare a cursor.
DECLARE tables CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';
-- Create the table.
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);
-- Open the cursor.
OPEN tables;
-- Loop through all the tables in the database.
FETCH NEXT
FROM tables
INTO @.tablename;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
-- Do the showcontig of all indexes of the table
INSERT INTO #fraglist
EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
FETCH NEXT
FROM tables
INTO @.tablename;
END;
-- Close and deallocate the cursor.
CLOSE tables;
DEALLOCATE tables;
-- Declare the cursor for the list of indexes to be defragged.
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
FROM #fraglist
WHERE LogicalFrag >= @.maxfrag
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
-- Open the cursor.
OPEN indexes;
-- Loop through the indexes.
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
' + RTRIM(@.indexid) + ') - fragmentation currently '
+ RTRIM(CONVERT(varchar(15),@.frag)) + '%';
SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
' + RTRIM(@.indexid) + ')';
EXEC (@.execstr);
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
END;
-- Close and deallocate the cursor.
CLOSE indexes;
DEALLOCATE indexes;
-- Delete the temporary table.
DROP TABLE #fraglist;
GO
Do you have a test environment where you can load a copy of prod? If so,
generate the defrag script there but execute it against prod. This way, you
won't be locking up the table in prod with a share lock by running DBCC
SHOWCONTIG.
Tom
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"David Hay" <david.hay@.gmail.com> wrote in message
news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
I am working with multiple servers, and numerous databases. I have
some large databases, some tables with 100's of millions of rows.
Some of the DB's are 24/7 so recreating the indexes is not really an
option. I am trying to come up with a "smart" index plan. I have
seen many examples of this, and I have one that does seem to work,
except that the DBCC showcontig seems to take forever! I am using a
variation on the following. The DBCC Showcontig seems to lock the
table, is this true?
Is there a faster way to get the DBCC fragmentation information?
Is it really worth the work identifying and defragmenting those
indexes that fall below a threshold or should I just defrag every
index?
Any other help on a "smart" maint plan for index maint would be
appreciated!
OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
I am on SQL 2000, some at SP3a / SP4.
Sample of code below:
http://msdn2.microsoft.com/en-us/library/ms175008.aspx
-- Declare variables
SET NOCOUNT ON;
DECLARE @.tablename varchar(128);
DECLARE @.execstr varchar(255);
DECLARE @.objectid int;
DECLARE @.indexid int;
DECLARE @.frag decimal;
DECLARE @.maxfrag decimal;
-- Decide on the maximum fragmentation to allow for.
SELECT @.maxfrag = 30.0;
-- Declare a cursor.
DECLARE tables CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';
-- Create the table.
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);
-- Open the cursor.
OPEN tables;
-- Loop through all the tables in the database.
FETCH NEXT
FROM tables
INTO @.tablename;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
-- Do the showcontig of all indexes of the table
INSERT INTO #fraglist
EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
FETCH NEXT
FROM tables
INTO @.tablename;
END;
-- Close and deallocate the cursor.
CLOSE tables;
DEALLOCATE tables;
-- Declare the cursor for the list of indexes to be defragged.
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
FROM #fraglist
WHERE LogicalFrag >= @.maxfrag
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
-- Open the cursor.
OPEN indexes;
-- Loop through the indexes.
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
' + RTRIM(@.indexid) + ') - fragmentation currently '
+ RTRIM(CONVERT(varchar(15),@.frag)) + '%';
SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
' + RTRIM(@.indexid) + ')';
EXEC (@.execstr);
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
END;
-- Close and deallocate the cursor.
CLOSE indexes;
DEALLOCATE indexes;
-- Delete the temporary table.
DROP TABLE #fraglist;
GO
|||The way to really solve this is to upgrade to 2005 as the equivalent of
Showcontig does not block like in 2000. But the code you have now seems to
be the fastest and lightest way to do this in 2000. I never recommend on
larger dbs to reindex blindly so your approach is what I would recommend
anyway. You can keep a history of the fragmentation by index and use that to
determine which to defrag. Chances are if the usage is roughly the same week
to week the indexes will be roughly the same in terms of fragmentation. So
collect samples for a few weeks and base your rebuilding off of those
numbers for x many future weeks.
Andrew J. Kelly SQL MVP
Solid Quality Mentors
"David Hay" <david.hay@.gmail.com> wrote in message
news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
>I am working with multiple servers, and numerous databases. I have
> some large databases, some tables with 100's of millions of rows.
> Some of the DB's are 24/7 so recreating the indexes is not really an
> option. I am trying to come up with a "smart" index plan. I have
> seen many examples of this, and I have one that does seem to work,
> except that the DBCC showcontig seems to take forever! I am using a
> variation on the following. The DBCC Showcontig seems to lock the
> table, is this true?
> Is there a faster way to get the DBCC fragmentation information?
> Is it really worth the work identifying and defragmenting those
> indexes that fall below a threshold or should I just defrag every
> index?
> Any other help on a "smart" maint plan for index maint would be
> appreciated!
> OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
> I am on SQL 2000, some at SP3a / SP4.
> Sample of code below:
> http://msdn2.microsoft.com/en-us/library/ms175008.aspx
> -- Declare variables
> SET NOCOUNT ON;
> DECLARE @.tablename varchar(128);
> DECLARE @.execstr varchar(255);
> DECLARE @.objectid int;
> DECLARE @.indexid int;
> DECLARE @.frag decimal;
> DECLARE @.maxfrag decimal;
> -- Decide on the maximum fragmentation to allow for.
> SELECT @.maxfrag = 30.0;
> -- Declare a cursor.
> DECLARE tables CURSOR FOR
> SELECT TABLE_NAME
> FROM INFORMATION_SCHEMA.TABLES
> WHERE TABLE_TYPE = 'BASE TABLE';
> -- Create the table.
> 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);
> -- Open the cursor.
> OPEN tables;
> -- Loop through all the tables in the database.
> FETCH NEXT
> FROM tables
> INTO @.tablename;
> WHILE @.@.FETCH_STATUS = 0
> BEGIN;
> -- Do the showcontig of all indexes of the table
> INSERT INTO #fraglist
> EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
> WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
> FETCH NEXT
> FROM tables
> INTO @.tablename;
> END;
> -- Close and deallocate the cursor.
> CLOSE tables;
> DEALLOCATE tables;
> -- Declare the cursor for the list of indexes to be defragged.
> DECLARE indexes CURSOR FOR
> SELECT ObjectName, ObjectId, IndexId, LogicalFrag
> FROM #fraglist
> WHERE LogicalFrag >= @.maxfrag
> AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
> -- Open the cursor.
> OPEN indexes;
> -- Loop through the indexes.
> FETCH NEXT
> FROM indexes
> INTO @.tablename, @.objectid, @.indexid, @.frag;
> WHILE @.@.FETCH_STATUS = 0
> BEGIN;
> PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
> ' + RTRIM(@.indexid) + ') - fragmentation currently '
> + RTRIM(CONVERT(varchar(15),@.frag)) + '%';
> SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
> ' + RTRIM(@.indexid) + ')';
> EXEC (@.execstr);
> FETCH NEXT
> FROM indexes
> INTO @.tablename, @.objectid, @.indexid, @.frag;
> END;
> -- Close and deallocate the cursor.
> CLOSE indexes;
> DEALLOCATE indexes;
> -- Delete the temporary table.
> DROP TABLE #fraglist;
> GO
|||And perhaps this "history of the index fragmentation" may show that some
indexes are fragmented too soon and maybe a new fill factor will be needed.
Hope this helps,
Ben Nevarez
Senior Database Administrator
AIG SunAmerica
"Andrew J. Kelly" wrote:

> The way to really solve this is to upgrade to 2005 as the equivalent of
> Showcontig does not block like in 2000. But the code you have now seems to
> be the fastest and lightest way to do this in 2000. I never recommend on
> larger dbs to reindex blindly so your approach is what I would recommend
> anyway. You can keep a history of the fragmentation by index and use that to
> determine which to defrag. Chances are if the usage is roughly the same week
> to week the indexes will be roughly the same in terms of fragmentation. So
> collect samples for a few weeks and base your rebuilding off of those
> numbers for x many future weeks.
> --
> Andrew J. Kelly SQL MVP
> Solid Quality Mentors
>
> "David Hay" <david.hay@.gmail.com> wrote in message
> news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
>
|||Yes good point Ben, I should have noted that as well.
Andrew J. Kelly SQL MVP
Solid Quality Mentors
"Ben Nevarez" <BenNevarez@.discussions.microsoft.com> wrote in message
news:93BADA3C-FD11-414E-9044-21E3D63B622B@.microsoft.com...[vbcol=seagreen]
> And perhaps this "history of the index fragmentation" may show that some
> indexes are fragmented too soon and maybe a new fill factor will be
> needed.
> Hope this helps,
> Ben Nevarez
> Senior Database Administrator
> AIG SunAmerica
>
> "Andrew J. Kelly" wrote:
|||Thanks to all for the information. I did run a test in Prod, and it
ran much faster. (Much larger box). I saw a script that loaded a
history table and I'll work on building that.
Thanks again to all responding.
David

Index Maint DBCC assistance.

I am working with multiple servers, and numerous databases. I have
some large databases, some tables with 100's of millions of rows.
Some of the DB's are 24/7 so recreating the indexes is not really an
option. I am trying to come up with a "smart" index plan. I have
seen many examples of this, and I have one that does seem to work,
except that the DBCC showcontig seems to take forever! I am using a
variation on the following. The DBCC Showcontig seems to lock the
table, is this true?
Is there a faster way to get the DBCC fragmentation information?
Is it really worth the work identifying and defragmenting those
indexes that fall below a threshold or should I just defrag every
index?
Any other help on a "smart" maint plan for index maint would be
appreciated!
OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
I am on SQL 2000, some at SP3a / SP4.
Sample of code below:
http://msdn2.microsoft.com/en-us/library/ms175008.aspx
-- Declare variables
SET NOCOUNT ON;
DECLARE @.tablename varchar(128);
DECLARE @.execstr varchar(255);
DECLARE @.objectid int;
DECLARE @.indexid int;
DECLARE @.frag decimal;
DECLARE @.maxfrag decimal;
-- Decide on the maximum fragmentation to allow for.
SELECT @.maxfrag = 30.0;
-- Declare a cursor.
DECLARE tables CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';
-- Create the table.
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);
-- Open the cursor.
OPEN tables;
-- Loop through all the tables in the database.
FETCH NEXT
FROM tables
INTO @.tablename;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
-- Do the showcontig of all indexes of the table
INSERT INTO #fraglist
EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
FETCH NEXT
FROM tables
INTO @.tablename;
END;
-- Close and deallocate the cursor.
CLOSE tables;
DEALLOCATE tables;
-- Declare the cursor for the list of indexes to be defragged.
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
FROM #fraglist
WHERE LogicalFrag >= @.maxfrag
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
-- Open the cursor.
OPEN indexes;
-- Loop through the indexes.
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
' + RTRIM(@.indexid) + ') - fragmentation currently '
+ RTRIM(CONVERT(varchar(15),@.frag)) + '%';
SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
' + RTRIM(@.indexid) + ')';
EXEC (@.execstr);
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
END;
-- Close and deallocate the cursor.
CLOSE indexes;
DEALLOCATE indexes;
-- Delete the temporary table.
DROP TABLE #fraglist;
GODo you have a test environment where you can load a copy of prod? If so,
generate the defrag script there but execute it against prod. This way, you
won't be locking up the table in prod with a share lock by running DBCC
SHOWCONTIG.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"David Hay" <david.hay@.gmail.com> wrote in message
news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
I am working with multiple servers, and numerous databases. I have
some large databases, some tables with 100's of millions of rows.
Some of the DB's are 24/7 so recreating the indexes is not really an
option. I am trying to come up with a "smart" index plan. I have
seen many examples of this, and I have one that does seem to work,
except that the DBCC showcontig seems to take forever! I am using a
variation on the following. The DBCC Showcontig seems to lock the
table, is this true?
Is there a faster way to get the DBCC fragmentation information?
Is it really worth the work identifying and defragmenting those
indexes that fall below a threshold or should I just defrag every
index?
Any other help on a "smart" maint plan for index maint would be
appreciated!
OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
I am on SQL 2000, some at SP3a / SP4.
Sample of code below:
http://msdn2.microsoft.com/en-us/library/ms175008.aspx
-- Declare variables
SET NOCOUNT ON;
DECLARE @.tablename varchar(128);
DECLARE @.execstr varchar(255);
DECLARE @.objectid int;
DECLARE @.indexid int;
DECLARE @.frag decimal;
DECLARE @.maxfrag decimal;
-- Decide on the maximum fragmentation to allow for.
SELECT @.maxfrag = 30.0;
-- Declare a cursor.
DECLARE tables CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';
-- Create the table.
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);
-- Open the cursor.
OPEN tables;
-- Loop through all the tables in the database.
FETCH NEXT
FROM tables
INTO @.tablename;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
-- Do the showcontig of all indexes of the table
INSERT INTO #fraglist
EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
FETCH NEXT
FROM tables
INTO @.tablename;
END;
-- Close and deallocate the cursor.
CLOSE tables;
DEALLOCATE tables;
-- Declare the cursor for the list of indexes to be defragged.
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
FROM #fraglist
WHERE LogicalFrag >= @.maxfrag
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
-- Open the cursor.
OPEN indexes;
-- Loop through the indexes.
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
' + RTRIM(@.indexid) + ') - fragmentation currently '
+ RTRIM(CONVERT(varchar(15),@.frag)) + '%';
SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
' + RTRIM(@.indexid) + ')';
EXEC (@.execstr);
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
END;
-- Close and deallocate the cursor.
CLOSE indexes;
DEALLOCATE indexes;
-- Delete the temporary table.
DROP TABLE #fraglist;
GO|||The way to really solve this is to upgrade to 2005 as the equivalent of
Showcontig does not block like in 2000. But the code you have now seems to
be the fastest and lightest way to do this in 2000. I never recommend on
larger dbs to reindex blindly so your approach is what I would recommend
anyway. You can keep a history of the fragmentation by index and use that to
determine which to defrag. Chances are if the usage is roughly the same week
to week the indexes will be roughly the same in terms of fragmentation. So
collect samples for a few weeks and base your rebuilding off of those
numbers for x many future weeks.
Andrew J. Kelly SQL MVP
Solid Quality Mentors
"David Hay" <david.hay@.gmail.com> wrote in message
news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
>I am working with multiple servers, and numerous databases. I have
> some large databases, some tables with 100's of millions of rows.
> Some of the DB's are 24/7 so recreating the indexes is not really an
> option. I am trying to come up with a "smart" index plan. I have
> seen many examples of this, and I have one that does seem to work,
> except that the DBCC showcontig seems to take forever! I am using a
> variation on the following. The DBCC Showcontig seems to lock the
> table, is this true?
> Is there a faster way to get the DBCC fragmentation information?
> Is it really worth the work identifying and defragmenting those
> indexes that fall below a threshold or should I just defrag every
> index?
> Any other help on a "smart" maint plan for index maint would be
> appreciated!
> OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
> I am on SQL 2000, some at SP3a / SP4.
> Sample of code below:
> http://msdn2.microsoft.com/en-us/library/ms175008.aspx
> -- Declare variables
> SET NOCOUNT ON;
> DECLARE @.tablename varchar(128);
> DECLARE @.execstr varchar(255);
> DECLARE @.objectid int;
> DECLARE @.indexid int;
> DECLARE @.frag decimal;
> DECLARE @.maxfrag decimal;
> -- Decide on the maximum fragmentation to allow for.
> SELECT @.maxfrag = 30.0;
> -- Declare a cursor.
> DECLARE tables CURSOR FOR
> SELECT TABLE_NAME
> FROM INFORMATION_SCHEMA.TABLES
> WHERE TABLE_TYPE = 'BASE TABLE';
> -- Create the table.
> 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);
> -- Open the cursor.
> OPEN tables;
> -- Loop through all the tables in the database.
> FETCH NEXT
> FROM tables
> INTO @.tablename;
> WHILE @.@.FETCH_STATUS = 0
> BEGIN;
> -- Do the showcontig of all indexes of the table
> INSERT INTO #fraglist
> EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
> WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
> FETCH NEXT
> FROM tables
> INTO @.tablename;
> END;
> -- Close and deallocate the cursor.
> CLOSE tables;
> DEALLOCATE tables;
> -- Declare the cursor for the list of indexes to be defragged.
> DECLARE indexes CURSOR FOR
> SELECT ObjectName, ObjectId, IndexId, LogicalFrag
> FROM #fraglist
> WHERE LogicalFrag >= @.maxfrag
> AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
> -- Open the cursor.
> OPEN indexes;
> -- Loop through the indexes.
> FETCH NEXT
> FROM indexes
> INTO @.tablename, @.objectid, @.indexid, @.frag;
> WHILE @.@.FETCH_STATUS = 0
> BEGIN;
> PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
> ' + RTRIM(@.indexid) + ') - fragmentation currently '
> + RTRIM(CONVERT(varchar(15),@.frag)) + '%';
> SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
> ' + RTRIM(@.indexid) + ')';
> EXEC (@.execstr);
> FETCH NEXT
> FROM indexes
> INTO @.tablename, @.objectid, @.indexid, @.frag;
> END;
> -- Close and deallocate the cursor.
> CLOSE indexes;
> DEALLOCATE indexes;
> -- Delete the temporary table.
> DROP TABLE #fraglist;
> GO|||And perhaps this "history of the index fragmentation" may show that some
indexes are fragmented too soon and maybe a new fill factor will be needed.
Hope this helps,
Ben Nevarez
Senior Database Administrator
AIG SunAmerica
"Andrew J. Kelly" wrote:

> The way to really solve this is to upgrade to 2005 as the equivalent of
> Showcontig does not block like in 2000. But the code you have now seems to
> be the fastest and lightest way to do this in 2000. I never recommend on
> larger dbs to reindex blindly so your approach is what I would recommend
> anyway. You can keep a history of the fragmentation by index and use that
to
> determine which to defrag. Chances are if the usage is roughly the same we
ek
> to week the indexes will be roughly the same in terms of fragmentation. So
> collect samples for a few weeks and base your rebuilding off of those
> numbers for x many future weeks.
> --
> Andrew J. Kelly SQL MVP
> Solid Quality Mentors
>
> "David Hay" <david.hay@.gmail.com> wrote in message
> news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
>|||Yes good point Ben, I should have noted that as well.
Andrew J. Kelly SQL MVP
Solid Quality Mentors
"Ben Nevarez" <BenNevarez@.discussions.microsoft.com> wrote in message
news:93BADA3C-FD11-414E-9044-21E3D63B622B@.microsoft.com...[vbcol=seagreen]
> And perhaps this "history of the index fragmentation" may show that some
> indexes are fragmented too soon and maybe a new fill factor will be
> needed.
> Hope this helps,
> Ben Nevarez
> Senior Database Administrator
> AIG SunAmerica
>
> "Andrew J. Kelly" wrote:
>|||Thanks to all for the information. I did run a test in Prod, and it
ran much faster. (Much larger box). I saw a script that loaded a
history table and I'll work on building that.
Thanks again to all responding.
David

Index Maint DBCC assistance.

I am working with multiple servers, and numerous databases. I have
some large databases, some tables with 100's of millions of rows.
Some of the DB's are 24/7 so recreating the indexes is not really an
option. I am trying to come up with a "smart" index plan. I have
seen many examples of this, and I have one that does seem to work,
except that the DBCC showcontig seems to take forever! I am using a
variation on the following. The DBCC Showcontig seems to lock the
table, is this true?
Is there a faster way to get the DBCC fragmentation information?
Is it really worth the work identifying and defragmenting those
indexes that fall below a threshold or should I just defrag every
index?
Any other help on a "smart" maint plan for index maint would be
appreciated!
OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
I am on SQL 2000, some at SP3a / SP4.
Sample of code below:
http://msdn2.microsoft.com/en-us/library/ms175008.aspx
-- Declare variables
SET NOCOUNT ON;
DECLARE @.tablename varchar(128);
DECLARE @.execstr varchar(255);
DECLARE @.objectid int;
DECLARE @.indexid int;
DECLARE @.frag decimal;
DECLARE @.maxfrag decimal;
-- Decide on the maximum fragmentation to allow for.
SELECT @.maxfrag = 30.0;
-- Declare a cursor.
DECLARE tables CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';
-- Create the table.
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);
-- Open the cursor.
OPEN tables;
-- Loop through all the tables in the database.
FETCH NEXT
FROM tables
INTO @.tablename;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
-- Do the showcontig of all indexes of the table
INSERT INTO #fraglist
EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
FETCH NEXT
FROM tables
INTO @.tablename;
END;
-- Close and deallocate the cursor.
CLOSE tables;
DEALLOCATE tables;
-- Declare the cursor for the list of indexes to be defragged.
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
FROM #fraglist
WHERE LogicalFrag >= @.maxfrag
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
-- Open the cursor.
OPEN indexes;
-- Loop through the indexes.
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
' + RTRIM(@.indexid) + ') - fragmentation currently '
+ RTRIM(CONVERT(varchar(15),@.frag)) + '%';
SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
' + RTRIM(@.indexid) + ')';
EXEC (@.execstr);
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
END;
-- Close and deallocate the cursor.
CLOSE indexes;
DEALLOCATE indexes;
-- Delete the temporary table.
DROP TABLE #fraglist;
GODo you have a test environment where you can load a copy of prod? If so,
generate the defrag script there but execute it against prod. This way, you
won't be locking up the table in prod with a share lock by running DBCC
SHOWCONTIG.
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"David Hay" <david.hay@.gmail.com> wrote in message
news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
I am working with multiple servers, and numerous databases. I have
some large databases, some tables with 100's of millions of rows.
Some of the DB's are 24/7 so recreating the indexes is not really an
option. I am trying to come up with a "smart" index plan. I have
seen many examples of this, and I have one that does seem to work,
except that the DBCC showcontig seems to take forever! I am using a
variation on the following. The DBCC Showcontig seems to lock the
table, is this true?
Is there a faster way to get the DBCC fragmentation information?
Is it really worth the work identifying and defragmenting those
indexes that fall below a threshold or should I just defrag every
index?
Any other help on a "smart" maint plan for index maint would be
appreciated!
OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
I am on SQL 2000, some at SP3a / SP4.
Sample of code below:
http://msdn2.microsoft.com/en-us/library/ms175008.aspx
-- Declare variables
SET NOCOUNT ON;
DECLARE @.tablename varchar(128);
DECLARE @.execstr varchar(255);
DECLARE @.objectid int;
DECLARE @.indexid int;
DECLARE @.frag decimal;
DECLARE @.maxfrag decimal;
-- Decide on the maximum fragmentation to allow for.
SELECT @.maxfrag = 30.0;
-- Declare a cursor.
DECLARE tables CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';
-- Create the table.
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);
-- Open the cursor.
OPEN tables;
-- Loop through all the tables in the database.
FETCH NEXT
FROM tables
INTO @.tablename;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
-- Do the showcontig of all indexes of the table
INSERT INTO #fraglist
EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
FETCH NEXT
FROM tables
INTO @.tablename;
END;
-- Close and deallocate the cursor.
CLOSE tables;
DEALLOCATE tables;
-- Declare the cursor for the list of indexes to be defragged.
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
FROM #fraglist
WHERE LogicalFrag >= @.maxfrag
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
-- Open the cursor.
OPEN indexes;
-- Loop through the indexes.
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
WHILE @.@.FETCH_STATUS = 0
BEGIN;
PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
' + RTRIM(@.indexid) + ') - fragmentation currently '
+ RTRIM(CONVERT(varchar(15),@.frag)) + '%';
SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
' + RTRIM(@.indexid) + ')';
EXEC (@.execstr);
FETCH NEXT
FROM indexes
INTO @.tablename, @.objectid, @.indexid, @.frag;
END;
-- Close and deallocate the cursor.
CLOSE indexes;
DEALLOCATE indexes;
-- Delete the temporary table.
DROP TABLE #fraglist;
GO|||The way to really solve this is to upgrade to 2005 as the equivalent of
Showcontig does not block like in 2000. But the code you have now seems to
be the fastest and lightest way to do this in 2000. I never recommend on
larger dbs to reindex blindly so your approach is what I would recommend
anyway. You can keep a history of the fragmentation by index and use that to
determine which to defrag. Chances are if the usage is roughly the same week
to week the indexes will be roughly the same in terms of fragmentation. So
collect samples for a few weeks and base your rebuilding off of those
numbers for x many future weeks.
--
Andrew J. Kelly SQL MVP
Solid Quality Mentors
"David Hay" <david.hay@.gmail.com> wrote in message
news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
>I am working with multiple servers, and numerous databases. I have
> some large databases, some tables with 100's of millions of rows.
> Some of the DB's are 24/7 so recreating the indexes is not really an
> option. I am trying to come up with a "smart" index plan. I have
> seen many examples of this, and I have one that does seem to work,
> except that the DBCC showcontig seems to take forever! I am using a
> variation on the following. The DBCC Showcontig seems to lock the
> table, is this true?
> Is there a faster way to get the DBCC fragmentation information?
> Is it really worth the work identifying and defragmenting those
> indexes that fall below a threshold or should I just defrag every
> index?
> Any other help on a "smart" maint plan for index maint would be
> appreciated!
> OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
> I am on SQL 2000, some at SP3a / SP4.
> Sample of code below:
> http://msdn2.microsoft.com/en-us/library/ms175008.aspx
> -- Declare variables
> SET NOCOUNT ON;
> DECLARE @.tablename varchar(128);
> DECLARE @.execstr varchar(255);
> DECLARE @.objectid int;
> DECLARE @.indexid int;
> DECLARE @.frag decimal;
> DECLARE @.maxfrag decimal;
> -- Decide on the maximum fragmentation to allow for.
> SELECT @.maxfrag = 30.0;
> -- Declare a cursor.
> DECLARE tables CURSOR FOR
> SELECT TABLE_NAME
> FROM INFORMATION_SCHEMA.TABLES
> WHERE TABLE_TYPE = 'BASE TABLE';
> -- Create the table.
> 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);
> -- Open the cursor.
> OPEN tables;
> -- Loop through all the tables in the database.
> FETCH NEXT
> FROM tables
> INTO @.tablename;
> WHILE @.@.FETCH_STATUS = 0
> BEGIN;
> -- Do the showcontig of all indexes of the table
> INSERT INTO #fraglist
> EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
> WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
> FETCH NEXT
> FROM tables
> INTO @.tablename;
> END;
> -- Close and deallocate the cursor.
> CLOSE tables;
> DEALLOCATE tables;
> -- Declare the cursor for the list of indexes to be defragged.
> DECLARE indexes CURSOR FOR
> SELECT ObjectName, ObjectId, IndexId, LogicalFrag
> FROM #fraglist
> WHERE LogicalFrag >= @.maxfrag
> AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
> -- Open the cursor.
> OPEN indexes;
> -- Loop through the indexes.
> FETCH NEXT
> FROM indexes
> INTO @.tablename, @.objectid, @.indexid, @.frag;
> WHILE @.@.FETCH_STATUS = 0
> BEGIN;
> PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
> ' + RTRIM(@.indexid) + ') - fragmentation currently '
> + RTRIM(CONVERT(varchar(15),@.frag)) + '%';
> SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
> ' + RTRIM(@.indexid) + ')';
> EXEC (@.execstr);
> FETCH NEXT
> FROM indexes
> INTO @.tablename, @.objectid, @.indexid, @.frag;
> END;
> -- Close and deallocate the cursor.
> CLOSE indexes;
> DEALLOCATE indexes;
> -- Delete the temporary table.
> DROP TABLE #fraglist;
> GO|||And perhaps this "history of the index fragmentation" may show that some
indexes are fragmented too soon and maybe a new fill factor will be needed.
Hope this helps,
Ben Nevarez
Senior Database Administrator
AIG SunAmerica
"Andrew J. Kelly" wrote:
> The way to really solve this is to upgrade to 2005 as the equivalent of
> Showcontig does not block like in 2000. But the code you have now seems to
> be the fastest and lightest way to do this in 2000. I never recommend on
> larger dbs to reindex blindly so your approach is what I would recommend
> anyway. You can keep a history of the fragmentation by index and use that to
> determine which to defrag. Chances are if the usage is roughly the same week
> to week the indexes will be roughly the same in terms of fragmentation. So
> collect samples for a few weeks and base your rebuilding off of those
> numbers for x many future weeks.
> --
> Andrew J. Kelly SQL MVP
> Solid Quality Mentors
>
> "David Hay" <david.hay@.gmail.com> wrote in message
> news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
> >I am working with multiple servers, and numerous databases. I have
> > some large databases, some tables with 100's of millions of rows.
> > Some of the DB's are 24/7 so recreating the indexes is not really an
> > option. I am trying to come up with a "smart" index plan. I have
> > seen many examples of this, and I have one that does seem to work,
> > except that the DBCC showcontig seems to take forever! I am using a
> > variation on the following. The DBCC Showcontig seems to lock the
> > table, is this true?
> >
> > Is there a faster way to get the DBCC fragmentation information?
> >
> > Is it really worth the work identifying and defragmenting those
> > indexes that fall below a threshold or should I just defrag every
> > index?
> >
> > Any other help on a "smart" maint plan for index maint would be
> > appreciated!
> >
> > OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
> >
> > I am on SQL 2000, some at SP3a / SP4.
> >
> > Sample of code below:
> >
> > http://msdn2.microsoft.com/en-us/library/ms175008.aspx
> >
> > -- Declare variables
> > SET NOCOUNT ON;
> > DECLARE @.tablename varchar(128);
> > DECLARE @.execstr varchar(255);
> > DECLARE @.objectid int;
> > DECLARE @.indexid int;
> > DECLARE @.frag decimal;
> > DECLARE @.maxfrag decimal;
> >
> > -- Decide on the maximum fragmentation to allow for.
> > SELECT @.maxfrag = 30.0;
> >
> > -- Declare a cursor.
> > DECLARE tables CURSOR FOR
> > SELECT TABLE_NAME
> > FROM INFORMATION_SCHEMA.TABLES
> > WHERE TABLE_TYPE = 'BASE TABLE';
> >
> > -- Create the table.
> > 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);
> >
> > -- Open the cursor.
> > OPEN tables;
> >
> > -- Loop through all the tables in the database.
> > FETCH NEXT
> > FROM tables
> > INTO @.tablename;
> >
> > WHILE @.@.FETCH_STATUS = 0
> > BEGIN;
> > -- Do the showcontig of all indexes of the table
> > INSERT INTO #fraglist
> > EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
> > WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
> > FETCH NEXT
> > FROM tables
> > INTO @.tablename;
> > END;
> >
> > -- Close and deallocate the cursor.
> > CLOSE tables;
> > DEALLOCATE tables;
> >
> > -- Declare the cursor for the list of indexes to be defragged.
> > DECLARE indexes CURSOR FOR
> > SELECT ObjectName, ObjectId, IndexId, LogicalFrag
> > FROM #fraglist
> > WHERE LogicalFrag >= @.maxfrag
> > AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
> >
> > -- Open the cursor.
> > OPEN indexes;
> >
> > -- Loop through the indexes.
> > FETCH NEXT
> > FROM indexes
> > INTO @.tablename, @.objectid, @.indexid, @.frag;
> >
> > WHILE @.@.FETCH_STATUS = 0
> > BEGIN;
> > PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
> > ' + RTRIM(@.indexid) + ') - fragmentation currently '
> > + RTRIM(CONVERT(varchar(15),@.frag)) + '%';
> > SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
> > ' + RTRIM(@.indexid) + ')';
> > EXEC (@.execstr);
> >
> > FETCH NEXT
> > FROM indexes
> > INTO @.tablename, @.objectid, @.indexid, @.frag;
> > END;
> >
> > -- Close and deallocate the cursor.
> > CLOSE indexes;
> > DEALLOCATE indexes;
> >
> > -- Delete the temporary table.
> > DROP TABLE #fraglist;
> > GO
>|||Yes good point Ben, I should have noted that as well.
--
Andrew J. Kelly SQL MVP
Solid Quality Mentors
"Ben Nevarez" <BenNevarez@.discussions.microsoft.com> wrote in message
news:93BADA3C-FD11-414E-9044-21E3D63B622B@.microsoft.com...
> And perhaps this "history of the index fragmentation" may show that some
> indexes are fragmented too soon and maybe a new fill factor will be
> needed.
> Hope this helps,
> Ben Nevarez
> Senior Database Administrator
> AIG SunAmerica
>
> "Andrew J. Kelly" wrote:
>> The way to really solve this is to upgrade to 2005 as the equivalent of
>> Showcontig does not block like in 2000. But the code you have now seems
>> to
>> be the fastest and lightest way to do this in 2000. I never recommend on
>> larger dbs to reindex blindly so your approach is what I would recommend
>> anyway. You can keep a history of the fragmentation by index and use that
>> to
>> determine which to defrag. Chances are if the usage is roughly the same
>> week
>> to week the indexes will be roughly the same in terms of fragmentation.
>> So
>> collect samples for a few weeks and base your rebuilding off of those
>> numbers for x many future weeks.
>> --
>> Andrew J. Kelly SQL MVP
>> Solid Quality Mentors
>>
>> "David Hay" <david.hay@.gmail.com> wrote in message
>> news:368f7322-7f34-40c7-8e5e-cd32f1af00aa@.w56g2000hsf.googlegroups.com...
>> >I am working with multiple servers, and numerous databases. I have
>> > some large databases, some tables with 100's of millions of rows.
>> > Some of the DB's are 24/7 so recreating the indexes is not really an
>> > option. I am trying to come up with a "smart" index plan. I have
>> > seen many examples of this, and I have one that does seem to work,
>> > except that the DBCC showcontig seems to take forever! I am using a
>> > variation on the following. The DBCC Showcontig seems to lock the
>> > table, is this true?
>> >
>> > Is there a faster way to get the DBCC fragmentation information?
>> >
>> > Is it really worth the work identifying and defragmenting those
>> > indexes that fall below a threshold or should I just defrag every
>> > index?
>> >
>> > Any other help on a "smart" maint plan for index maint would be
>> > appreciated!
>> >
>> > OS's : w2k Adv Server fully patched, 2003 Server Fully Patched
>> >
>> > I am on SQL 2000, some at SP3a / SP4.
>> >
>> > Sample of code below:
>> >
>> > http://msdn2.microsoft.com/en-us/library/ms175008.aspx
>> >
>> > -- Declare variables
>> > SET NOCOUNT ON;
>> > DECLARE @.tablename varchar(128);
>> > DECLARE @.execstr varchar(255);
>> > DECLARE @.objectid int;
>> > DECLARE @.indexid int;
>> > DECLARE @.frag decimal;
>> > DECLARE @.maxfrag decimal;
>> >
>> > -- Decide on the maximum fragmentation to allow for.
>> > SELECT @.maxfrag = 30.0;
>> >
>> > -- Declare a cursor.
>> > DECLARE tables CURSOR FOR
>> > SELECT TABLE_NAME
>> > FROM INFORMATION_SCHEMA.TABLES
>> > WHERE TABLE_TYPE = 'BASE TABLE';
>> >
>> > -- Create the table.
>> > 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);
>> >
>> > -- Open the cursor.
>> > OPEN tables;
>> >
>> > -- Loop through all the tables in the database.
>> > FETCH NEXT
>> > FROM tables
>> > INTO @.tablename;
>> >
>> > WHILE @.@.FETCH_STATUS = 0
>> > BEGIN;
>> > -- Do the showcontig of all indexes of the table
>> > INSERT INTO #fraglist
>> > EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''')
>> > WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
>> > FETCH NEXT
>> > FROM tables
>> > INTO @.tablename;
>> > END;
>> >
>> > -- Close and deallocate the cursor.
>> > CLOSE tables;
>> > DEALLOCATE tables;
>> >
>> > -- Declare the cursor for the list of indexes to be defragged.
>> > DECLARE indexes CURSOR FOR
>> > SELECT ObjectName, ObjectId, IndexId, LogicalFrag
>> > FROM #fraglist
>> > WHERE LogicalFrag >= @.maxfrag
>> > AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
>> >
>> > -- Open the cursor.
>> > OPEN indexes;
>> >
>> > -- Loop through the indexes.
>> > FETCH NEXT
>> > FROM indexes
>> > INTO @.tablename, @.objectid, @.indexid, @.frag;
>> >
>> > WHILE @.@.FETCH_STATUS = 0
>> > BEGIN;
>> > PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ',
>> > ' + RTRIM(@.indexid) + ') - fragmentation currently '
>> > + RTRIM(CONVERT(varchar(15),@.frag)) + '%';
>> > SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ',
>> > ' + RTRIM(@.indexid) + ')';
>> > EXEC (@.execstr);
>> >
>> > FETCH NEXT
>> > FROM indexes
>> > INTO @.tablename, @.objectid, @.indexid, @.frag;
>> > END;
>> >
>> > -- Close and deallocate the cursor.
>> > CLOSE indexes;
>> > DEALLOCATE indexes;
>> >
>> > -- Delete the temporary table.
>> > DROP TABLE #fraglist;
>> > GO
>>|||Thanks to all for the information. I did run a test in Prod, and it
ran much faster. (Much larger box). I saw a script that loaded a
history table and I'll work on building that.
Thanks again to all responding.
David

Friday, March 9, 2012

index for tables that located in different databases?

When design index for table to speed the joining, where conditions, etc,
any different comparing with tables in the same database in the same server?
and how about the tables in different server?No difference. In the end, SQL Server need to access the data. If that access ban be supported by an
index (to limit number of rows to go though, support sort or grouping etc), then it doesn't make a
difference from there the whole query originated.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"nick" <nick@.discussions.microsoft.com> wrote in message
news:BBA362DF-462E-494C-95A3-2B8A61D60A94@.microsoft.com...
> When design index for table to speed the joining, where conditions, etc,
> any different comparing with tables in the same database in the same server?
> and how about the tables in different server?

index for tables that located in different databases?

When design index for table to speed the joining, where conditions, etc,
any different comparing with tables in the same database in the same server?
and how about the tables in different server?
No difference. In the end, SQL Server need to access the data. If that access ban be supported by an
index (to limit number of rows to go though, support sort or grouping etc), then it doesn't make a
difference from there the whole query originated.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"nick" <nick@.discussions.microsoft.com> wrote in message
news:BBA362DF-462E-494C-95A3-2B8A61D60A94@.microsoft.com...
> When design index for table to speed the joining, where conditions, etc,
> any different comparing with tables in the same database in the same server?
> and how about the tables in different server?

index for tables that located in different databases?

When design index for table to speed the joining, where conditions, etc,
any different comparing with tables in the same database in the same server?
and how about the tables in different server?No difference. In the end, SQL Server need to access the data. If that acces
s ban be supported by an
index (to limit number of rows to go though, support sort or grouping etc),
then it doesn't make a
difference from there the whole query originated.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"nick" <nick@.discussions.microsoft.com> wrote in message
news:BBA362DF-462E-494C-95A3-2B8A61D60A94@.microsoft.com...
> When design index for table to speed the joining, where conditions, etc,
> any different comparing with tables in the same database in the same serve
r?
> and how about the tables in different server?

Wednesday, March 7, 2012

Index Defragmentation

None of the system tables in our databases have a significant number of
pages, and I can't see there would be many scans happening, but in the
interests of completeness should system tables be defragmented if they show
high fragmentation?Hi Ben
I would say yes, they are no different to any other table in that respect
and should be optimized in a similar way. Unfortunately you can't run DBCC
INDEXDEFRAG or DBCC DBREINDEX on system tables, so you will have to hope that
the system has it's own way of keeping these tables in tune.
John
"BenUK" wrote:
> None of the system tables in our databases have a significant number of
> pages, and I can't see there would be many scans happening, but in the
> interests of completeness should system tables be defragmented if they show
> high fragmentation?
>

Index Defragmentation

None of the system tables in our databases have a significant number of
pages, and I can't see there would be many scans happening, but in the
interests of completeness should system tables be defragmented if they show
high fragmentation?Hi Ben
I would say yes, they are no different to any other table in that respect
and should be optimized in a similar way. Unfortunately you can't run DBCC
INDEXDEFRAG or DBCC DBREINDEX on system tables, so you will have to hope tha
t
the system has it's own way of keeping these tables in tune.
John
"BenUK" wrote:

> None of the system tables in our databases have a significant number of
> pages, and I can't see there would be many scans happening, but in the
> interests of completeness should system tables be defragmented if they sho
w
> high fragmentation?
>