Can you apply LIMIT on MySQL before LEFT JOIN is another?

For example:

SELECT * FROM table_1 LIMIT 5 LEFT JOIN table_2 AS table_1.id = table_2.id WHERE 1 

Otherwise, the engine takes the entire table_1 before applying the join, and then limits it, which can slow down the query massively (with massive tables).

+8
mysql mysql-management
source share
1 answer

You can do this by joining a subquery, not the actual table. Something like this should work:

 SELECT * FROM (SELECT * FROM table_1 LIMIT 5) as subq LEFT JOIN table_2 AS subq.id = table_2.id WHERE 1 
+15
source share

All Articles