Skip to content

How to define composite index order ? #14424

Open

Description

@abcfy2

Here is a sample:

class Nft {
    String token
    String address

    static mapping = {
        token index: 'idx_nft'
        address index: 'idx_nft'
    }
}

The result index is:

psql -c '\d+ nft' db
                                                        Table "public.nft"
 Column  |          Type          | Collation | Nullable |             Default             | Storage  | Stats target | Description 
---------+------------------------+-----------+----------+---------------------------------+----------+--------------+-------------
 id      | bigint                 |           | not null | nextval('nft_id_seq'::regclass) | plain    |              | 
 version | bigint                 |           | not null |                                 | plain    |              | 
 address | character varying(255) |           | not null |                                 | extended |              | 
 token   | character varying(255) |           | not null |                                 | extended |              | 
Indexes:
    "nft_pkey" PRIMARY KEY, btree (id)
    "idx_nft" btree (address, token)
Access method: heap

The composite index is: "idx_nft" btree (address, token).

How do I define a composite index in order (token, address)?

Thanks.

Activity

  1. ppazos commented on Dec 26, 2023

    @ppazos

    I'm looking for composite index definition in grails 5 but can't find full documentation on defining indexes including composite ones. This is quite an important part for the domain that might be overlooked.

  2. borinquenkid commented on Nov 4, 2026

    @borinquenkid
    Member

    Triage note: on the current 8.0.x line, properties that name the same index give a composite index whose columns follow the order the properties are declared in, so your example creates an index on (token, address). I checked this on H2 with both Hibernate 5 and Hibernate 7, and on PostgreSQL 16, MySQL 8 and MariaDB 10.11 with Hibernate 7 (#16847 adds the tests). The column reference now describes it as well.

    If you still see (address, token) on a current version, please tell us which Grails version and database you use. To control the order, declare the properties in the order you want the columns.

    https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions