EXPLAIN
The EXPLAIN statement displays information about how Turso executes a SQL statement. There are two forms:EXPLAIN shows the virtual machine bytecode, and EXPLAIN QUERY PLAN shows the high-level query execution strategy.
Syntax
| Parameter | Type | Description |
|---|---|---|
| statement | SQL | Any SQL statement (SELECT, INSERT, UPDATE, DELETE, etc.) |
EXPLAIN
TheEXPLAIN prefix causes Turso to return the sequence of virtual machine (VDBE) opcodes that would be used to execute the statement, rather than executing the statement itself.
Output Columns
| Column | Description |
|---|---|
| addr | Instruction address (sequential integer) |
| opcode | The operation name (e.g., OpenRead, SeekRowid, Column, ResultRow) |
| p1 | First operand |
| p2 | Second operand |
| p3 | Third operand |
| p4 | Fourth operand (often a string value like table name or collation) |
| p5 | Fifth operand (flags) |
EXPLAIN QUERY PLAN
TheEXPLAIN QUERY PLAN prefix provides a high-level description of the strategy the query optimizer chose for executing a statement. This output is more useful than raw EXPLAIN for understanding query performance.
Output Columns
| Column | Description |
|---|---|
| id | A unique identifier for this step |
| parent | The id of the parent step (0 for top-level) |
| notused | Reserved for future use (always 0) |
| detail | Human-readable description of the execution step |
Common Detail Messages
| Detail Pattern | Meaning |
|---|---|
SCAN table | Full table scan (no index used) |
SEARCH table USING INDEX idx (col=?) | Index lookup on the specified column |
SEARCH table USING INTEGER PRIMARY KEY (rowid=?) | Direct rowid lookup |
USE TEMP B-TREE FOR ORDER BY | A temporary B-tree is used for sorting |
USE TEMP B-TREE FOR DISTINCT | A temporary B-tree is used for deduplication |
COMPOUND SUBQUERY | Indicates a UNION, INTERSECT, or EXCEPT |
CORRELATED SCALAR SUBQUERY | A correlated subquery that returns a single value |
Examples
Comparing Query Plans
Join Order
Subqueries
See Also
- ANALYZE for collecting statistics that improve query plans
- CREATE INDEX for creating indexes to speed up queries