跳至正文内容
TursoDB.cn 独立社区 · MVCC 并发写与每 Agent 独立库实战 阅读解析 →
Turso Database SQL 参考 SQL 语句

CREATE INDEX

在一个或多个列或表达式上创建索引,以加速查询

CREATE INDEX

在表上创建索引,以提升查找、连接和排序的查询性能。

语法

CREATE [UNIQUE] INDEX [IF NOT EXISTS] [schema-name.]index-name
    ON table-name (column-or-expr [COLLATE collation-name] [ASC|DESC], ...)
    [WHERE filter-expression];

说明

CREATE INDEX 会在表的一个或多个列或表达式上构建索引。Turso 使用与 SQLite 相同格式的 B-tree 索引。查询规划器会在索引能够加速查询时自动使用索引——你无需在 SQL 语句中显式引用索引。

参数

参数 说明
UNIQUE 强制实施唯一性约束。Turso 会拒绝任何会在索引列中产生重复值的 INSERT 或 UPDATE。
IF NOT EXISTS 如果同名索引已存在,则避免报错。当索引已存在时,该语句不执行任何操作。
schema-name 包含该表的附加数据库名称。省略时默认为主数据库。
index-name 索引在数据库内的唯一名称。
table-name 要建立索引的表。
column-or-expr 要包含在索引中的列名或表达式。多个条目用逗号分隔。
COLLATE collation-name 应用于索引列的可选排序规则序列(例如 NOCASE)。
ASC / DESC 索引列的排序方向。默认为 ASC。
WHERE filter-expression 可选过滤器,用于创建部分索引。只有匹配该表达式的行才会包含在索引中。

列索引

最常见的形式是按名称为一个或多个列建立索引。

-- Single-column index
CREATE INDEX idx_users_email ON users (email);

-- Multi-column (composite) index
CREATE INDEX idx_orders_customer_date ON orders (customer_id, order_date DESC);

当查询按多个列进行过滤或排序时,复合索引很有用。列的顺序很重要——(a, b) 上的索引可以加速仅按 a 过滤的查询,但不能加速仅按 b 过滤的查询。

UNIQUE 索引

UNIQUE 索引强制要求任意两行在索引列中不能包含相同的值组合。NULL 值被视为彼此不同,因此 UNIQUE 索引允许索引列中包含 NULL 的多行存在。

CREATE UNIQUE INDEX idx_users_email_unique ON users (email);

-- This succeeds:
INSERT INTO users (email) VALUES ('alice@example.com');

-- This fails with a UNIQUE constraint violation:
INSERT INTO users (email) VALUES ('alice@example.com');

部分索引

部分索引仅包含满足 WHERE 子句的行。部分索引比完整索引更小,对于始终包含相同过滤条件的查询更高效。

-- Index only active orders
CREATE INDEX idx_active_orders ON orders (customer_id)
    WHERE status = 'active';

-- The query planner uses this index when the WHERE clause matches
SELECT * FROM orders WHERE status = 'active' AND customer_id = 42;

部分索引的 WHERE 子句可以引用表的任意列,并可使用运算符、字面量值和内置函数。不允许使用子查询。

表达式索引

表达式索引存储表达式的结果,而不是原始列值。当查询经常按计算值进行过滤或排序时,请使用表达式索引。

-- Index on lowercase email for case-insensitive lookups
CREATE INDEX idx_users_email_lower ON users (lower(email));

-- The query planner can use this index for:
SELECT * FROM users WHERE lower(email) = 'alice@example.com';
-- Index on an arithmetic expression
CREATE INDEX idx_items_total ON items (quantity * unit_price);

索引中的每个表达式都必须是确定性表达式,并且只能引用被索引表的列。不允许使用聚合函数和子查询。

自定义索引方法

注意

Turso 扩展:自定义索引方法将索引能力扩展到 B-tree 之外。此功能为实验性功能,使用前必须启用。

Turso 支持使用 USING 子句指定替代索引方法。

CREATE INDEX index-name ON table-name USING method-name (columns...);

使用 USING fts 进行全文搜索

fts 索引方法会创建由 Tantivy 提供支持的全文搜索索引。FTS 索引支持通过 WITH 子句配置分词器。

-- Basic FTS index
CREATE INDEX idx_articles_search ON articles USING fts (title, body);

-- FTS index with custom tokenizers
CREATE INDEX idx_articles_search ON articles USING fts (
    title WITH tokenizer=simple,
    body WITH tokenizer=ngram
);

创建 FTS 索引后,使用 search() 函数查询它:

SELECT * FROM articles WHERE search(articles, 'database performance');

示例

为常见查找模式建立索引

CREATE TABLE products (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    category TEXT,
    price REAL,
    in_stock INTEGER DEFAULT 1
);

-- Speed up lookups by category
CREATE INDEX idx_products_category ON products (category);

-- Speed up price range queries within a category
CREATE INDEX idx_products_cat_price ON products (category, price);

使用唯一索引实施业务规则

CREATE TABLE employees (
    id INTEGER PRIMARY KEY,
    email TEXT NOT NULL,
    department TEXT
);

CREATE UNIQUE INDEX idx_employees_email ON employees (email);

为状态过滤建立部分索引

CREATE TABLE tasks (
    id INTEGER PRIMARY KEY,
    assignee TEXT,
    status TEXT DEFAULT 'pending',
    due_date TEXT
);

-- Only index pending tasks -- completed tasks are rarely queried
CREATE INDEX idx_pending_tasks ON tasks (assignee, due_date)
    WHERE status = 'pending';

另请参阅

同步自官方 Turso 文档仓库 ( tursodatabase/turso-docs · MIT 开源协议) 查看官方原文 ↗
搜索
按 Esc 关闭 · 由 Pagefind 静态索引驱动