Indexes
Indexes
dbx supports schema-level index declaration. Migration planning and AutoMigrate create missing indexes based on schema metadata.
When to Use
- You want schema-owned index definitions.
- You need both single-column and composite indexes.
Single-Column
type UserSchema struct {
dbx.Schema[User]
Username dbx.Column[User, string] `dbx:"username,index"`
Email dbx.Column[User, string] `dbx:"email,unique"`
}Composite
type UserSchema struct {
dbx.Schema[User]
TenantID dbx.Column[User, int64] `dbx:"tenant_id"`
Username dbx.Column[User, string] `dbx:"username"`
Email dbx.Column[User, string] `dbx:"email"`
ByTenantAndUsername dbx.Index[User] `idx:"columns=tenant_id|username"`
UniqueTenantEmail dbx.Unique[User] `idx:"columns=tenant_id|email"`
}Naming
- primary key:
pk_<table> - non-unique index:
idx_<table>_<columns> - unique index:
ux_<table>_<columns>
Pitfalls
idx:"columns=..."must use column names from schema tags.- Too many write-heavy indexes can reduce insert/update throughput.
Verify
go test ./dbx/... -run Migrate