Speed up common lookups
Queries frequently filter or sort by a model field.
Prisma · Const
@@index declares a database index on one or more fields of a Prisma model. Its options and supported behaviors vary by database provider.
Place @@index on a model to declare a database index over one or more fields. The field list can be written positionally or with the fields: name, and optional arguments such as map, where, sort, length, or type depend on the supported database and index configuration.
@@index(_ fields: FieldReference[], map: String?, where: raw(String) | { field: value }?)| Name | Type | Description |
|---|---|---|
fields | FieldReference[] | One or more model field references included in the index; the fields argument name may be omitted. |
map | String? | Optional database index name mapping. |
where | raw(String) | { field: value }? | Optional partial-index predicate, expressed as raw SQL or a type-safe object. |
Declares an index in the database.
model Post {
id Int @id @default(autoincrement())
title String
content String?
@@index([title, content])
}This model declares a database index spanning the title and content fields.
Queries frequently filter or sort by a model field.
A relational query benefits from a combined lookup across fields.
The database requires a provider-specific access method or predicate.
@@index declares an index in the database at the model level. Its field list can contain one field or multiple fields, such as @@index([title, content]).
Follow-up: How would you name the underlying database index? Use the optional map argument to control the database constraint or index name.
The positional form @@index([title, author]) is equivalent to @@index(fields: [title, author]); the fields argument name is optional. Field-level length and sort options are written alongside the relevant field.
Follow-up: How can a MongoDB composite-type field be indexed? Use dot notation such as @@index([address.number]) where supported.
Index configuration varies by database. For example, PostgreSQL supports access methods such as Hash, Gist, Gin, SpGist, and Brin through the type argument, while MySQL supports length configuration for indexed String and Bytes subparts.
Follow-up: What should be checked before using an advanced argument? Verify that the target database provider supports that argument on @@index.
@id@@id@unique@@unique@@fulltextmapwherelength