本文介绍了在EXISTS查询中使用LIMIT有什么意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将LIMIT
添加到EXISTS
查询是否对性能有好处,还是MySQL自行应用该限制?
Is there any performance benefit in adding a LIMIT
to an EXISTS
query, or would MySQL apply the limit on its own?
示例:
IF EXISTS (
SELECT 1
FROM my_table
LIMIT 1 -- can this improve performance?
)
THEN ... END IF;
推荐答案
EXISTS()
的目的是只执行查询直到,直到它可以确定是否有任何行与WHERE
子句匹配.也就是说,它在逻辑上与LIMIT 1
做相同的事情. EXISTS
在某些圈子中可能被称为semi-join
.
The purpose of EXISTS()
is to perform the query only until it can decide if there are any rows in that table matching the WHERE
clause. That is, it logically does the same thing as LIMIT 1
. EXISTS
is probably called semi-join
in some circles.
底线:请勿在EXISTS()
内使用LIMIT 1
.
附录:正如Paul所指出的,LIMIT
与以及OFFSET
(或LIMIT m,n
)确实具有含义.
Addenda: As Paul points out, a LIMIT
with an OFFSET
(or LIMIT m,n
) does have meaning.
这篇关于在EXISTS查询中使用LIMIT有什么意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!