Avoid using STRAIGHT_JOIN
Copyright © 2023 PawSQL
Problem Definition
STRAIGHT_JOIN is a table join method in MySQL that forces joining tables in the order defined, equivalent to an inner join in result. It gives developers some control over how the database executes the SQL query. However, it also loses out on join order optimizations by the optimizer, so should be used carefully based on the scenario. PawSQL provides a risk warning for statements using STRAIGHT_JOIN to avoid potential performance issues caused by it.
Taking the lineitem
and orders
tables in the tpch
database as an example, the following query will join the lineitem
table first and orders
table second directly, indicating the database should take lineitem
as the driving table and orders
as the driven table for the join operation, without optimizing the table order.
SELECT *
FROM lineitem
STRAIGHT_JOIN orders ON lineitem.l_orderkey = orders.o_orderkey;