BETWEEN clause versus <= AND >=

前端 未结 8 1319
無奈伤痛
無奈伤痛 2020-12-14 05:54

Is there a performance difference between using a BETWEEN clause or using <= AND >= comparisons?

i.e. these two queries:

SELECT *  
  FROM table           


        
相关标签:
8条回答
  • 2020-12-14 06:50

    There is no performance difference between the two example queries because BETWEEN is simply a shorthand way of expressing an inclusive range comparison. When Oracle parses the BETWEEN condition it will automatically expand out into separate comparison clauses:

    ex.

    SELECT *  
      FROM table
     WHERE column BETWEEN :lower_bound AND :upper_bound  
    

    ...will automatically become:

    SELECT *  
      FROM table
     WHERE :lower_bound <= column
       AND :upper_bound >= column
    
    0 讨论(0)
  • 2020-12-14 06:53

    It may be worth considering the SQL standard for this (although this might not correspond to all implementations, even if it should):

    Format
    
    <between predicate> ::=
      <row value constructor> [ NOT ] BETWEEN
        <row value constructor> AND <row value constructor>
    
    Syntax Rules
    
    [...]
    
    6) "X BETWEEN Y AND Z" is equivalent to "X>=Y AND X<=Z".
    

    Having said so, there is no difference in behaviour, although for complex X, there may be a difference in parsing time, as mentioned by Benoit here

    Found in http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt

    0 讨论(0)
提交回复
热议问题