guyeuro
V2EX  ›  问与答

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

  •  
  •   guyeuro · Jun 27, 2017 · 5704 views
    This topic created in 3422 days ago, the information mentioned may be changed or developed.

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

    11 replies  •  2017-06-28 02:40:33 +08:00
    billlee
    billlee
    Jun 27, 2017
    1
    你这是从哪里看来的歪理邪说?
    mkdong
    mkdong
    Jun 27, 2017 via iPhone
    2
    可能…加了索引?
    andrewpsy
    andrewpsy
    Jun 27, 2017
    3
    SQL 水平太差,没看懂 where 句 :<
    yangqi
    yangqi
    Jun 27, 2017
    4
    先把问题表达清楚了。。。
    shiji
    shiji
    Jun 27, 2017
    5
    用 explain 语句解释一下,你就会发现。。。MySQL 并不管你 where 里面的顺序
    stabc
    stabc
    Jun 27, 2017
    6
    问为什么之前先搞清楚是什么。
    msg7086
    msg7086
    Jun 27, 2017
    7
    为何你说最左边的执行最快?根据是什么?
    imherer
    imherer
    Jun 27, 2017
    8
    哪些属于左边?🤣
    msg7086
    msg7086
    Jun 28, 2017
    10
    @guyeuro 整篇文章我就没看到 MySQL 这个词。
    msg7086
    msg7086
    Jun 28, 2017
    11
    节选自标准文档。

    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.

    说是通常可以选择从左到右进行计算,但是具体是否要从左到右计算,是由数据库软件自行决定的。如果数据库认为优先计算其他表达式可以提高计算速度,那么他可以自行选择求值顺序。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   542 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 22:21 · PVG 06:21 · LAX 14:21 · JFK 17:21
    ♥ Do have faith in what you're doing.