SELECT ... WHERE expr1 and expr2 or expr3 or expr4
this query executes in > 3 minutes.
SELECT ... WHERE expr1 and (expr2 or expr3 or expr4)
this terminates instantly.
I would have expected different results, not different performance. Can anybody explain why?
Background: the select left outer joins 7 tables, but they are not very large (< 10k records the biggest one). Here is the query, but I strongly advise that it’s unintelligible because it’s generated from a very old tool on a legacy system and the overall project is very difficult to work with. I am only interested in the question descrived above
The second version requires that
expr1be true for all records. Ifexpr1depends only on a single table, then the DBMS can “push” the predicate “down” and do that filtering before even performing the joins. (Even if it depends on multiple tables, it can at least be pushed down before some of the joins.) This potentially allows many fewer records to be examined, and much better use of indices.