BETWEEN clause versus <= AND >=

前端 未结 8 1339
無奈伤痛
無奈伤痛 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:28

    Actually it depends on your DBMS engine.

    Some database management systems will compute twice your expression (once for each comparison), and only once when you use BETWEEN.

    Actually if the expression can have a non-deterministic result BETWEEN will have a different behaviour, compare the following in SQLite:

    WHERE RANDOM() BETWEEN x AND y -- one random value generated
    
    WHERE RANDOM() >= x AND RANDOM() <= y -- two distinct random values generated
    

    This can be very time consuming if your expression is (for example) a subquery.

提交回复
热议问题