MySQL-AND条件 [英] MySQL - AND condition
问题描述
假设我有这样的查询:
SELECT bla WHERE foo LIKE '%bar%' AND boo = 'bar' AND whatvr IN ('foo', 'bar')...
我想知道MySQL在检索结果时是否继续检查所有条件.
例如.如果foo
不是LIKE %bar%
,它将继续检查是否boo = 'bar'
,依此类推吗?
I was wondering if MySQL continues to check all conditions when retrieving results.
For eg. if foo
is not LIKE %bar%
, will it continue to check if boo = 'bar'
, and so on ?
如果我最后放置不太可能成立的条件会更快吗?
Would it be any faster if I put conditions that are less likely to be true at the end?
很抱歉,如果这似乎是一个愚蠢的问题,那么对于SQL来说,我是一个完全菜鸟:)
I'm sorry if this seems to be stupid question, I'm a complete noob when it comes to SQL :)
推荐答案
我认为无法保证是否会短路多个条件,但是...
I don't think there are any guarantees about whether or not multiple conditions will be short-circuited, but...
通常,您应该将查询优化器视为黑匣子,并假设-除非有相反的证据,否则它会正确执行其工作.优化者的工作是确保尽可能高效地检索所请求的数据.如果最有效的计划涉及短路,那就去做;如果没有,那就不会.
In general, you should treat the query optimiser as a black box and assume -- unless you have evidence to the contrary -- that it will do its job properly. The optimiser's job is to ensure that the requested data is retrieved as efficiently as possible. If the most efficient plan involves short-circuiting then it'll do it; if it doesn't then it won't.
(当然,查询优化器并不完美.如果您有证据表明查询没有以最佳方式执行,那么通常值得重新排序和/或重新定义查询以查看是否有任何更改.)
(Of course, query optimisers aren't perfect. If you have evidence that a query isn't being executed optimally then it's often worth re-ordering and/or re-stating the query to see if anything changes.)
这篇关于MySQL-AND条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!