I've got an app that I'm working on that has a few different
many-to-many collections. There are a couple hundred rows in each of
the tables. When I do a simple "Article.get(1)" in my grails console,
it takes 5 - 10 seconds to return. I turned on the hibernate debug
logging and you can see tons of statements being executed just to
return that one entity.
From looking at the statements, it appears the relationships are lazy,
but grails tries to initialize all of those relationships, even if I'm
not using them. To track down the problem, I created a simple test
app with a Foo and and a Bar object with a many-to-many relationship
between them. I created 1 Foo and 1 Bar and related the Bar to the
Foo. Then, if I execute Foo.get(1) in the grails console, I see these
SQL statements:
select foo0_.id as id0_0_, foo0_.version as version0_0_, foo0_.name as
name0_0_ from foo foo0_ where foo0_.id=?
select bars0_.bars_id as bars1_0_, bars0_.foos_id as foos2_0_ from
foo_bar bars0_ where bars0_.bars_id=?
select bar0_.id as id2_0_, bar0_.version as version2_0_, bar0_.name as
name2_0_ from bar bar0_ where bar0_.id=?
select foos0_.foos_id as foos2_0_, foos0_.bars_id as bars1_0_ from
foo_bar foos0_ where foos0_.foos_id=?
I would expect only the first statement, but based on the other
statements it appears that grails it trying to initialize all lazy
relationships, even though they are not need for my simple statement.