Can you apply LIMIT on MySQL before LEFT JOIN another?

梦想与她 提交于 2019-11-30 01:45:19

问题


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 all of table_1 before applying the join, then limiting which can slow the query down massively (with massive tables).


回答1:


You can do it by joining on a subquery instead of an actual table. Something like this should work:

SELECT * FROM
    (SELECT * FROM table_1 LIMIT 5) as subq
    LEFT JOIN table_2 ON subq.id = table_2.id WHERE 1


来源:https://stackoverflow.com/questions/7405432/can-you-apply-limit-on-mysql-before-left-join-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!