SQL Clone
SQLServerCentral is supported by Redgate
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: 2340 | Views in the last 30 days: 5
Related Articles

Block altering indexed views.

Altering an indexed view will drop all indexes on it without warning, even if nothing is changed. T...


Script Alter Tables

I want to Generate Script Alter Tables Created


Does "Alter Index All Rebuild"...

Does "Alter Index All Rebuild"...


Index Utilization Summary Report Script

Index Report Scripts ...


Generate Index only scripts for whole DB

scripts indexes database

sql server 2005