mysql 中 where 条件从左到右按顺序执行,为何最左边的执行最快?

2017-06-27 01:06:13 +08:00
 guyeuro

譬如 where price_apple>5 and price_orange>5 price_apple>5 这个条件执行比后面的快 原理是什么?

5034 次点击
所在节点    问与答
11 条回复
billlee
2017-06-27 01:14:57 +08:00
你这是从哪里看来的歪理邪说?
mkdong
2017-06-27 01:23:14 +08:00
可能…加了索引?
andrewpsy
2017-06-27 01:53:19 +08:00
SQL 水平太差,没看懂 where 句 :<
yangqi
2017-06-27 03:01:19 +08:00
先把问题表达清楚了。。。
shiji
2017-06-27 04:39:47 +08:00
用 explain 语句解释一下,你就会发现。。。MySQL 并不管你 where 里面的顺序
stabc
2017-06-27 05:37:25 +08:00
问为什么之前先搞清楚是什么。
msg7086
2017-06-27 05:48:44 +08:00
为何你说最左边的执行最快?根据是什么?
imherer
2017-06-27 11:15:45 +08:00
哪些属于左边?🤣
guyeuro
2017-06-27 12:48:49 +08:00
msg7086
2017-06-28 02:34:56 +08:00
@guyeuro 整篇文章我就没看到 MySQL 这个词。
msg7086
2017-06-28 02:40:33 +08:00
节选自标准文档。

ANSI SQL Draft 2003 5WD-01-Framework-2003-09.pdf

6.3.3.3 Rule evaluation order

...

Where the precedence is not determined by the Formats or by parentheses, effective evaluation of expressions is generally performed from left to right. However, it is implementation-dependent whether expressions are actually evaluated left to right, particularly when operands or operators might cause conditions to be raised or if the results of the expressions can be determined without completely evaluating all parts of the expression.

说是通常可以选择从左到右进行计算,但是具体是否要从左到右计算,是由数据库软件自行决定的。如果数据库认为优先计算其他表达式可以提高计算速度,那么他可以自行选择求值顺序。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/371259

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX