Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails multi column indexes

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!

like image 883
Kimble Avatar asked Oct 02 '09 17:10

Kimble


2 Answers

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.

like image 152
dave Avatar answered Oct 11 '22 12:10

dave


To make your index multi-column, list the columns with comma separator (note, no space after the comma, to avoid this bug. The second URL you point to hits the bug, as it says:

index:'Name_Idx, Address_Index'

with a space; it should work as

index:'Name_Idx,Address_Index'

The first URL you point to was a proposed change (I don't believe it's implemented currently and have no idea how likely it is to ever be).

like image 26
Alex Martelli Avatar answered Oct 11 '22 14:10

Alex Martelli