1

I have a table like:

CREATE TABLE IF NOT EXISTS my_table (
    id uuid NOT NULL PRIMARY KEY,
    duplicate_ids uuid[] DEFAULT NULL,
);

And my query is:

SELECT * FROM my_table WHERE 'some-uuid'=ANY(duplicate_ids)

Using EXPLAIN ANALYZE and trying lots of different indexes, I am unable to get the above to use an index.

Here's what I've tried (Postgres 12):

CREATE INDEX duplicate_ids_idx ON my_table USING GIN (duplicate_ids);
CREATE INDEX duplicate_ids_idx ON my_table USING GIN (duplicate_ids array_ops);
CREATE INDEX duplicate_ids_idx ON my_table USING BTREE (duplicate_ids);
CREATE INDEX duplicate_ids_idx ON my_table USING BTREE (duplicate_ids array_ops);

I've also ran SET enable_seqscan TO off; before these tests to enforce index usage.

Questions I've read:

Thank you very much for your time.

2
  • 1
    Try where duplicate_ids && array['some_uuid']::uuid[] or where duplicate_ids @> array['some_uuid']::uuid[] Commented Nov 10, 2021 at 16:58
  • Thank you very much! That works great! Commented Nov 10, 2021 at 17:18

1 Answer 1

3

Question was answered by @a_horse_with_no_name

The solution appears to be to use something like:

SELECT * FROM my_table WHERE duplicate_ids && array['some_uuid']::uuid[]
Sign up to request clarification or add additional context in comments.

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.