We have such query:
select X.id_x, A.id_a, B.id_b
from X
left join A on 'A|' + A.id_a = X.id_aOrB
left join B on 'B|' + B.id_b = X.id_aOrB
X is linked to A or B and id is computed in the join statement.
- X: 8 000 rows
- A: 36 000 rows
- B: 3 000 rows
This query is very slow, like 10 seconds. No index on X.id_aOrb.
Then using 2 “join” tables, updated by trigger, we don’t need to concat 'A|' + id and 'B|'+id. Results are fetch in less than 1 second. OK.
My question: why is this concat so slow? Is SQL Server no very efficient in ‘+’ when there is too much data?
is not sargable (SQL Server cannot use an index).
If it uses a nested loops join then it must scan
Aas many times as there are rows inX. It is not possible to do an index seek onAto evaluate the'A|' + A.id_a = X.id_aOrBpredicate.If it uses a merge join it must take a copy of the data and sort it first rather than being able to use the order from the index.
The same of course applies to the join on
B