我正在使用PostgreSQL 10.1 jsonb 数据类型并设计以下结构的JSON文档:
{
"guid": "9c36adc1-7fb5-4d5b-83b4-90356a46061a",
"name": "Angela Barton",
"is_active": true,
"company": "Magnafone",
"address": "178 Howard Place, Gulf, Washington, 702",
"registered": "2009-11-07T08:53:22 +08:00",
"latitude": 19.793713,
"longitude": 86.513373,
"timestamp": 2001-09-28 01:00:00,
"tags": [
"enim",
"aliquip",
"qui"
]
}
我需要通过基于 tags 搜索并按 timestamp 排序来检索JSON文档 .
我已阅读这些文档,并说jsonb_path_ops提供了更好的性能:
-
https://www.postgresql.org/docs/current/static/datatype-json.html#JSON-INDEXING
-
https://www.postgresql.org/docs/current/static/gin-builtin-opclasses.html
要索引标签,1 . 举个例子:
CREATE INDEX idxgintags ON api USING GIN ((jdoc -> 'tags'));
但是,该示例使用jsonb_ops但我认为最好使用jsonb_path_ops .
基于我的案例的需求,即能够 search based on tags 和 sorted by timestamp ,在 optimized search performance 方面创建索引的最佳方法是什么?如果专家可以给我创建索引的SQL和一些查询数据的示例,我将不胜感激 .
谢谢!
1 回答
http://dbfiddle.uk/?rdbms=postgres_10&fiddle=cbb2336edf796f9b9458be64c4654394
正如你所看到的,jsonb的一小部分有效的jsonb_ops接近于jsonb_path_ops . 关闭尺寸,关闭时间,仅支持不同的操作员 . 如果你,它会开始有很大不同