Files
turso/docs/sql-reference/statements/create-index.mdx
Claude 8618d9a555 docs: document NULLS FIRST/LAST in indexes and constraints
Cover the new NULLS FIRST/LAST support in CREATE INDEX and in table
PRIMARY KEY and UNIQUE constraints: syntax, storage placement defaults,
sort elision, the rowid alias and WITHOUT ROWID cases where the clause
has no effect, and the warning that stock SQLite cannot open a schema
that uses it. List it as a Turso extension on the compatibility page
and note that upsert conflict targets still reject it.

Also fix wrong claims that predate this feature: the SELECT page said
the default NULL placement was NULLS LAST for ASC and NULLS FIRST for
DESC (it is the opposite), and the language reference book said NULLS
LAST was a parse error and showed COLLATE after ASC/DESC in ordering
terms (the parser only accepts it before).

Tests: verified every documented example and default against tursodb,
including EXPLAIN QUERY PLAN showing sort elision for matching
placements and a sorter fallback for mismatched ones.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VpGXUdx42eQrq5ZXEqCRNz
2026-08-03 19:27:20 +00:00

215 lines
8.1 KiB
Plaintext

---
title: CREATE INDEX
description: Create an index on one or more columns or expressions to accelerate queries
sidebarTitle: CREATE INDEX
---
# CREATE INDEX
Create an index on a table to improve query performance for lookups, joins, and ordering.
## Syntax
```sql
CREATE [UNIQUE] INDEX [IF NOT EXISTS] [schema-name.]index-name
ON table-name (column-or-expr [COLLATE collation-name] [ASC|DESC] [NULLS FIRST|NULLS LAST], ...)
[WHERE filter-expression];
```
## 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. |
| `COLLATE collation-name` | An optional collation sequence applied to the indexed column (e.g. `NOCASE`). |
| `ASC` / `DESC` | Sort direction for the indexed column. Default is `ASC`. |
| `NULLS FIRST` / `NULLS LAST` | Where NULL values are stored relative to non-NULL values in the index. Defaults to `NULLS FIRST` for `ASC` columns and `NULLS LAST` for `DESC` columns. See [NULL Ordering](#nulls-first-last). |
| `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.
```sql
-- 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 composite index is useful when queries filter or sort by multiple columns. The order of columns matters -- an index on `(a, b)` can accelerate queries filtering on `a` alone, but not queries filtering only on `b`.
## NULL Ordering {#nulls-first-last}
<Info>
**Turso Extension**: SQLite rejects `NULLS FIRST` and `NULLS LAST` in index definitions. Turso supports them fully.
</Info>
Each indexed column may declare where its NULL values are stored relative to non-NULL values. By default an `ASC` column stores NULLs before all values and a `DESC` column stores them after, matching the defaults of `ORDER BY`. `NULLS FIRST` and `NULLS LAST` override that placement without changing how non-NULL values are ordered.
```sql
-- Store rows with a NULL due date at the end of the index
CREATE INDEX idx_tasks_due ON tasks (due_date ASC NULLS LAST);
```
The main benefit is sorting: a query whose `ORDER BY` asks for the same direction and NULL placement as the index (or the exact reverse of both, which the planner satisfies by scanning the index backward) reads rows straight from the index with no sort step.
```sql
-- Both of these use idx_tasks_due without sorting:
SELECT * FROM tasks ORDER BY due_date ASC NULLS LAST;
SELECT * FROM tasks ORDER BY due_date DESC NULLS FIRST;
```
The declared placement also lets range scans and `min()`/`max()` lookups skip over the NULL block no matter which end of the index it lives at.
NULL placement does not affect uniqueness: NULL values are still considered distinct from each other in a `UNIQUE` index.
<Warning>
A database whose schema uses `NULLS FIRST` or `NULLS LAST` in an index, `PRIMARY KEY`, or `UNIQUE` definition cannot be opened by stock SQLite, because SQLite fails to parse the stored schema. Use this feature only for databases that stay on Turso.
</Warning>
## UNIQUE Indexes
A `UNIQUE` 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.
```sql
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');
```
## Partial Indexes
A partial index includes only the rows that satisfy the `WHERE` clause. Partial indexes are smaller than full indexes and are more efficient for queries that always include the same filter condition.
```sql
-- 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;
```
The `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.
```sql
-- 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';
```
```sql
-- Index on an arithmetic expression
CREATE INDEX idx_items_total ON items (quantity * unit_price);
```
Each expression in the index must be a deterministic expression that references only columns of the indexed table. Aggregate functions and subqueries are not allowed.
## Custom Index Methods {#using-method}
<Info>
**Turso Extension**: Custom index methods extend indexing beyond B-trees. This feature is experimental and must be [enabled before use](/docs/sql-reference/experimental-features).
</Info>
Turso supports a `USING` clause to specify an alternative index method.
```sql
CREATE INDEX index-name ON table-name USING method-name (columns...);
```
### 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.
```sql
-- 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
);
```
Once an FTS index exists, use the `search()` function to query it:
```sql
SELECT * FROM articles WHERE search(articles, 'database performance');
```
## Examples
### Index for a Common Lookup Pattern
```sql
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);
```
### Unique Index to Enforce a Business Rule
```sql
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
department TEXT
);
CREATE UNIQUE INDEX idx_employees_email ON employees (email);
```
### Partial Index for a Status Filter
```sql
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';
```
## See Also
- [DROP INDEX](/docs/sql-reference/statements/drop-index) for removing indexes
- [REINDEX](/docs/sql-reference/statements/reindex) for rebuilding indexes
- [CREATE TABLE](/docs/sql-reference/statements/create-table) for inline UNIQUE and PRIMARY KEY constraints
- [EXPLAIN](/docs/sql-reference/statements/explain) for verifying index usage in query plans