0

I have a table that looks similar to this:

id uuid
ancestor_ids text[]

I need to perform a query to retrieve the rows where the first element in the ancestor_ids array is any of the input values. The query looks something like this:

select * from table where ancestor_ids[1] in ('multiple', 'input', 'values');

The query is working fine but I'd like to know if adding an index to the ancestor_ids column would improve the performance given the fact I'm not using any special operator. Is it possible to optimize the query at all? (table re-design is not an option)

1 Answer 1

2

If it's always the first element, no need for a GIN index. A standard B-Tree will do:

create index on the_table ( (ancestor_ids[1]) );
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting an error when trying to create this index: ERROR: syntax error at or near ")", is it possible to index specific array positions?
@iamdeit: I missed a closing ) - see my edit
It worked! I didn't know it needed double parenthesis. Thanks!

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.