Domain classes in Grails have default index on Primary Key and Foreign Key, as they are usually used to join tables. But only these are created by default. If you wish to create a custom index given a field and index name, the syntax is simple. Here is an example:

class Person {
    String firstName, lastName, middleName, nickName
    Date dateOfBirth
    EGender gender
    ECivilStatus civilStatus

    static mapping = {
        dateOfBirth index: 'person_dob_idx'
    }
}    

This will create an index on dateOfBirth, with the name of the index as person_dob_idx. Which is equivalent to

CREATE INDEX person_dob_idx ON person (date_of_birth)

If you wish to create index on multiple fields, just list the fields and use the same index name. For example:

class Person {
    String firstName, lastName, middleName, nickName
    Date dateOfBirth
    EGender gender
    ECivilStatus civilStatus

    static mapping = {
        dateOfBirth index: 'person_dob_idx'
        firstName index: 'person_name_idx'
        lastName index: 'person_name_idx'
    }
}    

This will create an index on firstName and lastName, with the name of the index as person_name_idx. Which is equivalent to:

CREATE INDEX person_name_idx ON person (first_name, last_name)

Comments

comments