CREATE INDEX
Create an index on a table to improve query performance for lookups, joins, and ordering.Syntax
Description
CREATE INDEX builds an index on one or more columns or expressions of a table. Turso uses B-tree indexes in the same format as SQLite. The query planner automatically uses indexes when they can speed up a query — you do not need to reference an index explicitly in your SQL statements.
Parameters
| Parameter | Description |
|---|---|
UNIQUE | Enforces a uniqueness constraint. Turso rejects any INSERT or UPDATE that would create duplicate values in the indexed columns. |
IF NOT EXISTS | Prevents an error if an index with the same name already exists. The statement is a no-op when the index is present. |
schema-name | The name of the attached database containing the table. Defaults to the main database if omitted. |
index-name | A unique name for the index within the database. |
table-name | The table to index. |
column-or-expr | A column name or an expression to include in the index. Multiple entries are separated by commas. |
ASC / DESC | Sort direction for the indexed column. Default is ASC. |
WHERE filter-expression | An optional filter that creates a partial index. Only rows matching the expression are included in the index. |
Column Indexes
The most common form indexes one or more columns by name.(a, b) can accelerate queries filtering on a alone, but not queries filtering only on b.
UNIQUE Indexes
AUNIQUE index enforces that no two rows contain the same combination of values in the indexed columns. NULL values are considered distinct from each other, so a UNIQUE index permits multiple rows with NULL in the indexed columns.
Partial Indexes
A partial index includes only the rows that satisfy theWHERE clause. Partial indexes are smaller than full indexes and are more efficient for queries that always include the same filter condition.
WHERE clause of a partial index can reference any column of the table and may use operators, literal values, and built-in functions. Subqueries are not allowed.
Expression Indexes
An expression index stores the result of an expression rather than a raw column value. Use expression indexes when queries frequently filter or sort by a computed value.Custom Index Methods
Turso Extension: Custom index methods extend indexing beyond B-trees. This feature is experimental and must be enabled before use.
USING clause to specify an alternative index method.
Full-Text Search with USING fts
The fts index method creates a full-text search index powered by Tantivy. FTS indexes support tokenizer configuration through the WITH clause.
search() function to query it:
Examples
Index for a Common Lookup Pattern
Unique Index to Enforce a Business Rule
Partial Index for a Status Filter
See Also
- DROP INDEX for removing indexes
- CREATE TABLE for inline UNIQUE and PRIMARY KEY constraints
- EXPLAIN for verifying index usage in query plans