Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Checking fragmented indexes

By Gaby Abed,

Here's a simple script for a procedure where you pass a database (and optional schema...else assume DBO) and you get a list of ALTER INDEX...REORGANIZE statements that target any indices fragmented more than 10% which you can change to suit your needs. The ALTER INDEX syntax for 2005/2008 replaces the DBCC INDEXDEFRAG which will soon be deprecated.

Be aware that this script takes quite a while to generate on a database with lots of indices, even with LIMITED being passed to dm_db_index_physical_stats for the sample quality.

If you feel comfortable with this script, you can obviously adapt it to execute each one of those ALTER INDEX statements automatically by reading from the table.




Gaby Abed

Total article views: 2219 | Views in the last 30 days: 14
Related Articles

Script Alter Tables

I want to Generate Script Alter Tables Created


Does "Alter Index All Rebuild"...

Does "Alter Index All Rebuild"...


Stairway to SQL Server Indexes: Level 12, Create Alter Drop

Options and impacts when creating, altering and dropping an index


Generate Index only scripts for whole DB

scripts indexes database


ALTER INDEX online MS SQL 2005

ALTER INDEX online MS SQL 2005

sql server 2005    

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones