2

What's the difference between using ARRAY[] and '{}'::int[] when it comes to performance? Is one of them better than the other?

I'm using functions which contains queries that use conditions like:

  AND ('{13,57,30,59}'::int[] && _user_profiles)

where is necessary to compare an array (_user_profiles in the example) against a static array ('{13,57,30,59}'::int[]).

This way to compare replies in almost every one of these queries. So what would be better to use?

  1. AND ('{13,57,30,59}'::int[] && _user_profiles)

(OR)

  1. AND (ARRAY[13,57,30,59] && _user_profiles)
1
  • 1
    Just try it explain (analyze) select '{13,57,30,59}'::int[] from generate_series(1,10e6) and explain (analyze) select ARRAY[13,57,30,59] from generate_series(1,10e6) Commented Jan 27, 2020 at 6:50

1 Answer 1

1

Both forms are evaluated at parse time and the result is the same, so there shouldn't be any difference.

To see that for yourself, run both queries with EXPLAIN (VERBOSE) and check for differences in the execution plan.

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

3 Comments

Thanks for taking the time to answer. Is your answer based on any documentation or is it based on your expertise?
Sorry for the lack of explanation. I have added a sentence recommending EXPLAIN to check the execution plan.
Also used the EXPLAIN (ANALYZE) suggested by @a_horse_with_no_name and got this result: imgur.com/a/ee3tYPu So I'll take them as the same in terms of performance.Thank you.

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.