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

how can I run a group by query on a jsonb column with a gin index in postgresql?

我有一个 table,其中有一个名为 data 的列,其格式为 jsonb。 我在这个专栏上创建了一个 gin 索引。

我想让 postgres 在执行 group by 子句时使用 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 btree ((data #> '{home_zip}'))