HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to design index for faster deletes

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
designdeletesfasterforhowindex

Problem

I want to speed up following piece of code

delete from ssn_sdo
where       
           (art_id=@art_id and skl_id=@skl_id and @level=0 )
        or (art_id=@art_id and skl_id=@skl_id and @level=1 and tip=@tip)
        or (art_id=@art_id and skl_id=@skl_id and doc_id = @doc_id)


My server is MS SQL-Server 2005.

art_id,skl_id and doc_id are integers, while tip is varchar(10) type of.

I want to make an index(es) on ssn_sdo table so this delete is going to faster.

What I was considering to do is to make three indexes, each one for every case:

doc_id (ASC), art_id (ASC), skl_id (ASC)  
skl_id (ASC), art_id (ASC), tip (ASC)  
skl_id (ASC), art_id (ASC),


Or is there better way to make one index which will include all three cases.

I am careful with indexes because I do not want to slow down inserts in this table.

Solution

Bit akward, I can't see how to comment but I wanted to ask why your condition isn't:

delete from ssn_sdo
where       
       art_id=@art_id 
       and skl_id=@skl_id
       and (@level = 0 or (@level = 1 and tip=@tip) or doc_id = @doc_id)


Not sure that helps at all, before or after you add any further indexes, but it'd interesting to see the executions plans (actual) and see if any benefit is realised.

Code Snippets

delete from ssn_sdo
where       
       art_id=@art_id 
       and skl_id=@skl_id
       and (@level = 0 or (@level = 1 and tip=@tip) or doc_id = @doc_id)

Context

StackExchange Database Administrators Q#45712, answer score: 8

Revisions (0)

No revisions yet.