Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 index defrag - getting index missing errors

Author  Topic 

bpsintl
Posting Yak Master

132 Posts

Posted - 2009-04-10 : 05:53:11
I have a script that loops through all tables in my db and defrags the indexes. I'm using the following code for this:

USE mydb
DECLARE @TableName sysname
DECLARE @indid int
DECLARE cur_tblfetch CURSOR FOR
SELECT table_name FROM information_schema.tables WHERE table_type = 'base table'
OPEN cur_tblfetch
FETCH NEXT FROM cur_tblfetch INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE cur_indfetch CURSOR FOR
SELECT indid FROM sysindexes WHERE id = OBJECT_ID (@TableName) and keycnt > 0
OPEN cur_indfetch
FETCH NEXT FROM cur_indfetch INTO @indid
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT 'Derfagmenting index_id = ' + convert(char(3), @indid) + 'of the '
+ rtrim(@TableName) + ' table'
IF @indid <> 255 DBCC INDEXDEFRAG (mydb, @TableName, @indid)
FETCH NEXT FROM cur_indfetch INTO @indid
END
CLOSE cur_indfetch
DEALLOCATE cur_indfetch
FETCH NEXT FROM cur_tblfetch INTO @TableName
END
CLOSE cur_tblfetch
DEALLOCATE cur_tblfetch

When I run it I get multiple errors like:

Msg 7999, Level 16, State 41, Line 18
Could not find any index named '_WA_Sys_00000003_3BFFE745' for table 'mytable'.

Any ideas why?

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2009-04-11 : 08:00:48
This looks like an autogenerated statistic - you are not supposed to defrag these stats.Use INDEXPROPERTY() to identify their status


Jack Vamvas
--------------------
http://www.ITjobfeed.com
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-12 : 00:31:40
If you are using SQL Server 2005 (you posted in a 2005 forum), then you should not be using DBCC INDEXDEFRAG. You should be using ALTER INDEX instead. And you might as well not reinvent the wheel and rather just use my script: http://weblogs.sqlteam.com/tarad/archive/2009/03/27/Defragmenting-Indexes-in-SQL-Server-2005Again.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

bpsintl
Posting Yak Master

132 Posts

Posted - 2009-04-14 : 06:30:42
Thanks tkizer
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-14 : 08:19:26
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -