A database function returns id's of all events within a certain radius, ordered by their distance.
Afterwards, to preserve performance I'm eagerly loading necessary collections in withCriteria
like this:
def events = Event.withCriteria {
'in'('id', ids)
fetchMode("someRelation", FetchMode.JOIN)
// a few more joins
setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
}
However this messes up the ordering. I've noticed that the result of this criteria query returns all events sorted by id. Which does somewhat make sense since in
doesn't guarantee any sort of special ordering (nor does it make any sense that it should). However this poses a bit of a problem, since I want this list to be ordered.
So what I did was this:
List<Event> temp = [];
ids.each { id -> temp << events.find { it.id == id } }
events = temp;
However when the list contains ~2400 elements this piece of code adds around 1 second to total execution time which is something I wish to lower as much as possible.
Is there any other way of doing this which could speed up the process?
order("id", "asc")
to your criteria? – Warmsorder by field
and since I'm also usingdistinct
due to eager fetch joins I can't seem to use the alternative solutions for field ordering. I guess I'll have to come up with something else. – Organic