1

I'm working on Grails. At some point I have to fetch large sets of objects from the db, based on their ids. If I do this id by id, the performance is very bad.

ids.each{
   Myclass.findById( id )
   ...
}

Given that for storing batches there's the useful withTransaction closure, is it possible to do something like that to fetch objects, instead of storing them?

Another idea could be a long HQL query like:

"select * from Myclass where (id = 1) OR ( id = 2) ... OR ( id = n )"

Is this an ok solution?

Thanks!

Mulone
  • 3,433
  • 8
  • 42
  • 66

1 Answers1

2

As noted in this question, HQL supports a better syntax for selecting based on a group of values, which is supported by GORM's find() method. GORM dynamic finders would also support syntax like:

def myObjs = MyClass.findByIdInList(ids)

Similarly, if your operation is read-only, you could use getAll(), which will take advantage of the optimizations Hibernate can make for read-only transations.

Community
  • 1
  • 1
ig0774
  • 33,803
  • 3
  • 52
  • 56