0

In java code I generate thousands of ids, next I need to get those ids in the postgresql database, so far I have this (used createNativeQuery as pseudocode):

            Query q = em.createNativeQuery(
                    "select * from mytable where id in (:ids)"
            ).setParameter("ids", listofIds);

I'm afraid about the IN clause, I fear that there might be a huge number of values, is there another better way to perform this?

1
  • 1
    Either build a sub-query which fetches the ids, or load your records in multiple batches to reduce the amount of data you process with each query. Commented Aug 26, 2020 at 2:29

1 Answer 1

1

I think you can scale to quite large lists by doing

select * from mytable where id = any(cast(:ids as bigint[]))

but you have to format the list of ids as a string in Java first, like

ids = "{" + String.join(",", listofIds) + "}";

Still, there will always be some threshold where the list is just too large for Java or Postgresql to handle, so you'll need to break it up into batches if you want to support arbitrarily large lists.

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.