Is it faster to query by doing greater than or doing multiple equals?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2432

    Is it faster to query by doing greater than or doing multiple equals?

    We have a large table with lots or rows. One of the columns has the values either 0,1,2.

    Is it faster to do a query such as

    WHERE columnid > 0

    or

    WHERE columid=1 OR columid=2
    niheel @ bytes
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    I would guess the first, since the tests would have the same weight, and there's only one of them. I don't know if MySql does short-circuit logic, but even if so, for every "2", you're doing two tests instead of one. Unless there's something strange that I'm not aware of.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      I did a small test and it seems to back up what Curtis said. The > 0 method was consistently 15% - 20% faster. Doing IN(1, 2) was also faster, but not as fast.

      Comment

      • Niheel
        Recognized Expert Moderator Top Contributor
        • Jul 2005
        • 2432

        #4
        Thanks @atli, that's what we ended up going with :) It made world of difference in speed.
        niheel @ bytes

        Comment

        Working...