【发布时间】:2017-05-08 02:53:12
【问题描述】:
我在我的 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},
{"recipient":1,"user":4,"created_at":"2016-11-20","content":"In hac habitasse platea dictumstm.","is_sender_user":true},
{"recipient":1,"user":20,"created_at":"2016-12-14","content":"Donec ut dolor.","is_sender_user":true},
{"recipient":1,"user":13,"created_at":"2016-12-06","content":"Nulla mollis molestie lorem. Quisque ut erat. Curabitur gravida nisi at nibh.","is_sender_user":true}]
最好创建一个用户表并创建一个 JSONB 消息字段来存储您的消息。或者我可以使用 JSONB 查询创建我的报告?
【问题讨论】:
-
考虑到您的所有字段都在顶层,您可以在整个列上创建 GIN 类型索引 - 这应该会加快速度。 Postgre 还允许您在 jsonb 中的字段上创建索引(例如 field_name -> 'created_at')
标签: postgresql jsonb