I have two tables
Table X: millions or records
|-----|-----|-----|-----|
| a | b | c | d |
|-----|-----|-----|-----|
Table Y: only a few records
|-----|-----|
| e | f |
|-----|-----|
X.d
allows me to join both tables on X.d = Y.e
I have the following indices:
- (X.a)
- (X.b)
- (X.c)
- (X.d)
- (X.a, X.b, X.c, X.d)
- (Y.e)
One of our application was executing the following query, which took ages to run:
SELECT *
FROM X
INNER JOIN Y ON X.d = Y.e
WHERE
X.a in (1, 2, 3)
AND X.b IS NULL
AND X.c in (4, 5 ,6)
AND X.d in (7, 8, 9)
After changing the INNER JOIN
to a LEFT JOIN
, the query was extremely fast:
SELECT *
FROM X
LEFT JOIN Y ON X.d = Y.e
WHERE
X.a in (1, 2, 3)
AND X.b IS NULL
AND X.c in (4, 5 ,6)
AND X.d in (7, 8, 9)
Looking at explain plans for these queries, first query is doing a full scan
when the second is only doing an Index Scan (range)
on my compound index.
I saw other posts on SO but they had different scenarios.
Why such a diffence in the plans ?