Skip to main content

Defining Functional Indexes in MySQL with Atlas HCL

Question

How can I define functional indexes in MySQL using Atlas HCL syntax?

Answer

MySQL 8.0.13+ supports functional indexes (also called expression-based indexes), which allow you to index the result of an expression rather than just column values. In Atlas HCL syntax, you can define these using the expr field within an index's on block.

For multi-valued indexing of JSON array columns:

schema.hcl
table "construction" {
schema = schema.public
column "id" {
type = int
}
column "keywords" {
type = json
}
index "construction_keywords" {
on {
expr = "(CAST(keywords AS UNSIGNED ARRAY))"
}
}
}

For more details on index expressions, see the Atlas HCL documentation.