CREATE INDEX
CREATE INDEX builds a secondary index on an existing table in the active database and populates it immediately from current rows. DataVo supports four index methods: BTREE for scalar lookups and ranges, HNSW and FLAT for vector similarity search, and COLUMNAR for vectorized analytical aggregations.
Syntax
CREATE INDEX <index_name> ON <table_name> (<column_name> [, <column_name>, ...])
[USING BTREE | HNSW | FLAT | COLUMNAR]
[WITH (<option_name> = <value> [, ...])];DataVo Extension
USING HNSW, USING FLAT, USING COLUMNAR, and the WITH (...) index-tuning clause are DataVo extensions beyond standard SQLite and ANSI SQL.
Parameters
| Clause | Required | Description |
|---|---|---|
<index_name> | Yes | Unique index name for <table_name>. |
ON <table_name> | Yes | Existing table in the active database to index. |
(<column_name>, ...) | Yes | One or more target columns. HNSW and FLAT require exactly one VECTOR(n) column; BTREE and COLUMNAR accept one or more columns. |
USING <method> | No | Index implementation: BTREE (default when omitted), HNSW, FLAT, or COLUMNAR. |
WITH (<opt> = <val>, ...) | No | Key-value tuning options for vector indexes (HNSW and FLAT). Option names ignore case and underscores. |
Syntax Notes in v0.4
CREATE INDEX does not support UNIQUE or IF NOT EXISTS in DataVo v0.4. To enforce uniqueness on a column, declare UNIQUE inside CREATE TABLE.
Index Methods Overview
| Method | Syntax | Target Columns | Best For |
|---|---|---|---|
BTREE (default) | USING BTREE (or omit USING) | Single or composite scalar columns | Equality lookups, range scans, selective join probes, and single-key integer GROUP BY COUNT. |
HNSW | USING HNSW [WITH (...)] | Single VECTOR(n) column | Fast approximate nearest-neighbor (ANN) search on medium and large embedding tables. |
FLAT | USING FLAT [WITH (metric = ...)] | Single VECTOR(n) column | Exact exhaustive vector search on small tables and recall benchmarks. |
COLUMNAR | USING COLUMNAR | Numeric (INT, BIGINT, FLOAT, DOUBLE), BIT, and DATE columns | Vectorized COUNT, SUM, AVG, MIN, and MAX aggregations without decoding full rows. |
Scalar B-Tree Indexes (USING BTREE)
B-Tree is the default index method when USING is omitted. You can index a single scalar column or a composite list of columns:
-- Single-column B-Tree index
CREATE INDEX ix_players_score ON players (score);
-- Explicit USING BTREE on a composite key
CREATE INDEX ix_players_region_score ON players (region, score) USING BTREE;Behavior & Query Acceleration
- Non-
NULLindexing: A B-Tree index stores entries only for rows where all indexed columns are non-NULL. Rows containing aNULLkey receive no index entry, soIS NULLpredicates scan table rows and multipleNULLs may coexist in aUNIQUEcolumn. - When B-Tree accelerates queries:
- Single-column equality predicates (
WHERE score = 500) use an index point lookup. - Selective
INNER JOINprobes on indexed integer keys probe the B-Tree instead of streaming the full table. - Whole-table
GROUP BY <indexed_int_col>withCOUNT(*)orCOUNT(col)streams groups directly in key order from the index. - Secondary B-Tree indexes are not used to satisfy general
ORDER BYclauses or non-countingGROUP BYshapes (see Query Planner).
- Single-column equality predicates (
Vector Indexes (USING HNSW & USING FLAT)
Vector indexes require exactly one VECTOR(n) column (VECTOR(n), VEC2, VEC3, VEC4, FLOAT[n], or REAL[n]). Rows where the vector column is NULL are skipped.
-- Approximate HNSW index for text embeddings
CREATE INDEX ix_documents_emb ON documents (embedding)
USING HNSW WITH (
metric = 'cosine',
m = 16,
ef_construction = 64,
ef_search = 64,
enable_diversity_heuristic = true
);
-- Exact FLAT index for 3D spatial coordinates
CREATE INDEX ix_entities_pos ON world_entities (position)
USING FLAT WITH (metric = 'euclidean');Vector WITH (...) Options
Passing HNSW graph options (m, ef_construction, ef_search, enable_diversity_heuristic) to FLAT or BTREE, passing metric to BTREE, or specifying out-of-range values throws a BindingException and creates no index. Option names ignore case and underscores (for example, efConstruction and ef_construction are equivalent).
| Option | Default | Valid Range | Applies To | Description |
|---|---|---|---|---|
metric | 'cosine' | 'cosine', 'euclidean' (alias 'l2') | HNSW, FLAT | Distance metric persisted in the catalog. Use 'cosine' for normalized embeddings and 'euclidean'/'l2' for spatial positions. |
m | 16 | 2 – 128 | HNSW | Maximum bi-directional links per graph layer (doubled on layer 0). Higher values improve recall at higher memory and build cost. |
ef_construction | 64 | 1 – 4096 | HNSW | Candidate beam width during graph construction. Higher values yield better graph quality at slower build speed. |
ef_search | 64 | 1 – 4096 | HNSW | Minimum candidate beam width during nearest-neighbor search. |
enable_diversity_heuristic | true | true, false | HNSW | Preserves cross-cluster links during neighbor selection; disable only for faster builds on uniform data. |
Filtered Vector Search Behavior
For hybrid queries combining WHERE predicates with ORDER BY embedding <=> @q LIMIT k (or <->), DataVo automatically widens the candidate beam and switches to an exact SIMD scan when graph traversal would cost more than scanning stored vectors, ensuring filtered LIMIT k queries return all k matching rows whenever enough rows qualify.
Columnar Indexes (USING COLUMNAR)
Columnar indexes store selected columns in compact typed column chunks so analytical aggregates avoid reading full row payloads.
CREATE INDEX cx_players_stats ON players (score, id) USING COLUMNAR;Eligibility & Storage Rules
- Allowed column types:
INT,BIGINT,FLOAT,DOUBLE,BIT, andDATEonly. Any other type (such asVARCHAR,GUID, orVECTOR) is rejected atCREATE INDEXtime. - Storage modes:
InMemory: Stores 4–8 bytes per row per indexed column in RAM for SIMD-accelerated scans.Lsm: Persists format-v8 columnar segments inside.sstfiles withmin/max/NULL/NaNzone maps for chunk skipping.- Legacy
Disk: Records the index in the catalog but falls back to row scans.
- Query eligibility: Accelerates
COUNT,SUM,AVG,MIN, andMAXqueries withoutGROUP BYwhoseWHEREclause is anANDof column-to-literal comparisons orIS NULLchecks over indexed columns. Queries fall back cleanly to row scans when:- Running inside an explicit
BEGIN...COMMITtransaction. - Using
GROUP BY. - Filtering on comparisons where precision could differ (such as a fractional literal against an
INTcolumn),BIT/DATEpredicates, orNaNfloats. - Computing
SUMorAVGoverFLOAT/DOUBLEon anLsmtable with an unflushed memtable tail (to preserve deterministic floating-point addition order).
- Running inside an explicit
- Consolidation & scope: Columnar indexes never serve point lookups and do not replace B-Tree indexes. Multiple
USING COLUMNARindexes on the same table are consolidated internally.
Choosing the Right Index
- Use
BTREEfor selectiveWHERE col = ...filters and join keys. - Use
HNSWforVECTOR(n)tables with thousands or millions of rows; useFLATfor small tables or exact recall baselines. - Use
COLUMNARfor dashboard rollups (COUNT,SUM,AVG,MIN,MAX) over numeric columns.
Dropping an Index
Remove any index using DROP INDEX:
DROP INDEX ix_players_score ON players;See Also
- ALTER TABLE & DROP: Dropping indexes (
DROP INDEX ... ON <table>) and altering indexed columns - Vector Search: Embeddings,
<=>/<->distance operators, and hybrid vector queries - Query Planner: How DataVo routes queries to B-Tree, HNSW, FLAT, and Columnar fast paths
- EXPLAIN: Inspecting logical and physical execution plans