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)
 Reorg/Defrag Script

Author  Topic 

Why9999
Starting Member

10 Posts

Posted - 2011-05-24 : 12:16:10
We have been using sqlfool's defrag/reindex/reorg script. However, it has bogged down in speed as the amount of schema has grown.

This is for a large server and with lots of tables/indexes in several databases and what I have found is that it takes forever just for the script to gather the stats for deciding which tables to defrag based on fragmentation percentage, etc.

Anybody have any recommendations for some good scripts out there?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-05-24 : 12:44:02
The problem isn't with sqlfool's script. The problem is with the DMF that we have to query to get the index information. You can choose to specify LIMITED or SAMPLED if DETAILED is taking too long. I always use SAMPLED.

Here's my custom script, but remember that they all have to query the same DMF: http://weblogs.sqlteam.com/tarad/archive/2009/11/03/DefragmentingRebuilding-Indexes-in-SQL-Server-2005-and-2008Again.aspx

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

Subscribe to my blog
Go to Top of Page

Why9999
Starting Member

10 Posts

Posted - 2011-05-24 : 13:28:52
quote:
Originally posted by tkizer

The problem isn't with sqlfool's script. The problem is with the DMF that we have to query to get the index information. You can choose to specify LIMITED or SAMPLED if DETAILED is taking too long. I always use SAMPLED.

Here's my custom script, but remember that they all have to query the same DMF: http://weblogs.sqlteam.com/tarad/archive/2009/11/03/DefragmentingRebuilding-Indexes-in-SQL-Server-2005-and-2008Again.aspx

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

Subscribe to my blog



Thanks for the tip. I'm trying it with 'sampled' and it's still running after 11 minutes. The other one ran for 2+ hours before I killed it, so we'll see if this shaves off some time. If you have any other ideas, let me know....
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-05-24 : 15:19:20
The DMF takes time, there's no way around it. The bigger your database gets, the longer it'll take to run.

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

Subscribe to my blog
Go to Top of Page

purdyra
Starting Member

7 Posts

Posted - 2011-05-26 : 13:19:13
I am running something very similar I found somewhere? It gets the fragmentation of each index so I can then choose which index needs to be executed. It takes 6-12 minutes. Lately we've been getting some weird errors, like other jobs timing out or server is not responding. I've noticed it's when I happen to be running this script at the same time. Could this be the reason why or is it just a fluke and something else I need to look into?

SELECT left(o.name, 30) as tblName,
left(b.name, 20) as IndexName,
left(avg_fragmentation_in_percent, 10) as avg_fragm_pct,
left(page_count, 10) as PageCount,
left(index_type_desc, 20) as Index_Type_Desc,
left(fragment_count, 10) as fragm_count,
avg_fragment_size_in_pages,
left(a.index_id, 5) as IndexID
FROM sys.dm_db_index_physical_stats (DB_ID(N'i-wireless'), NULL,
NULL, NULL, NULL) AS a
JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id
inner join sys.objects o on b.OBJECT_ID = O.OBJECT_ID
where o.type <> 's'
and index_type_desc <> 'heap'
and page_count > 100
order by avg_fragmentation_in_percent desc
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-05-26 : 13:52:43
The reason why it's so fast is that it's using LIMITED for the DMF. LIMITED is the default. You can choose to specify LIMITED if you run my code, and it too will be fast. But be warned that LIMITED doesn't provide as good of information as SAMPLED and SAMPLED doesn't provide as good of information as DETAILED. LIMITED is the fasted, then SAMPLED, then DETAILED. So the better information you collect, the slower it'll be. That's just the way it works.

So there's nothing special about the code you found. My code and sqlfool's code are querying the same DMF.

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

Subscribe to my blog
Go to Top of Page
   

- Advertisement -