0

left join with or condition reduces query performance so what could be alternative of left join or condition

select tableb.columnA, tableb.columnB,tablec.id,tabled.id,tablee.id
from tableA tableA
LEFT JOIN tableb tableb ON ((tableb.columnA = tableA.id)
                         OR (tableb.columnB = tableA.id))

LEFT JOIN tablec tablec ON tablec.Id = tableb.columnA 
LEFT JOIN tabled tabled ON tabled.id = tableb.columnB
LEFT JOIN tablee tablee ON tablee.Id= tableb .id
4
  • 1
    Why involve tablec and tabled, since you don't select any of their columns?
    – jarlh
    Oct 17, 2019 at 6:44
  • Are tableA.id, tableb.columnA and tableb.columbB all indexed? I suspect you may find that they are not and therefore the impact of adding the OR condition is much greater than it should be
    – Martin
    Oct 17, 2019 at 6:46
  • @Martin yes all the columns is indexed Oct 17, 2019 at 7:57
  • @jarlh i need this table data but i forgot to add in this question Oct 17, 2019 at 8:02

1 Answer 1

0

I would start from joining tableb again on columnb in separate join.

On the other joins you can utilize the ISNULL function like so tablec.Id = ISNULL(tableba.columnA, tablebb.columnA).

Depends on what you need and your data you can use UNION/UNION ALL or COALESCE to get data from proper instance of tableb.

And assure that both columns in tableb are coverted by index.

1
  • but i am using tableb columns to other table for left join after tableb left join Oct 17, 2019 at 8:01

Your Answer

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

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