Skip to content
Advertisement

Combine 2 GIN indexes with gin_trgm_ops into one | Postgres

I have a flowing table

Columns text_id and customer_unit_id basically store codes like K-2021-8524 and similar.

In a web interface I use search window to filter out entries by text_id or customer_unit_id.

Typical search would be like this

I have created to GIN indexes to facilitate search

I have tried to make 2 column GIN index in order to have one combined index instead of 2 separate – but it doesn’t work (seq scan)

Question – is it possible to have one combined index instead of 2 separate ones fro this exact type of queries???

Thanks….

PostgreSQL version -11

Advertisement

Answer

An alternative would be to create a third column containing a tsvector with the contents of both columns and then index it, e.g.:

And you’d be able to query it like this:

In case you cannot afford an extra column, you can use the tsvector only in the index, but it would make queries a little confusing and it should be also a bit slower:

And query it like this:

Demo: db<>fiddle

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement