Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

My problem is how do I maintain an index on table, weekly we are deleting and inserting bulk amount of data from the table.Because drop and create index taking too long time..

share|improve this question
add comment

3 Answers

You could set up a job to rebuild or reorganize them weekly/daily.

I recommend checking out http://sqlfool.com/2011/06/index-defrag-script-v4-1/

This index rebuilder is highly customizable.

share|improve this answer
add comment

What you want to do is have an ongoing analysis of the fragmentation of the indexes pertaining to your particular table. The below query will show you average fragmentation per index for all indexes in your particular object that have a record_count of 1000 or greater:

select
    ps.index_id,
    i.name,
    ps.index_type_desc,
    ps.alloc_unit_type_desc,
    ps.index_level,
    ps.avg_fragmentation_in_percent,
    case
        when ps.avg_fragmentation_in_percent < 5
            then 'NO ACTION'
        when ps.avg_fragmentation_in_percent >= 5
            and ps.avg_fragmentation_in_percent < 30
            then 'REORGANIZE'
        when ps.avg_fragmentation_in_percent >= 30
            then 'REBUILD'
    end as recommended_action
from sys.dm_db_index_physical_stats
(
    db_id('AdventureWorks2012'), 
    object_id('Person.Address'), 
    default, 
    default, 
    'detailed'
) ps
inner join sys.indexes i
on ps.object_id = i.object_id
and ps.index_id = i.index_id
where ps.record_count >= 1000

You can work this into a logical script that is run on a schedule depending on your environment and how big of a maintenance window you have. A good rule of thumb is that fragmentation of 5% to 30% warrants an index reorganization, and 30% or greater should be rebuilt.

Edit: I've worked in simple CASE logic to help with the rule of thumb calculation.

share|improve this answer
add comment

Also check out Ola Hallengren's excellent database maintanence script at http://ola.hallengren.com/

You can customize it to ignore backups & integrity checks if you dont need them.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.