热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PostgreSQLDBA(110)pgAdmin(Don'tdothis:Don'tusetimestampwithouttz)

nozuonodie系列,来自于pg的wiki。这一节的内容是:不要使用timestamp(withouttimezone)。理由是:timestamptzrecordsasing

no zuo no die系列,来自于pg的wiki。
这一节的内容是:不要使用timestamp (without time zone) 。
理由是:

timestamptz records a single moment in time. Despite what the name says it doesn’t store a timestamp, just a point in time described as the number of microseconds since January 1st, 2000 in UTC. You can insert values in any timezone and it’ll store the point in time that value describes. By default it will display times in your current timezone, but you can use at time zone to display it in other time zones.
Because it stores a point in time it will do the right thing with arithmetic involving timestamps entered in different timezones – including between timestamps from the same location on different sides of a daylight savings time change.
timestamp (also known as timestamp without time zone) doesn’t do any of that, it just stores a date and time you give it. You can think of it being a picture of a calendar and a clock rather than a point in time. Without additional information – the timezone – you don’t know what time it records. Because of that, arithmetic between timestamps from different locations or between timestamps from summer and winter may give the wrong answer.
So if what you want to store is a point in time, rather than a picture of a clock, use timestamptz.

原因是timestamp (without time zone)没有记录时区信息,只是当时的时间点(可以理解为当时的时钟快照)。

创建数据表并插入数据

[local]:5432 pg12@testdb=# drop table if exists t_timestamp;
NOTICE: table "t_timestamp" does not exist, skipping
DROP TABLE
Time: 1.594 ms
[local]:5432 pg12@testdb=# create table t_timestamp(id int,time1 timestamptz,time2 timestamp without time zone);
CREATE TABLE
Time: 4.389 ms
[local]:5432 pg12@testdb=# truncate table t_timestamp;
);
insert into t_timestamp values(4,date_trunc('day',CURRENT_TIMESTAMP),date_trunc('day',CURRENT_TIMESTAMP));
select * from t_timestamp;
TRUNCATE TABLE
Time: 2.228 ms
[local]:5432 pg12@testdb=# insert into t_timestamp values(1,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);
INSERT 0 1
Time: 1.355 ms
[local]:5432 pg12@testdb=# insert into t_timestamp values(2,now(),now());
INSERT 0 1
Time: 1.012 ms
[local]:5432 pg12@testdb=# insert into t_timestamp values(3,date_trunc('second',CURRENT_TIMESTAMP),date_trunc('second',CURRENT_TIMESTAMP));
INSERT 0 1
Time: 1.809 ms
[local]:5432 pg12@testdb=# insert into t_timestamp values(4,date_trunc('day',CURRENT_TIMESTAMP),date_trunc('day',CURRENT_TIMESTAMP));
INSERT 0 1
Time: 1.052 ms
[local]:5432 pg12@testdb=#

查询数据

[local]:5432 pg12@testdb=# select * from t_timestamp;
id | time1 | time2
----+-------------------------------+----------------------------
1 | 2019-10-18 12:16:25.333097+08 | 2019-10-18 12:16:25.333097
2 | 2019-10-18 12:16:25.335162+08 | 2019-10-18 12:16:25.335162
3 | 2019-10-18 12:16:25+08 | 2019-10-18 12:16:25
4 | 2019-10-18 00:00:00+08 | 2019-10-18 00:00:00
(4 rows)
Time: 0.794 ms

可以看到,timestamp (without time zone)没有时区信息,只是当时的时间点快照。

参考资料
Don’t Do This


推荐阅读
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
  • 基于词向量计算文本相似度1.测试数据:链接:https:pan.baidu.coms1fXJjcujAmAwTfsuTg2CbWA提取码:f4vx2.实验代码:imp ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • Oracle优化新常态的五大禁止及其性能隐患
    本文介绍了Oracle优化新常态中的五大禁止措施,包括禁止外键、禁止视图、禁止触发器、禁止存储过程和禁止JOB,并分析了这些禁止措施可能带来的性能隐患。文章还讨论了这些禁止措施在C/S架构和B/S架构中的不同应用情况,并提出了解决方案。 ... [详细]
  • 本文介绍了解决Facebook脸书面试题中插入区间的方法,通过模拟遍历的方式判断当前元素与要插入元素的关系,找到插入点并将新区间插入。同时对算法的时间复杂度和空间复杂度进行了分析。 ... [详细]
  • Vue基础一、什么是Vue1.1概念Vue(读音vjuː,类似于view)是一套用于构建用户界面的渐进式JavaScript框架,与其它大型框架不 ... [详细]
  • QuestionThereareatotalofncoursesyouhavetotake,labeledfrom0ton-1.Somecoursesmayhaveprerequi ... [详细]
  • TiDB | TiDB在5A级物流企业核心系统的应用与实践
    TiDB在5A级物流企业核心系统的应用与实践前言一、业务背景科捷物流概况神州金库简介二、现状与挑战神州金库现有技术体系业务挑战应对方案三、TiDB解决方案测试迁移收益问题四、说在最 ... [详细]
  • 我有一个带有H2数据库的springboot应用程序。该应用程序会在启动时引导数据库,为此,我在 ... [详细]
author-avatar
mobiledu2502909027
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有