我有一个带有abc列的表,其中包含unix时间戳(例如13898161481435),我想在日期之间运行select。
执行
效率不高where TO_CHAR(TO_TIMESTAMP(abc / 1000), 'DD/MM/YYYY') > '14/01/2014 00:00:00' and ..;
会转换每条记录。
而是做类似的事情 其中abc> ('14 / 01/2014 00:00:00'tobigint())和abc< ......
但我找不到任何参考,但反过来说。
答案 0 :(得分:18)
答案 1 :(得分:4)
您无需将其转换为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')
答案 2 :(得分: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条记录
最有可能的原因是我会说这个部门。
答案 3 :(得分:1)
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))"