Unofficial Hacker News client - receive Push Notifications on replies.

Why MySQL OR is slower than IN (opens external site)

22 points by millspore 2 days ago | www.percona.com | 14 comments on HN | Vote on HN (opens Hacker News)

View full thread · View on Hacker News

Comment thread (8)

  1. Why does order of the OR operands matter?
    Replies (2)
    1. With a perfect optimizer and perfect knowledge of ordinality and statistics, I think performance would be the same. However in practice, I do not believe it always reorders predicates reliably for optimal performance.

      My experience is that order does matter in MySQL and especially in DWH environments where the optimizer may not be aware of things like partitioning.

      Replies (2)
      1. The hypergraph optimizer orders expensive predicates last (where “expensive” is stuff like subqueries). The old (default) optimizer only does reordering more-or-less by accident, AFAIK.
        Replies (1)
        1. > where “expensive” is stuff like subqueries

          Subqueries aren't always going to be more expensive

          Replies (1)
          1. This is true, but the optimizer considers them so. They're more expensive than e.g. adding two ints.

            Of course, if they're rewritten to a semijoin or similar, they won't be subqueries anymore or indeed not conditions at all, so sorting them is not applicable.

      2. Now I'm really confused. Your orignal comment reads like reordering changes semantics, so the two id= checks cannot be fused since reordering would be required for that. But now it sounds like reordering keeps semantics and is "just" left on the table by a simple optimizer, which isn't really relevant because the original comment is about a hypothetical optimization anyway...
        Replies (1)
        1. It is only about optimization yes. But the way it works today, but it is not guaranteed that collapsing the ORs to INs is optimal when it reorders _other_ predicates.
    2. OR short-circuiting, evaluation stops when one of the statements is true. So it might influence performance?