Indices
Column indices
You can create a database index for a specific column by using @Index
on a column you want to make an index. You can create indices for any columns of your entity. Example:
You can also specify an index name:
Unique indices
To create an unique index you need to specify { unique: true }
in the index options:
Note: CockroachDB stores unique indices as
UNIQUE
constraints
Indices with multiple columns
To create an index with multiple columns you need to put @Index
on the entity itself and specify all column property names which should be included in the index. Example:
Spatial Indices
MySQL, CockroachDB and PostgreSQL (when PostGIS is available) supports spatial indices.
To create a spatial index on a column in MySQL, add an Index
with spatial: true
on a column that uses a spatial type (geometry
, point
, linestring
, polygon
, multipoint
, multilinestring
, multipolygon
, geometrycollection
):
To create a spatial index on a column add an Index
with spatial: true
on a column that uses a spatial type (geometry
, geography
):
Concurrent creation
In order to avoid having to obtain an access exclusive lock when creating and dropping indexes in postgres, you may create them using the CONCURRENTLY modifier. If you want use the concurrent option, you need set migrationsTransactionMode: none
between data source options.
Typeorm supports generating SQL with this option if when the concurrent option is specified on the index.
For more information see the postgres documentation.
Disabling synchronization
TypeORM does not support some index options and definitions (e.g. lower
, pg_trgm
) because of lot of different database specifics and multiple issues with getting information about exist database indices and synchronizing them automatically. In such cases you should create index manually (for example in the migrations) with any index signature you want. To make TypeORM ignore these indices during synchronization use synchronize: false
option on @Index
decorator.
For example, you create an index with case-insensitive comparison:
after that, you should disable synchronization for this index to avoid deletion on next schema sync:
Last updated