DBAtlas · 数据库运维图谱

创建索引

Oracle

CREATE INDEX customer_city_idx ON customer(city) ONLINE;

ONLINE 可在不锁表的情况下创建。唯一索引使用 CREATE UNIQUE INDEX;复合索引使用 (a, b)。

PostgreSQL

CREATE INDEX CONCURRENTLY customer_city_idx ON customer(city);

CONCURRENTLY 不会阻塞表写入,但不能在事务块内运行,且速度较慢。

MySQL

CREATE INDEX customer_city_idx ON customer(city);
-- Lock-free: ... ALGORITHM=INPLACE, LOCK=NONE;

InnoDB 可在线添加大多数索引;大表上使用 LOCK=NONE 允许写入。

SQL Server

CREATE NONCLUSTERED INDEX IX_customer_city
ON dbo.customer (city)
WITH (ONLINE = ON);

ONLINE 是 Enterprise 功能。覆盖列使用 INCLUDE (col1, col2)。

MongoDB

db.customer.createIndex({ city: 1 })
// Unique: { city: 1 }, { unique: true }
// Built in the background without blocking (default behavior in 4.2+)

1 为升序,-1 为降序。在大集合上,先在从节点上创建可减轻负载。

Redis

# Not applicable (core). If RediSearch is present:
# FT.CREATE idx ON HASH PREFIX 1 customer: SCHEMA city TAG

二级索引只能通过 RediSearch 模块获得;核心 Redis 按键访问。

在 DBAtlas 中打开 →