As this answer explains, a JOIN ON
combined with OR
can't be optimized. I've indeed noticed the horrible performance in a query that I'm trying to write.
To describe my scenario, a header record should be returned along with data from all related item records. An item can be related to a header record based on one of three fields. The following SQL seems syntactically sound but is hideously expensive as it can't optimize the JOIN ON OR:
SELECT
header.a,
header.b,
item.x,
item.y,
item.z
FROM header
LEFT OUTER JOIN item ON item.x = header.a
OR item.y = header.a
OR item.z = header.b;
Note that this was generalised from a query that is a great deal more complex (there are another 6 joins and various filters involved). I expect that the JOIN ON OR
implementation will work, though I can't confirm it as the full table scan that results from the OR
is likely to take several hours to complete. (Bonus question: will the triple condition lead to a single sequential FTS or to three consecutive ones?)
Because of the complexity of the surrounding query I want to avoid the UNION ALL approach suggested on the linked answer. It's not just the fact that I want to avoid that level of repetition but also that the rest of the query, while optimized, is itself quite expensive. Is there an alternative that I'm not seeing?