(Quick Reference)

batchSize

Purpose

Customizes how many results are fetching during lazy loading.

Examples

class Book {
    …
    static mapping = {
        batchSize 10
    }
}

Description

Usage: batchSize(integer)

Given a lazy association where an Author has many Books, GORM will perform one query to get a hold of the Author and then an additional query for each associated Book. This is what is known as the N+1 problem and can often be worked around by using a join query. However, joins can be expensive.

Batch fetching is an optimization of lazy loading so that if, for example, you set a batchSize of 10 and you have an Author that has written 30 books, instead of 31 queries you get 4 (1 to fetch the Author and then three batches of 10):

static mapping = {
    batchSize 10
}

You can also configure batchSize on a per association basis:

class Author {
    static hasMany = [books:Book]
    static mapping = {
        books batchSize: 10
    }
}