Reputation: 15385
I have a collection of Long and for each element in the collection, I have to lookup a table in the database and do a select. The problem with this approach is that it just issues those many selects and every time there is a new connection being opened and when I make multiple calls to this method, the connection pool is exhausted too soon!
val allIds = Seq(1,2,3,4....)
for each id in allIds, I do:
db.run(fetchTableRowFromDb(_))
Is there a better way to avoid giving so many select statements?
Upvotes: 0
Views: 152
Reputation: 1108
You can use bulk fetch . Assuming your table class name is "SampleTable" and table name is "sampletable".
val allIds = Seq(1,2,3,4....)
val query = TableQuery[SampleTable].filter(_.id inSet(allIds.toTraversable))
db.run(query)
The above query is similar to
select * from sampletable where id in (1,2,3....);
Upvotes: 1