2

Please let me know how to create index for below query.

SELECT * FROM customers
WHERE identifiers @>
      '[{"systemName": "SAP", "systemReference": "33557"}]'
  AND country_code = 'IN';

identifiers is jsonb type and data is as below.

[{"systemName": "ERP", "systemReference": "TEST"}, {"systemName": "FEED", "systemReference": "2733"}, {"systemName": "SAP", "systemReference": "33557"}]

country_code is varchar type.

2 Answers 2

1

Either create a GIN index on identifiers ..

CREATE INDEX customers_identifiers_idx ON customers 
USING GIN(identifiers);

.. or a composite index with identifiers and country_code.

CREATE INDEX customers_country_code_identifiers_idx ON customers 
USING GIN(identifiers,country_code gin_trgm_ops);

The second option will depend on the values distribution of country_code.

Demo: db<>fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

You can create gin index for jsonb typed columns in Postgresql. gin index has built-in operator classes to handle jsonb operators. Learn more about gin index here https://www.postgresql.org/docs/12/gin-intro.html

For varchar types, btree index is good enough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.