# Indexes

## Column indexes[​](#column-indexes "Direct link to 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[​](#unique-indexes "Direct link to Unique indexes")

To create a unique index you need to specify `{ unique: true }` in the index options:

> Note: CockroachDB stores unique indexes as `UNIQUE` constraints

```
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[​](#indexes-with-multiple-columns "Direct link to 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[​](#spatial-indexes "Direct link to 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[​](#concurrent-creation "Direct link to 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](https://www.postgresql.org/docs/current/sql-createindex.html).

## Index Type[​](#index-type "Direct link to 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[​](#disabling-synchronization "Direct link to 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](https://typeorm.io/docs/migrations/why.md)) 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

}
```
