如何在postgresql中使用gin索引的jsonb列上通过查询运行组



我有一个表,它有一个名为data的列,格式为jsonb。我在这个专栏上创建了一个杜松子酒索引。

我想让postgres在执行groupby子句时使用gin索引。这个查询的正确语法是什么?以下是数据列中的一个示例条目

{"firstname": "Bob", "lastname": "Smith", "home_zip": "11234"}

我已经尝试过了,但当我运行这个查询时,它postgres不使用gin索引。

explain analyze select data#>'{home_zip}',count(*) from contacts group by data#>'{home_zip}'

Gin索引不支持can_order,因此不能用于通过(除了通过加速提供group by的WHERE子句(来加速组。

您可以通过以下表达式索引来加速这一过程:

create index on contacts using btree ((data #> '{home_zip}'))

最新更新