Solved

sqlserver 2008 rebuild all indexes based on fragmentation

Posted on 2013-06-11
6
543 Views
Last Modified: 2013-06-18
Hello Everyone,

I have a sql 2008 database that has 400 indexes that over 30 percent fragmented.  Does anyone know of a way to rebuild only the indexes that are over 30 percent fragmented?  

Thanks,

Bill
0
Comment
Question by:bjennings
6 Comments
 
LVL 9

Expert Comment

by:MattSQL
ID: 39239843
There is no inbuilt way to do this with the ALTER INDEX ... REBUILD command so you need to code something.

If you query sys.dm_db_index_physical_stats() with average_fragmentation > 30 then send the results of this query to a table. You can then dynamically create the ALTER INDEX commands from the index names in this table and run the resulting statements...
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39239914
here's some based on fragmentation (the last of these appears to be nicely simple):
http://stackoverflow.com/questions/10495367/automate-index-rebuild-based-on-fragmentation-results

this one doesn't take fragmentation into account, but may be a source of ideas for you, (i.e. the scope is broader than a single db):
http://www.mssqltips.com/sqlservertip/1367/sql-server-script-to-rebuild-all-indexes-for-all-tables-and-all-databases/
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 400 total points
ID: 39239964
Go here and get the script from SQL Server Index and Statistics Maintenance

It then becomes a very simple task of executing the script as follows:
EXECUTE dbo.IndexOptimize
@Databases = 'YourDatabase',
@FragmentationLow = NULL,
@FragmentationMedium = 'INDEX_REORGANIZE,INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE',
@FragmentationHigh = 'INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE',
@FragmentationLevel1 = 5,
@FragmentationLevel2 = 30

Open in new window


Of course some of those parameters are Enterprise Edition only.  But you get the idea.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:bjennings
ID: 39242130
Hello acperkins, Forgive me, I am a DBA novice....I have downloaded the script "MaintenanceSolution"...Do I run the script first or do I save it as a stored procedure.

Thanks,

Bill
0
 
LVL 9

Expert Comment

by:MattSQL
ID: 39242732
That is a great solution. If you run the MaintenanceSolution.sql script it will create the required stored procedures (and other objects) in the master database of the SQL instance.

You can change this easily if you want to create the objects elsewhere by finding the use [master] statement near the top of the script and changing to your desired database.
0
 

Author Closing Comment

by:bjennings
ID: 39257586
It worked great!   Thank you so much!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now