Grails multi column indexes

Kimble picture Kimble · Oct 2, 2009 · Viewed 9.1k times · Source

Can someone explain how to define multi column indexes in Grails? The documentation is at best sparse.

This for example does not seem to work at all: http://grails.org/GORM+Index+definitions

I've had some luck with this, but the results seems random at best. Definitions that works in one domain class does not when applied to another (with different names of course). http://www.grails.org/doc/1.1/guide/single.html#5.5.2.6%20Database%20Indices

Some working examples and explanations would be highly appreciated!

Answer

dave picture dave · Apr 22, 2010

The solution that has worked for me for multi-column indexes is:

class ClassName {
    String name
    String description
    String state

    static mapping = {
        name index: 'name_idx'
        description index: 'name_idx'
        state index: 'name_idx'
    }
}

This creates an index called 'name_idx' with the three columns in the index.

Downside: the columns are listed in the index in alphabetical order, not the order that they were entered.