To my understinding it should be simple, does colA='X' or ColB='X', if any one of them is true then return the result otherwise not.
TableA
╔════╦═══╦═══╦═══╦═══╗
║ id ║ A ║ B ║ C ║ D ║
╠════╬═══╬═══╬═══╬═══╣
║ 1 ║ x ║ x ║ x ║ x ║
║ 2 ║ x ║ x ║ x ║ x ║
║ 3 ║ x ║ x ║ x ║ x ║
╚════╩═══╩═══╩═══╩═══╝
TableB
╔════╦═══╗
║ id ║ A ║
╠════╬═══╣
║ 1 ║ x ║
║ 2 ║ x ║
║ 3 ║ x ║
╚════╩═══╝
select T2.ID from tableA T1
JOIN TableB T2
ON T1.A=T2.A OR T1.B=T2.A OR T1.C=T2.A OR T1.D=T2.A /* **takes 6+ min** */
This simple join takes 6+ minutes. If I join using only one field, then it is pretty quick. Also if I write 4 different queries, one for each OR and then Union the result, it is very quick also.
My question is, How does SQL OR Query works? Does it work on current row, does all the ORING and move on to the next or does it do the complete OR of the two tables for each OR. Even then it should take long as seperate queries are fast enough. I out think an internal optimiser can optiise this really quick: I am comparing one column from one table to 4 column of second table. It should be easy to optimise. Why is it taking so long? Is there a way to manually optimize this query. Btw I tried changing = to != or Or to AND but it takes the same amount of time.