Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails/hibernate Eager Fetching appears to be ignored
    primarykey
    data
    text
    <p>I'm working through some performance issues that appear to be related to repeated calls to the database (several thousand objects loading each with several dozen child objects).</p> <p>The issue appears to be that despite loading the relationships eagerly (and confirming that the objects are actually loaded eagerly by logging the hibernate SQL) they are still fetched from the database each time.</p> <p><strong>My instinct is to ask if there's a way to tell grails/hibernate to use the object in memory instead of going back to the database for it when I'm positive it's already in memory?</strong></p> <p>This may be the wrong question, though.</p> <hr> <p>To give a concrete example of what's happening. My domain class Parent defines:</p> <pre><code>static hasMany = [children: Child] static mapping = {children fetch:'select'} </code></pre> <p>might have been fetch:'eager'... I tried both ways, not sure which generated the log results below, but it's clearly eagerly fetching the data.</p> <p>The first thing I do is:</p> <p>def results = Parent.executeQuery("SELECT distinct p from Parent p WHERE ... complex where clause")</p> <p>Looking at the log, this does not eagerly load the child relationships, but that's not terribly unexpected and not really a killer at this point. Next I iterate across the parents:</p> <pre><code>for(Parent parent : results) { blah blah } </code></pre> <p>Each iteration of the loop shows that hibernate is querying for Parent p by id and appears to be eagerly loading all child relationships. Log snippet:</p> <pre><code>select parent0_.id as id4_4_, parent0_.version as version4_4_, parent0_.date_created as date3_4_4_, child1_.parent_id as parent6_6_, child1_.id as id6_, child1_.child_idx as idx9_6_, child1_.id as id22_1_, child1_.version as version22_1_, ...continues for all fields. </code></pre> <p>Note: I have no idea why it maps the id field twice.</p> <p>Great! It's eagerly loading the child object that I need! Inside the above parent loop, I then iterate across the children:</p> <pre><code>for(Parent parent : results) { for(Child child : parent.children) { blah blah } } </code></pre> <p>And this is where the problem is. Every iteration across the inner loop, another hibernate query is logged loading the object by id. Log snippet:</p> <pre><code>select child0_.id as id22_1_, child0_.version as version22_1_, child0_.parent_id as parent6_22_1_, child0_.sequence_number as sequence8_22_1_, ...and all the rest </code></pre> <p>These unnecessary loads are absolutely killing my performance (at least, this appears to be the bottleneck, I can't be 100% certain) since it's performing thousands of reads. Any thoughts on how to properly use in memory objects or different ways to load the data are very much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload