微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – 以下情况的最佳替代方案是什么?

我在 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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐