我正在从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"}');
此外,我想知道如何获得良好的性能方法,谢谢。
答案 0 :(得分:14)
SELECT *
FROM stations
WHERE to_date(event->>'date', 'YYYY-MM-DD')
BETWEEN '2014-02-01'
AND '2014-03-01'
AND event->>'station' = 'AA01';