Iterative Ecto queries

Hello everyone, I’ve working recently on Ecto, doing some composed queries and I have crossed with one question regarding the correct way of executing multiple queries. So I have a list of integers that are the ids from some record of an specific table. Do you guys think that iterating the list and obtaining each record may cause performance issues?.

Example:

list = [1,2,3, ... 20000]
query = from q in "table", select: q.name

Enum.map(list, fn id -> Repo.one(from q in query, where: q.id == ^id,))

Just a little gist for you guys to imagine my current situation. Hope I was clear in my issue, just starting to learn Ecto so I’m not sure it this is ok

1 Like

Yes, it will be far more performant to query for many records at once. Repo.all(from(q in query, where: q.id in ^list, select: q.name)).

3 Likes

Thanks, having the IN operator completely simplifies the logic behind it!

1 Like