postgresql – 以下情况的最佳替代方案是什么?
发布时间:2020-12-13 15:58:17 所属栏目:百科 来源:网络整理
导读:我在 Postgresql数据库中使用 JSONB字段来存储以下文档.我拥有数千份文件.我需要使用此数据创建报告,但搜索速度非常慢. 如果我需要创建一个报告,说明一个月的新用户,我需要通过整个文档来比较用户是在一个月而不在另一个月. 消息文件: [{"recipient":1,"use
我在
Postgresql数据库中使用
JSONB字段来存储以下文档.我拥有数千份文件.我需要使用此数据创建报告,但搜索速度非常慢.
如果我需要创建一个报告,说明一个月的新用户,我需要通过整个文档来比较用户是在一个月而不在另一个月. 消息文件: [{"recipient":1,"user":4,"created_at":"2016-11-10","content":"Duis aliquam convallis nunc.","is_sender_user":true},{"recipient":1,"user":18,"created_at":"2016-12-10","content":"Proin eu mi.","is_sender_user":false},"created_at":"2016-11-20","content":"In hac habitasse platea dictumstm.","user":20,"created_at":"2016-12-14","content":"Donec ut dolor.","user":13,"created_at":"2016-12-06","content":"Nulla mollis molestie lorem. Quisque ut erat. Curabitur gravida nisi at nibh.","is_sender_user":true}] 最好创建一个User表并创建一个JSONB消息字段来存储您的消息.或者它的方式我可以使用JSONB查询创建我的报告? 解决方法
正如Samuil Petrov提到你可以在jsonb字段上创建索引,我建议在created_at和user的月份部分创建索引
create INDEX td002_si3 ON testData002 (substring(doc->>'created',8),(doc->>'user')); 用这个查询 SELECT substring(doc ->> 'created',8) AS m,ARRAY_AGG(DISTINCT doc ->> 'user') AS users FROM testData002 GROUP BY substring(doc ->> 'created',8) 将通过索引扫描为您提供每月用户 GroupAggregate (cost=0.28..381.52 rows=3485 width=50) Group Key: ""substring""((doc ->> 'created'::text),8) -> Index Scan using td002_si3 on testdata002 (cost=0.28..294.28 rows=3500 width=50) 使用生成的测试数据 create table testData002 as select row_number() OVER () as id,jsonb_build_object('created',dt::DATE,'user',(random()*1000)::INT) as doc from generate_series(1,10),generate_series('2016-01-01'::TIMESTAMP,'2016-12-15'::TIMESTAMP,'1 day'::INTERVAL) as dt; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |