Postgresql 9.4 JSONB类型,按日期范围选择

前端之家收集整理的这篇文章主要介绍了Postgresql 9.4 JSONB类型,按日期范围选择前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在从 Mongodb迁移,看看如何使用jsonb类型按范围选择,我每天有~2.880.000记录,我需要按站和日期字段查询数据库,我知道如何按时间范围选择
SELECT * FROM stations WHERE date >= '2014-02-01' AND date <  '2014-03-01 AND station = 'AA01'

但我不知道如何使用JSONB数据类型查询数据库.

CREATE TABLE stations (
  id SERIAL PRIMARY KEY,event JSONB
);

INSERT INTO test(event) VALUES('{"station":"AA01","value":2.31,"date":"2015-01-23 18:02:48.906569865 +0000 UTC"}');
INSERT INTO test(event) VALUES('{"station":"AA02","value":4.1,"date":"2015-01-23 19:02:48.906569865 +0000 UTC"}');

另外,我想知道如何获得良好的性能方法,谢谢.

SELECT *
FROM stations
WHERE to_date(event->>'date','YYYY-MM-DD') 
    BETWEEN '2014-02-01' 
    AND     '2014-03-01'
AND event->>'station' = 'AA01';

猜你在找的Postgre SQL相关文章