Slow eager loading in Vapor/Fluent

22 views Asked by At

I two models: Child and Parent with simple 1 to 1 relation.

The query code is code similar to this: offset = 0 limit = 2000

Child.query(on: req.db)
.filter(...)
.with(\.$parent)
.range(offset..<offset + limit).all()

I would expect that in this case Fluent would execute a join, However inspecting the db Query I see it queries Child instances first, then makes a second request to collect parents based on the list of parent.ids.

My problem it is extremely slow. Setting limit to 2000 it takes 10 seconds to execute on a postgres database. I double checked that the db access is optimized and in fact a join query for these two related objects with the same limit 2000 takes only milliseconds so it is likely not a database. In the code above if I remove .with statement the code runs much faster.

I setup db logging and see that the delay is happening after the second sql query to bring parents. Does it take 10 seconds to map 2000 child/parent objects ? Maybe I am missing something.

0

There are 0 answers