开发者

Can you apply LIMIT on MySQL before LEFT JOIN 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 all of table_1 b开发者_如何学JAVAefore applying the join, then limiting which can slow the query down massively (with massive tables).


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜