4

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 ?

2
  • I don't know the reason, but you can try "fixing" it with USE KEY or FORCE KEY. Commented Jan 31, 2018 at 11:33
  • Please provide EXPLAIN SELECT ... for each query. Commented Feb 11, 2018 at 1:20

3 Answers 3

6

The reason for the different plans is that LEFT JOIN will force the join order of your tables to match the order they appear in your query. Without the left join, the optimizer will choose the join order for you, and in this case it will choose the very small table first. (You can see this in your explain by looking at the order the tables are listed.) Once your join order is switched, the index for X changes to KEY d which must have a much larger data set than the compound key.

To fix this, change your select to SELECT STRAIGHT_JOIN *. This is preferred over USE INDEX so that the optimizer can still choose the best key for table X... You might find a better compound key than a,b,c,d, or if your data in X changes dramatically, one of your other keys may be better after a point.

I have to point out, that you normally can't just switch to a LEFT JOIN. The data returned will usually be different!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. In my case the result set is the same
STRAIGHT_JOIN absolutely blew my mind. You hit the nail on the head, when I EXPLAIN both queries, the join order was backwards resulting in a much much slower query in my case. With the STRAIGHT_JOIN, it followed same order as I had them in the query.
0

A LEFT JOIN is not faster than INNER JOIN. It always depends on the structure of your table whereas the proper key index is applied to that or not. If there you do not use a Dependency Or Index Undoubtedly the Left Join is way faster because that not Scan Complete table. But if the scenario change and both tables depend on a proper cluster index then both the Join provide data as feasible as they can.

Left Join is always faster if you not use a proper indexing any of your tables. Also sometimes it depends on data and data structure because every scenario has their own sufficient Logics.

Post INNER JOIN vs LEFT JOIN For Example this having relative to MsSQL but applied to both MySql and MsSql.

6 Comments

I have a matching index but only the left join profits from it
So both queries provide you the same result or you facing any issue?
They provide the same result set but with different plans and massive difference in execution times
Time difference very because you have n number of operation with dependency.
or you can check your query details by explain keyword followed by your query in mysql.
|
0

Drop INDEX(a) as being redundant with your composite index

Replace INDEX(b) with INDEX(b,d).

Then provide EXPLAIN SELECT ... so we can discuss things further.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.