【发布时间】:2016-07-04 16:59:50
【问题描述】:
使用 Postgres 9.4,我想在 json 列上创建一个索引,在搜索列中的特定键时将使用该索引。
例如,我有一个带有 json 列“动物”的“农场”表。
animals 列有一般格式的 json 对象:
'{"cow": 2, "chicken": 11, "horse": 3}'
我已经尝试了一些索引(分别):
create INDEX animal_index ON farm ((animal ->> 'cow'));create INDEX animal_index ON farm using gin ((animal ->> 'cow'));create INDEX animal_index ON farm using gist ((animal ->> 'cow'));
我想运行如下查询:
SELECT * FROM farm WHERE (animal ->> 'cow') > 3;
并让该查询使用索引。
当我运行这个查询时:
SELECT * FROM farm WHERE (animal ->> 'cow') is null;
那么 (1) 索引有效,但我无法让任何索引为不等式工作。
这样的索引可能吗?
农场表仅包含约 5000 个农场,但其中一些包含 100 只动物,对于我的用例而言,查询时间太长。像这样的索引是我能想到的加快查询速度的唯一方法,但也许还有另一种选择。
【问题讨论】:
标签: sql json postgresql indexing database-design