PostgreSQLDBA(109)-pgAdmin(Don'tdothis:Don'tuseBETWEEN)-成都快上网建站

PostgreSQLDBA(109)-pgAdmin(Don'tdothis:Don'tuseBETWEEN)

no zuo no die系列,来自于pg的wiki。
这一节的内容是:不要使用between。
理由是:

BETWEEN uses a closed-interval comparison: the values of both ends of the specified range are included in the result.
This is a particular problem with queries of the form
SELECT FROM blah WHERE timestampcol BETWEEN ‘2018-06-01’ AND ‘2018-06-08’;
This will include results where the timestamp is exactly 2018-06-08 00:00:00.000000, but not timestamps later in that same day. So the query might seem to work, but as soon as you get an entry exactly on midnight, you’ll end up double-counting it.
Instead, do:
SELECT FROM blah WHERE timestampcol >= ‘2018-06-01’ AND timestampcol < ‘2018-06-08’

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站制作、成都网站设计、瑞金网络推广、小程序定制开发、瑞金网络营销、瑞金企业策划、瑞金品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供瑞金建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com

原因是between是闭合区间,在处理日期时会丢失精度,比如日期’2018-06-08’会认为是’2018-06-08 00:00:00.000000’而不是’2018-06-08 23:59:59.999999’,下面举例说明。

创建数据表并插入数据

[local]:5432 pg12@testdb=# drop table if exists t_between;
DROP TABLE
Time: 4.715 ms
[local]:5432 pg12@testdb=# create table t_between(id int,tz timestamptz);
CREATE TABLE
Time: 4.788 ms
[local]:5432 pg12@testdb=# 
[local]:5432 pg12@testdb=# insert into t_between values(1,CURRENT_TIMESTAMP);
INSERT 0 1
Time: 3.620 ms
[local]:5432 pg12@testdb=# insert into t_between values(2,now());
INSERT 0 1
Time: 2.319 ms
[local]:5432 pg12@testdb=# insert into t_between values(3,date_trunc('second',CURRENT_TIMESTAMP));
INSERT 0 1
Time: 2.542 ms
[local]:5432 pg12@testdb=# insert into t_between values(4,date_trunc('day',CURRENT_TIMESTAMP));
INSERT 0 1
Time: 2.766 ms
[local]:5432 pg12@testdb=# select * from t_between order by id;
 id |              tz               
----+-------------------------------
  1 | 2019-10-17 11:47:07.876236+08
  2 | 2019-10-17 11:47:07.881309+08
  3 | 2019-10-17 11:47:07+08
  4 | 2019-10-17 00:00:00+08
(4 rows)
Time: 1.760 ms

查询数据

[local]:5432 pg12@testdb=# select * from t_between where tz between'2019-10-16' and '2019-10-17';
 id |           tz           
----+------------------------
  4 | 2019-10-17 00:00:00+08
(1 row)
Time: 1.691 ms
[local]:5432 pg12@testdb=# select * from t_between where tz >= '2019-10-16'  and tz < '2019-10-17';
 id | tz 
----+----
(0 rows)
Time: 1.186 ms
[local]:5432 pg12@testdb=#

用between会把值为2019-10-17 00:00:00+08的数据输出,因此建议使用普通的比较符(>、<、=等)。

参考资料
Don’t Do This


分享标题:PostgreSQLDBA(109)-pgAdmin(Don'tdothis:Don'tuseBETWEEN)
本文网址:http://kswjz.com/article/gjpjjs.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流