【问题标题】:Convert date to unix timestamp in postgresql在postgresql中将日期转换为unix时间戳
【发布时间】:2014-02-25 00:13:19
【问题描述】:

我有一个表,其中 abc 列带有 unix 时间戳(例如 13898161481435),我想运行日期之间的选择。

这样做是没有效率的

where TO_CHAR(TO_TIMESTAMP(abc / 1000), 'DD/MM/YYYY') > '14/01/2014 00:00:00' and ..;

这将转换每条记录。

宁可做类似
where abc > ('14/01/2014 00:00:00' tobigint()) 和 abc

但我找不到任何参考资料,虽然是相反的情况。

【问题讨论】:

    标签: postgresql date unix-timestamp


    【解决方案1】:

    试试这个

    WHERE abc > extract(epoch from timestamp '2014-01-28 00:00:00')
    

    PostgreSQL Docs

    【讨论】:

    • 您也可以将它用于确切的日期,只需进行简单的修改,例如 select extract(epoch from now())
    【解决方案2】:

    您无需将其转换为 char 即可进行比较。

    WHERE to_timestamp(abc/1000) > timestamp '2014-01-28 00:00:00'
    

    我认为转换不会非常低效,因为时间戳在内部以与 epoch secs 类似的格式存储(诚然具有不同的来源和分辨率)。

    如果你真的想走另一条路:

    WHERE abc > extract(epoch from timestamp '2014-01-28 00:00:00')
    

    【讨论】:

      【解决方案3】:

      有趣的观察,而

      select count(*) from cb.logs where to_timestamp(timestmp/1000) > timestamp '2014-01-15 00:00:00' and to_timestamp(timestmp/1000) < timestamp '2014-01-15 23:59:59';
      

      大约需要 10 秒(我的数据库有 1.5 条磨机记录),下面只需要 1.5 秒

      select count(*) from cb.logs where (timestmp > (select extract(epoch from timestamp '2014-01-15 00:00:00') * 1000) and timestmp < (select extract(epoch from timestamp '2014-01-15 23:59:59') * 1000));
      

      以下约1秒

      select count(*) from cb.logs where (timestmp > extract(epoch from timestamp '2014-01-15 00:00:00') * 1000) and (timestmp < extract(epoch from timestamp '2014-01-15 23:59:59') * 1000);
      

      统计 ~40.000 条记录

      很可能是因为我要说的部门。

      【讨论】:

      • 第一个查询不能使用在timestmp 列上定义的索引,而其他两个可以。检查执行计划。
      • 我和马在一起。任何算术运算都无法产生这种影响。看到这些案例的“解释分析”会很有趣。
      【解决方案4】:

      1

      select count(*) from cb.logs where to_timestamp(timestmp/1000) > timestamp '2014-01-15 00:00:00' and to_timestamp(timestmp/1000) < timestamp '2014-01-15 23:59:59';  
      8600ms
      
      "Aggregate  (cost=225390.52..225390.53 rows=1 width=0)"
      "  ->  Seq Scan on logs  (cost=0.00..225370.34 rows=8073 width=0)"
      "        Filter: ((to_timestamp(((timestmp / 1000))::double precision) > '2014-01-15 00:00:00'::timestamp without time zone) AND (to_timestamp(((timestmp / 1000))::double precision) < '2014-01-15 23:59:59'::timestamp without time zone))"
      

      2

      select count(*) from cb.logs where (timestmp > (select extract(epoch from timestamp '2014-01-15 00:00:00') * 1000) and timestmp < (select extract(epoch from timestamp '2014-01-15 23:59:59') * 1000));
      1199ms
      "Aggregate  (cost=209245.94..209245.95 rows=1 width=0)"
      "  InitPlan 1 (returns $0)"
      "    ->  Result  (cost=0.00..0.01 rows=1 width=0)"
      "  InitPlan 2 (returns $1)"
      "    ->  Result  (cost=0.00..0.01 rows=1 width=0)"
      "  ->  Seq Scan on logs  (cost=0.00..209225.74 rows=8073 width=0)"
      "        Filter: (((timestmp)::double precision > $0) AND ((timestmp)::double precision < $1))"
      

      【讨论】:

        猜你喜欢
        • 2021-04-07
        • 2020-10-19
        • 2020-12-23
        • 2016-04-24
        • 2012-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多