-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Domain class mapping with fetch: 'join'
on OneToOne association results in n+1 queries
#750
Comments
Hi -- I have isolated this behavior to a problem with unconstrained calls to In contrast, all the I was able to trace the issue down to the override of Line 77 in 85a11a8
Avoiding this method by using an empty criteria seems to workaround the problem, and I now have the following workaround in our plugin:
|
Hi -- Think it might be a relatively straightforward matter to just remove the problematic |
We have been troubleshooting n+1 query behavior resulting from a simple association between two domain objects. We believe this should be prevented by specifying a
fetch: 'join'
mapping on the parent object, but our tests show this mapping having no effect.We are left wondering if we have some ongoing misunderstanding about the documented features in GORM to control fetching of associated objects, or if something has regressed in GORM itself and this essential feature is broken.
We have created a sample application to reproduce: https://github.com/xh/gorm-list-nplus1-bug/tree/nPlusOneWithSingleAssociation - please see the
nPlusOneWithSingleAssociation
branch for the clearest expression of the issue that we could create.Grails 6.0.0
GORM 8.0.0. (via Grails)
Hibernate 5.6.15.Final (via GORM)
Database: H2 (via Grails, although same behavior replicates on MySQL, MSSQL)
Parent class:
Child class:
Within
TestController
we have afindAll
endpoint that effectively does the following:Expected result: SQL logging shows a single query to load all rows from
flight
, with a join tolocation
to include location-based fields for each flight.Actual result: SQL logging shows initial query to fetch all flights without any join, and then n queries to fetch each flight's location, one-by-one. Here are the logs from that call - we bootstrap three flights in total, each with a single location:
That same controller has another
findAllWithFetchArg()
example that attempts to use an explicitfetch
argument tofindAll
, which also does not work as expected/documented:Expected result: SQL logging shows a single query to load all rows from
flight
, with a join tolocation
to include location-based fields for each flight.Actual result: SQL logging shows initial query to fetch all flights with an odd and seemingly useless
inner join location
, but no fields read from location, and then the same n queries to fetch each flight's location, one-by-one:I know there are several gotchas and non-intuitive behaviors around GORM, Hibernate, associations, and n+1 - but this really seems like a bad break from the expected behavior for such a core and straightforward use case.
If anyone can confirm or advise we would appreciate it greatly!
The text was updated successfully, but these errors were encountered: