[英]how can I run a group by query on a jsonb column with a gin index in postgresql?
I have a table with a column named data that is formatted as jsonb.我有一个表,其中有一列名为 data 的格式为 jsonb。 I created a gin index on this column.我在此列上创建了 gin 索引。
I would like to make postgres use the gin index when executing a group by clause.我想让 postgres 在执行 group by 子句时使用 gin 索引。 What is the proper syntax for this query?此查询的正确语法是什么? Here is an example entry from the data column这是来自数据列的示例条目
{"firstname": "Bob", "lastname": "Smith", "home_zip": "11234"}
I have tried this, but it postgres does not use the gin index when I run this query.我试过这个,但是当我运行这个查询时,它 postgres 不使用 gin 索引。
explain analyze select data#>'{home_zip}',count(*) from contacts group by data#>'{home_zip}'
Gin indexes do not support can_order
, so can't be used to accelerate a group by (other than by accelerating the WHERE clause which feeds the GROUP BY). Gin 索引不支持can_order
,因此不能用于加速 group by(除了通过加速为 GROUP BY 提供数据的 WHERE 子句)。
You could accelerate this with an expression index through:您可以通过以下方式使用表达式索引来加速:
create index on contacts btree ((data #> '{home_zip}'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.