Adds a new index to the table. column_name
can be a single ::Symbol
, or an ::Array
of Symbols.
The index will be named after the table and the column name(s), unless you pass :name
as an option.
Creating a simple index
add_index(:suppliers, :name)
generates:
CREATE INDEX index_suppliers_on_name ON suppliers(name)
Creating a index which already exists
add_index(:suppliers, :name, if_not_exists: true)
generates:
CREATE INDEX IF NOT EXISTS index_suppliers_on_name ON suppliers(name)
Note: Not supported by MySQL
.
Creating a unique index
add_index(:accounts, [:branch_id, :party_id], unique: true)
generates:
CREATE UNIQUE INDEX index_accounts_on_branch_id_and_party_id ON accounts(branch_id, party_id)
Creating a named index
add_index(:accounts, [:branch_id, :party_id], unique: true, name: 'by_branch_party')
generates:
CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
Creating an index with specific key length
add_index(:accounts, :name, name: 'by_name', length: 10)
generates:
CREATE INDEX by_name ON accounts(name(10))
Creating an index with specific key lengths for multiple keys
add_index(:accounts, [:name, :surname], name: 'by_name_surname', length: {name: 10, surname: 15})
generates:
CREATE INDEX by_name_surname ON accounts(name(10), surname(15))
Note: only supported by MySQL
Creating an index with a sort order (desc or asc, asc is the default)
add_index(:accounts, [:branch_id, :party_id, :surname], name: 'by_branch_desc_party', order: {branch_id: :desc, party_id: :asc})
generates:
CREATE INDEX by_branch_desc_party ON accounts(branch_id DESC, party_id ASC, surname)
Note: MySQL
only supports index order from 8.0.1 onwards (earlier versions accepted the syntax but ignored it).
Creating a partial index
add_index(:accounts, [:branch_id, :party_id], unique: true, where: "active")
generates:
CREATE UNIQUE INDEX index_accounts_on_branch_id_and_party_id ON accounts(branch_id, party_id) WHERE active
Note: Partial indexes are only supported for PostgreSQL
and SQLite.
Creating an index that includes additional columns
add_index(:accounts, :branch_id, include: :party_id)
generates:
CREATE INDEX index_accounts_on_branch_id ON accounts USING btree(branch_id) INCLUDE (party_id)
Note: only supported by PostgreSQL
.
Creating an index where NULLs are treated equally
add_index(:people, :last_name, nulls_not_distinct: true)
generates:
CREATE INDEX index_people_on_last_name ON people (last_name) NULLS NOT DISTINCT
Note: only supported by PostgreSQL
version 15.0.0 and greater.
Creating an index with a specific method
add_index(:developers, :name, using: 'btree')
generates:
CREATE INDEX index_developers_on_name ON developers USING btree (name) -- PostgreSQL
CREATE INDEX index_developers_on_name USING btree ON developers (name) -- MySQL
Note: only supported by PostgreSQL
and MySQL
Creating an index with a specific operator class
add_index(:developers, :name, using: 'gist', opclass: :gist_trgm_ops)
add_index(:developers, [:name, :city], using: 'gist', opclass: { city: :gist_trgm_ops })
add_index(:developers, [:name, :city], using: 'gist', opclass: :gist_trgm_ops)
Note: only supported by PostgreSQL
Creating an index with a specific type
add_index(:developers, :name, type: :fulltext)
generates:
CREATE FULLTEXT INDEX index_developers_on_name ON developers (name) -- MySQL
Note: only supported by MySQL
.
Creating an index with a specific algorithm
add_index(:developers, :name, algorithm: :concurrently)
add_index(:developers, :name, algorithm: :inplace)
Note: only supported by PostgreSQL
and MySQL
.
Concurrently adding an index is not supported in a transaction.
For more information see the “Transactional Migrations” section.