0

I have a SQL request that produce a view on my SQL Server that I need to optimize :

SELECT some_stuff 
FROM some_table 
INNER JOIN another_table
       ON some_condition
LEFT OUTER JOIN third_table
       ON third_table.responsetype = another_table.eventcode
WHERE  ( another_table.externalsystemreferenceid <> '' )

From the execution plan in SQL server, it is the LEFT OUTER JOIN that takes much of the time (83%).

My first idea was to put an index on another_table.eventcode (there is already one on third_table.responsetype) but this table is frequently updated and the cost of having to update an index very often is probably not worth it.

So I have two questions :

  • Is there a way to optimize the LEFT OUTER JOIN without index and how ?
  • (A more theorical one to better understand index) Let's say I want to use index without regard to the impact, on which part should I put it and why ?
    • ON third_table.responsetype = another_table.eventcode (on which one? both?) ?
    • or WHERE ( another_table.externalsystemreferenceid <> '' ) ?

Thanks in advance for any response,

2 Answers 2

2

The indexes that you want are:

  • thirdtable(responsetype, . . . ) -- the . . . is for the other referenced columns
  • anothertable(externalsystemreferenceid, eventcode . . )

The second index is probably not helpful, unless almost all the values are blank in anothertable.

You should check to see if the index updates on thirdtable are expensive. If this is an issue, you may need two copies of your database -- one for transaction processing and another for queries such as the one you want to implement.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the reply, just to be clear, by "second index" you mean an index on externalsystemreferenceid ? why would it not be helpful on eventcode ?
@DaveSmith . . . Because most rows probably pass the WHERE condition.
1

You could use a composite index on table another_table

columns  (  eventcode, externalsystemreferenceid ) 

and you should also add to the composite index also the another_table column that are involved in your JOIN another_table ON some_condition

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.