October 18, 2011 at 9:49 am
I have been tasked with creating a stored procedure to defrag all indexes for our ERP system. I have found a table called sysindexes that lists all indexes for each table; however, it also lists all statistics for each table.
I have two questions. First, how can I differentiate between indexes and statistics in this table? Second, can I defrag the statistics as well as the indexes?
Thank you.
Steve
October 18, 2011 at 9:54 am
Are you on SQL 2000?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 18, 2011 at 9:58 am
statistics can be identified like this, i believe:
select
OBJECT_NAME(id),*
from sysindexes
where first is null --NULL for statsitics
and indid >0 --avoid HEAP indexes
they typically have a name that starts with _WA_Sys_ as well.
Lowell
October 18, 2011 at 10:01 am
GilaMonster: Yes, this is SQL 2000. Sorry for not clarifying.
Lowell: When I run this code, I get no records.
October 18, 2011 at 10:03 am
If you do a DBCC DBREINDEX it will defrag all the indexes on the table.. Why are you trying to go index by index?
CEWII
October 18, 2011 at 10:03 am
Assuming SQL 2K.
INDEXPROPERTY has pretty much all the info you want but I don't have a fully build script for ya.
Gail wants to know if you mistakenly posted in the wrong forum so she can suggest this instead for sql 2K5+ => http://sqlfool.com/2011/06/index-defrag-script-v4-1/
October 18, 2011 at 10:03 am
i ran mine on SQL 2005/2008; the views may report different results between versions, i guess.
how about ....WHERE name like '_WA_Sys_%'
Lowell
October 18, 2011 at 10:03 am
Lowell's code is one way, you can also use the status column. Status is 8388672 for statistics and other values for indexes. Also filter for indid>0
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 18, 2011 at 10:05 am
Lowell (10/18/2011)
i ran mine on SQL 2005/2008; the views may report different results between versions, i guess.how about ....WHERE name like '_WA_Sys_%'
That will only catch the auto-created stats, not any that may have been manually created.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 18, 2011 at 10:23 am
Elliott: We want to use DBCC INDEXDEFRAG because we need to be able to do this online and keep the tables available for the users. It also allows us to cancel the job before it is finished if necessary. DBCC REINDEX does not afford us these luxuries.
GilaMonster: Thank you very much. That is exactly what I was looking for. The numbers must be slightly different between databases because my magic number is 8388704, but your answer allowed me to find it. This will save me a LOT of time.
Thanks all of you for your help.
Steve
October 18, 2011 at 10:58 am
sdownen05 (10/18/2011)
GilaMonster: Thank you very much. That is exactly what I was looking for. The numbers must be slightly different between databases because my magic number is 8388704, but your answer allowed me to find it. This will save me a LOT of time.
I'd suggest rather use the IndexProperty function. I forgot it existed, but there's a property 'IsStatistics' that you can use to find the items that aren't statistics
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 18, 2011 at 11:01 am
GilaMonster (10/18/2011)
sdownen05 (10/18/2011)
GilaMonster: Thank you very much. That is exactly what I was looking for. The numbers must be slightly different between databases because my magic number is 8388704, but your answer allowed me to find it. This will save me a LOT of time.I'd suggest rather use the IndexProperty function. I forgot it existed, but there's a property 'IsStatistics' that you can use to find the items that aren't statistics
+21
where status = q32423047132940832 doesn't say much ;-).
There can be a little, very negligible, performance hit by using indexproperty especially multiple times to get the list of objects to work on but I think it's worth paying it!
October 18, 2011 at 12:00 pm
Thanks again. I will look into all of this and incorporate what I can.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply