索引
列索引
你可以通过在想建立索引的列上使用 @Index,为该列创建数据库索引。你可以为实体的任意列创建索引。示例:
import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm"
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Index()
@Column()
firstName: string
@Column()
@Index()
lastName: string
}你也可以指定索引名:
import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm"
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Index("name1-idx")
@Column()
firstName: string
@Column()
@Index("name2-idx")
lastName: string
}唯一索引
要创建唯一索引,你需要在索引选项中指定 { unique: true }:
注意:CockroachDB 把唯一索引存储为
UNIQUE约束
import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm"
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Index({ unique: true })
@Column()
firstName: string
@Column()
@Index({ unique: true })
lastName: string
}多列索引
要创建包含多列的索引,你需要把 @Index 放在实体本身,并指定所有应包含在索引中的列属性名。示例:
import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm"
@Entity()
@Index(["firstName", "lastName"])
@Index(["firstName", "middleName", "lastName"], { unique: true })
export class User {
@PrimaryGeneratedColumn()
id: number
@Column()
firstName: string
@Column()
middleName: string
@Column()
lastName: string
}空间索引
MySQL、CockroachDB 和 PostgreSQL(当 PostGIS 可用时)支持空间索引。
要在 MySQL 中为某一列创建空间索引,给使用空间类型(geometry、point、linestring、polygon、multipoint、multilinestring、multipolygon、geometrycollection)的列加上带 spatial: true 的 Index:
@Entity()
export class Thing {
@Column("point")
@Index({ spatial: true })
point: string
}要为某一列创建空间索引,给使用空间类型(geometry、geography)的列加上带 spatial: true 的 Index:
export interface Geometry {
type: "Point"
coordinates: [Number, Number]
}
@Entity()
export class Thing {
@Column("geometry", {
spatialFeatureType: "Point",
srid: 4326,
})
@Index({ spatial: true })
point: Geometry
}并发创建
为了避免在 Postgres 中创建和删除索引时必须获取 ACCESS EXCLUSIVE 锁,你可以使用 CONCURRENTLY 修饰符来创建它们。如果你想使用并发选项,需要在数据源选项中设置 migrationsTransactionMode: none。
当索引上指定了 concurrent 选项时,TypeORM 支持生成带该选项的 SQL。
@Index(["firstName", "middleName", "lastName"], { concurrent: true })更多信息请参阅 Postgres 文档。
索引类型
如果你需要为索引指定自定义类型,可以使用 type 属性。如果设置了 spatial 属性,该字段会被忽略。
@Index({ type: 'hash' })该特性目前仅受 PostgreSQL 支持。
禁用同步
由于各数据库之间存在诸多差异,并且获取已有数据库索引信息并自动同步存在多个问题,TypeORM 不支持某些索引选项和定义(例如 lower、pg_trgm)。这种情况下你应当手动创建索引(例如在迁移中),使用你想要的任何索引签名。要让 TypeORM 在同步过程中忽略这些索引,请在 @Index 装饰器上使用 synchronize: false 选项。
例如,你创建了一个大小写不敏感比较的索引:
CREATE INDEX "POST_NAME_INDEX" ON "post" (lower("name"))之后,你应当为该索引禁用同步,以避免在下次结构同步时它被删除:
@Entity()
@Index("POST_NAME_INDEX", { synchronize: false })
export class Post {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
}Column indexes
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 indexes for any columns of your entity. Example:
import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm"
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Index()
@Column()
firstName: string
@Column()
@Index()
lastName: string
}You can also specify an index name:
import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm"
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Index("name1-idx")
@Column()
firstName: string
@Column()
@Index("name2-idx")
lastName: string
}Unique indexes
To create a unique index you need to specify { unique: true } in the index options:
Note: CockroachDB stores unique indexes as
UNIQUEconstraints
import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm"
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Index({ unique: true })
@Column()
firstName: string
@Column()
@Index({ unique: true })
lastName: string
}Indexes 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:
import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm"
@Entity()
@Index(["firstName", "lastName"])
@Index(["firstName", "middleName", "lastName"], { unique: true })
export class User {
@PrimaryGeneratedColumn()
id: number
@Column()
firstName: string
@Column()
middleName: string
@Column()
lastName: string
}Spatial Indexes
MySQL, CockroachDB and PostgreSQL (when PostGIS is available) supports spatial indexes.
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):
@Entity()
export class Thing {
@Column("point")
@Index({ spatial: true })
point: string
}To create a spatial index on a column add an Index with spatial: true on a column that uses a spatial type (geometry, geography):
export interface Geometry {
type: "Point"
coordinates: [Number, Number]
}
@Entity()
export class Thing {
@Column("geometry", {
spatialFeatureType: "Point",
srid: 4326,
})
@Index({ spatial: true })
point: Geometry
}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 to use the concurrent option, you need to set migrationsTransactionMode: none in your data source options.
TypeORM supports generating SQL with this option when the concurrent option is specified on the index.
@Index(["firstName", "middleName", "lastName"], { concurrent: true })For more information see the Postgres documentation.
Index Type
If you need to specify a custom type for the index, you can use the type property. If the spatial property is set, this field will be ignored.
@Index({ type: 'hash' })This feature is currently supported only for PostgreSQL.
Disabling synchronization
TypeORM does not support some index options and definitions (e.g. lower, pg_trgm) due to many database-specific differences and multiple issues with getting information about existing database indexes and synchronizing them automatically. In such cases you should create the index manually (for example, in the migrations) with any index signature you want. To make TypeORM ignore these indexes during synchronization, use synchronize: false option on the @Index decorator.
For example, you create an index with case-insensitive comparison:
CREATE INDEX "POST_NAME_INDEX" ON "post" (lower("name"))after that, you should disable synchronization for this index to avoid deletion on next schema sync:
@Entity()
@Index("POST_NAME_INDEX", { synchronize: false })
export class Post {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
}