mysql 的“BETWEEN”是什么?性能超过..?

发布于 2024-10-06 16:51:58 字数 302 浏览 4 评论 0原文

在(特别是)mysql 中查询以下内容时是否有更好的性能:

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

over:

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

或 BETWEEN 语法只是被第二个 sql 替换?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜扑 2024-10-13 16:51:58

我记得,没有什么区别。但请亲自看看是否:

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

和:

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

制定相同的计划。

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.

最后的乘客 2024-10-13 16:51:58

来自文档

  • expr 介于 minmax

如果expr大于或等于minexpr小于或等于max, BETWEEN 返回 1,否则返回 0。 这相当于表达式 (min <= expr AND expr <= max) 如果所有参数的类型相同。 否则,类型转换将根据第 11.2 节“表达式求值中的类型转换”中描述的规则进行,但适用于所有参数三个参数。

所以它实际上只是语法糖。

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.

零時差 2024-10-13 16:51:58

BETWEEN 显示更清晰的意图并且可读性更好(SQL 打算这样做)。

BETWEEN shows clearer intent and reads better (something SQL set out to do).

只是偏爱你 2024-10-13 16:51:58

以下三个语句将产生相同的结果:

  1. i BETWEEN x AND y
  2. x <= i AND i <= y
  3. i >= x AND i <= y

但在配置 3 中,mysql 可能(取决于您的索引)使用不同的索引:顺序似乎很重要,但您应该使用 EXPLAIN 查询进行测试以了解差异。它也可能会根据 MySQL 的版本而变化。

The following three statements will produce the same result:

  1. i BETWEEN x AND y
  2. x <= i AND i <= y
  3. i >= x AND i <= y

But in configuration 3 mysql might (depending on your indexes) use different indexes: order seems to matter but you should be testing with EXPLAIN query to understand the difference. It might also change depending on the version of MySQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文