简体   繁体   English

日期比较 BETWEEN vs < >

[英]Date comparison BETWEEN vs < >

I have a question about the performance of the BETWEEN keyword vs. using the < and > operators to compare the upper and lower bound dates.我有一个关于 BETWEEN 关键字与使用 < 和 > 运算符比较上限和下限日期的性能的问题。

Example:例子:

WHERE EncDate BETWEEN '2010-04-30' AND GETDATE() - 1

WHERE EncDate > '2010-04-29' AND EncDate < GETDATE()

Both queries above should output the exact same number of rows, however, am I correct in assuming that BETWEEN keyword performs an inclusive (>= or <=) comparison behind the scenes which is not as performant as doing a < or > comparison from the second query?上面的两个查询都应该 output 完全相同的行数,但是,我是否正确假设 BETWEEN 关键字在幕后执行包容性(> = 或 <=)比较,其性能不如从第二个查询?

According to MSDN: BETWEEN根据 MSDN: BETWEEN

BETWEEN returns TRUE if the value of test_expression is greater than or equal to the value of begin_expression and less than or equal to the value of end_expression.如果 test_expression 的值大于或等于 begin_expression 的值且小于或等于 end_expression 的值,则 BETWEEN 返回 TRUE。

So you are right, it is inclusive.所以你是对的,它是包容性的。

Regarding the first part of your question about performance, I believe BETWEEN is just an alias for syntax of using >= and <= .关于性能问题的第一部分,我相信BETWEEN只是使用>=<=语法的别名。 It is just to simplify and make query more readable.这只是为了简化并使查询更具可读性。

My assumption is based on following threads:我的假设基于以下线程:

sql: BETWEEN v1 and v2 sql:在 v1 和 v2 之间

SQL: BETWEEN vs <= and >= SQL:在与 <= 和 >= 之间

These two are not the same!这两个不一样!

WHERE EncDate BETWEEN '2010-04-30' AND GETDATE() - 1

WHERE EncDate > '2010-04-29' AND EncDate < GETDATE()

They will produce the same result set if EncDate is a date with no time component .如果EncDate是没有时间分量的日期,它们将产生相同的结果集。 Otherwise, they are different.否则,它们是不同的。

The general recommendation is to use:一般建议是使用:

WHERE EncDate >= '2010-04-30' AND 
      EncDate < CONVERT(DATE, GETDATE())

This has the same meaning regard of whether or not there is a time component.这对于是否存在时间分量具有相同的含义。

I am pretty sure that from an optimization perspective, these are exactly the same.我很确定从优化的角度来看,这些是完全相同的。 When working with databases, something as minor as comparing times is generally going to have no impact on performance -- the expensive operations are moving the data around.在使用数据库时,比较时间这样的小事情通常不会对性能产生影响——昂贵的操作正在移动数据。

Aaron Bertrand has a very good discussion on using BETWEEN with date/time data types, called What Do Between And The Devil Have In Common . Aaron Bertrand 对使用BETWEEN和日期/时间数据类型进行了很好的讨论,称为What Do Between And The Devil Have In Common

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 日期和以char转换的日期之间的比较 - Comparison between a date and a date converted in char 数据类型为varchar和日期的列之间的时间比较 - Time comparison between colums with datatype varchar and date 比较当前日期和上一个日期并更新目标 - Comparison between the current date and previous date and update the target VS 2005 DateTime Picker日期值和SQL比较 - VS 2005 DateTime Picker date values and SQL Comparison Oracle Joins - 常规语法与 ANSI 语法的比较 - Oracle Joins - Comparison between conventional syntax VS ANSI Syntax groupBy + join 与 window func Spark 之间的性能比较 - Performance comparison between groupBy + join vs window func Spark 时间戳记和日期给出错误的比较ora-01852 - Comparison between Timestamp and date giving error ora-01852 MSSQL varchar和PHP字符串之间的日期比较不起作用-为什么? - Date comparison between MSSQL varchar and PHP string does not work - why? 在 MS Access DB 查询中用于日期比较的运算符之间无法正常工作 - Between operator for date comparison not working properly in MS Access DB query 在mysql中存储日期:Date的各种方法之间的性能比较 - Storing dates in mysql: Performance comparison between various methods for Date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM