简体   繁体   中英

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. I created a gin index on this column.

I would like to make postgres use the gin index when executing a group by clause. 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.

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).

You could accelerate this with an expression index through:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM