Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's mysql's "BETWEEN" performance over..?

Tags:

mysql

between

Is there any better performance when querying in (particularly) mysql of the following:

SELECT * FROM `table` WHERE `unix_date` BETWEEN 1291736700 AND 1291737300 

over:

SELECT * FROM `table` WHERE `unix_date` >= 1291736700 AND `unix_date` <= 1291737300 

or BETWEEN syntax is just being substituted with the second sql?

like image 458
Daveel Avatar asked Dec 07 '10 23:12

Daveel


People also ask

What is performance schema in MySQL?

The MySQL Performance Schema is a feature for monitoring MySQL Server execution at a low level. The Performance Schema has these characteristics: The Performance Schema provides a way to inspect internal execution of the server at runtime.

Why does MySQL query take so long to execute?

There are a number of things that may cause a query to take longer time to execute: Inefficient query – Use non-indexed columns while lookup or joining, thus MySQL takes longer time to match the condition. Table lock – The table is locked, by global lock or explicit table lock when the query is trying to access it.

Which MySQL version is best?

MySQL 8.0 increases the overall reliability of MySQL because : MySQL 8.0 stores its meta-data into InnoDB, a proven transactional storage engine.


2 Answers

As I recall, there's no difference. But see for yourself if:

explain plan for  SELECT * FROM `table` WHERE `unix_date` BETWEEN 1291736700 AND 1291737300 

and:

explain plan for SELECT * FROM `table` WHERE `unix_date` >= 1291736700 AND `unix_date` <= 1291737300 

produce the same plans.

like image 190
tpdi Avatar answered Sep 27 '22 20:09

tpdi


From the documentation:

  • expr BETWEEN min AND max

If expr is greater than or equal to min and expr is less than or equal to max, BETWEEN returns 1, otherwise it returns 0. This is equivalent to the expression (min <= expr AND expr <= max) if all the arguments are of the same type. Otherwise type conversion takes place according to the rules described in Section 11.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments.

So it really is just syntax sugar.

like image 35
Paolo Bergantino Avatar answered Sep 27 '22 21:09

Paolo Bergantino