martmists
12/06/2023, 12:30 AMval postPoints = wrapAsExpression<Int>(PostTable.slice(PostTable.points.sum()).select { UserTable.id eq PostTable.author })
val usersByPoints = UserTable.selectAll().orderBy(postPoints), SortOrder.DESC).toList()
What's the benefit over doing this as an alternative with the DAO:
val usersByPoints = User.all().sortedByDescending { it.posts.sumOf { it.points } }
Because I was under the impression that the DAO did lazy evaluation and the latter would be more readable and preferredChantal Loncle
12/20/2023, 6:46 PMSELECT
statement (albeit a nested one).
The second approach first executes a parent SELECT
for all()
which is a terminal operation of sorts, since it returns a SizedIterable
. Then one SELECT
statement is executed for every referencing child entity as they are iterated over by sumOf()
.
It comes down to preference on how a user wants to manipulate data and, depending on the underlying database, potentially performance. Does the user want all the data processing to occur on the database-side by aggregating and loading references simultaneously in a single complex query? Or do they want to handle the data processing themselves? And whether the scale of references to lazy load (potentially leading to N additional queries for every parent query) is a concern for the user.