This article mainly introduces how to use STRAIGHT_JOIN in MySQL, including some performance optimization techniques using STRAIGHT_JOIN. For more information, see
Problem
Using the show full processlist statement, you can easily find the problematic SQL statement, as shown below:
SELECT post.*FROM postINNER JOIN post_tag ON post.id = post_tag.post_idWHERE post.status = 1 AND post_tag.tag_id = 123ORDER BY post.created DESCLIMIT 100
Note: Because post and tag are many-to-many relationships, an association table post_tag exists.
Try to use EXPLAIN to query the SQL execution plan (due to space limitations, the results are deleted ):
+----------+---------+-------+-----------------------------+| table | key | rows | Extra |+----------+---------+-------+-----------------------------+| post_tag | tag_id | 71220 | Using where; Using filesort || post | PRIMARY | 1 | Using where |+----------+---------+-------+-----------------------------+
The following shows the optimized SQL statement. The only change is to change the connection method to "STRAIGHT_JOIN 」:
SELECT post.*FROM postSTRAIGHT_JOIN post_tag ON post.id = post_tag.post_idWHERE post.status = 1 AND post_tag.tag_id = 123ORDER BY post.created DESCLIMIT 100
Try to use EXPLAIN to query the SQL execution plan (due to space limitations, the results are deleted ):
+----------+----------------+--------+-------------+| table | key | rows | Extra |+----------+----------------+--------+-------------+| post | status_created | 119340 | Using where || post_tag | post_id | 1 | Using where |+----------+----------------+--------+-------------+
Compared with the results of the two EXPLAIN statements before and after optimization, the optimized SQL statement is larger than the "rows" statement, but does not have the "Using filesort" statement. In summary, the performance is improved.
Explanation
For the first SQL statement, why does the MySQL Optimizer select a time-consuming execution solution? For the second SQL statement, why is the performance improved after the connection method is changed to STRAIGHT_JOIN?
All this has to begin with the MySQL multi-table connection processing method. First, we need to determine who is driving the table, that is, which table is used as the benchmark. when dealing with such problems, the MySQL Optimizer uses a simple and crude solution: When the result set of a table is small, the table is used as the driving table. this is usually the best choice.
Note: In the EXPLAIN result, the table that appears in the first row is the driver table.
In the example of continuing post to connect to post_tag, the MySQL Optimizer has the following two options:
- Use post as the driving table and use status_created index to filter. The result set contains 119340 rows.
- Use post_tag as the driving table and use the tag_id index to filter the table. The result set contains 71220 rows.
Obviously, the post_tag filtering result set is smaller, so the MySQL Optimizer selects it as the driver table. Sadly, we still need to sort it by the created field in the post table, that is to say, the sorting field is not in the driver table, so "Using filesort" is inevitable, resulting in slow query.
It is easy to optimize it. The top priority is to ensure that the sorting fields are in the driving table. Therefore, post must be the driving table, so "STRAIGHT_JOIN" is the answer, which forces the join order.
...
However, I always think that the non-standard syntax "STRAIGHT_JOIN" belongs to the tricky category and does not need to be used as much as possible. after all, MySQL optimizer can make the right choice in most cases.