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)
Thank this author by sharing:
By MUTHUKKUMARAN KALIYAMOORTHY,
Index Defragmentation is one of the most important DBA tasks. This will significantly improve query performance. When you perform any DML operation (INSERT, UPDATE, or DELETE statements) table fragmentation can occur. If you use predefined maintenance plan it will take much server resource and time. Here is a custom stored procedure.
If you want to determine the level of fragmentation, you can use the SYS.DM_DB_INDEX_PHYSICAL_STATS statement. The SYS.DM_DB_INDEX_PHYSICAL_STATS DMV displays fragmentation information for the data and indexes of the specified object.
Here I use 4 conditions :
1. Fragmentation >=30 AND PAGES>1000 then rebuild
2. Fragmentation between 15 to 29 AND PAGES>1000 then reorganize & update statistics
3. If the page level locking is disabled then rebuild
4. If the above conditions are false and statistics are outdated more than 10 days then update the statistics
Before you run the procedure create the tables provided for history propose
Click the following URL Index Architecture By Gail Shaw-->http://www.sqlservercentral.com/articles/Indexing/68439/
Note : This Index Defragmentation script only works for SQL server 2005 and sql server 2008.
You can also get defragmentation script for SQL server 2000 here.
INDEX DEFRAGMENTATION SCRIPT SQL 2000
Index Defragmentation is one of the most important DBA tas...
INDEX DEFRAGMENTATION SCRIPT for 2008 / 2005
I dedicate this article to my senior DBA “Roshan Jo...
Complete script for index defragmentation and updating stats
Script to see how fragmented the indexes are.
This stored procedure is capable of performing defragmentation indices with avg_fragmentation_in_per...