EXPLAIN
The EXPLAIN command shows the query execution plan that Stoolap will use to execute a query. EXPLAIN ANALYZE additionally shows actual runtime statistics after executing the query.
Basic Syntax
-- Show planned execution strategy
EXPLAIN query;
-- Show plan with actual runtime statistics
EXPLAIN ANALYZE query;
EXPLAIN
Shows the execution plan without running the query.
A SELECT over one table, view, CTE, derived table, VALUES list or table function is planned once, when it is prepared, and EXPLAIN prints that plan: the steps the rows go through, outermost first, each indented under the step it feeds. The access at the bottom is the one the run takes, and the indexes it names are the ones it reads. A SELECT over one of these sources whose WHERE or select list holds a subquery prints its plan the same way, as Subqueries Over One Source shows. Other statements (joins, window functions, DISTINCT or a set operation over anything but a table, an aggregate that reads its rows with no source of its own, EXPLAIN ANALYZE) print the format shown in their own sections below.
Sequential Scan
When no suitable index exists:
EXPLAIN SELECT * FROM users;
Output:
plan
----
Project id, name, email
SeqScan users
Primary Key Lookup
When filtering by primary key, the row is read by its key:
EXPLAIN SELECT * FROM users WHERE id = 1;
Output:
plan
----
Project id, name, email
PkGet users id = 1
else IndexScan users (id = 1)
PkKey
A run whose key is not an integer takes the read after else.
An equality on a column with a unique index of its own reads the one row that index names. The row is still judged by the whole WHERE:
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT UNIQUE);
EXPLAIN SELECT id, name FROM users WHERE email = 'alice@example.com';
Output:
plan
----
Project id, name
Filter
UniqueGet users email = 'alice@example.com'
else IndexScan users (email = 'alice@example.com')
Index unique_users_email (email =)
A run inside a transaction, or one while a commit is publishing to the table, takes the read after else.
An UPDATE or DELETE whose WHERE is the primary key’s equality reads its row the same way, when it has no RETURNING or WITH, keeps no foreign key, sets no primary key, and runs no CHECK on a column it sets. Its SET values show in column order:
EXPLAIN UPDATE users SET email = 'new@example.com' WHERE id = 1;
Output:
plan
----
Update users SET email = 'new@example.com'
PkGet users id = 1
else IndexScan users (id = 1)
PkKey
An UPDATE whose WHERE ANDs the key’s equality with other conditions takes the same plan. The row the key names is written only when the whole WHERE holds for it, which the Filter line shows:
EXPLAIN UPDATE users SET email = 'new@example.com' WHERE id = 1 AND email <> 'new@example.com';
Output:
plan
----
Update users SET email = 'new@example.com'
PkGet users id = 1
Filter: ((id = 1) AND (email <> 'new@example.com'))
else IndexScan users ((id = 1) AND (email <> 'new@example.com'))
PkKey
Any other UPDATE or DELETE shows its clauses:
EXPLAIN UPDATE users SET name = 'x' WHERE email = 'a';
Output:
plan
----
UPDATE users
Set: 1 column(s)
Filter: (email = 'a')
Index Scan
When using an index for equality, the scan line shows the condition every row it reads is judged by, and the line under it the index that finds the rows:
CREATE INDEX idx_category ON products(category);
EXPLAIN SELECT * FROM products WHERE category = 'Electronics';
Output:
plan
----
Project id, name, category, value
IndexScan products (category = 'Electronics')
Index idx_category (category =)
Index Range Scan
When using an index for range queries:
CREATE INDEX idx_value ON products(value);
EXPLAIN SELECT * FROM products WHERE value > 100;
Output:
plan
----
Project id, name, category, value
IndexScan products (value > 100)
Index idx_value (value range)
Several Indexes
Conditions on several indexed columns joined by AND read the rows every index names; joined by OR, the rows any index names. An OR with a branch no index answers is scanned:
EXPLAIN SELECT name FROM products WHERE category = 'Electronics' AND value > 15;
EXPLAIN SELECT * FROM products WHERE category = 'Books' OR value > 25;
Output:
plan
----
Project name
IndexScan products ((category = 'Electronics') AND (value > 15))
Intersect
Index idx_category (category =)
Index idx_value (value range)
plan
----
Project id, name, category, value
IndexScan products ((category = 'Books') OR (value > 25))
Union
Index idx_category (category =)
Index idx_value (value range)
An OR whose operands an index reads only some of is read by every row, unless a LIMIT stops the read: then the indexed operands are read first, and a scan finds the rest only if they do not fill it. The plan shows both:
EXPLAIN SELECT * FROM products WHERE category = 'Books' OR name = 'Pen' LIMIT 1;
Output:
plan
----
Limit 1
Project id, name, category, value
IndexScan products ((category = 'Books') OR (name = 'Pen')) to a LIMIT
MixedOr
Index idx_category (category =)
Scan
else SeqScan products ((category = 'Books') OR (name = 'Pen'))
A multi-column index shows as CompositeIndex name (a =, b range): equalities on its leading columns and a range, with an upper bound, on the next one.
Aggregates Over One Table
COUNT, SUM, AVG, MIN and MAX over a whole table, with no WHERE or GROUP BY, are answered from what the table keeps without reading its rows: its row count, an index’s count of distinct values, a column’s sum and bounds (Stats). A single MIN or MAX of a column with a B-tree index, or of the primary key, reads the index’s first or last key (IndexBound). A run the table cannot answer that way takes the line after else, down to reading every row:
EXPLAIN SELECT COUNT(*), SUM(value) FROM products;
EXPLAIN SELECT MAX(id) FROM products;
Output:
plan
----
Aggregate COUNT(*), SUM(value)
Stats products
else SeqScan products
plan
----
Aggregate MAX(id)
IndexBound products (id)
else Stats products
else SeqScan products
With a WHERE, the table computes the calls over the rows the WHERE passes (StorageFiltered) when its volumes can; otherwise the rows are read by the scan after else, through the indexes the WHERE can use:
EXPLAIN SELECT SUM(value) FROM products WHERE value > 15;
Output:
plan
----
Aggregate SUM(value)
StorageFiltered products (value > 15)
else IndexScan products (value > 15)
Index idx_value (value range)
Aggregation
A GROUP BY of one column with no WHERE shows its groups after by. The table computes the groups itself when it can (StorageGrouped); groups read in the order of the column’s index, each by its rows, stop once a LIMIT’s groups pass the HAVING (SortedStream); otherwise every row is read and grouped. A HAVING naming no column but the group’s judges the groups the table computes when the index walk does not take it:
EXPLAIN SELECT category, SUM(value) FROM products GROUP BY category;
EXPLAIN SELECT category, COUNT(*) FROM products GROUP BY category HAVING COUNT(*) > 1;
EXPLAIN SELECT category, SUM(value) FROM products GROUP BY category HAVING SUM(value) > 100;
Output:
plan
----
Aggregate category, SUM(value) by category
StorageGrouped products (category)
else SeqScan products
plan
----
Having (COUNT(*) > 1)
Aggregate category, COUNT(*) by category
SortedStream products (category)
else SeqScan products
plan
----
Having (SUM(value) > 100)
Aggregate category, SUM(value) by category
StorageGrouped products (category)
else SeqScan products
Any other grouping over one table, with a WHERE, several keys, expressions, ROLLUP, CUBE or GROUPING SETS, groups the rows its read passes, and a HAVING without GROUP BY judges the one row:
EXPLAIN SELECT category, SUM(value) FROM products WHERE value > 0 GROUP BY category;
Output:
plan
----
Aggregate category, SUM(value) by category
IndexScan products (value > 0)
Index idx_value (value range)
The scan line is the same one a planned statement prints: the access rule that chooses its keys or indexes is one rule for every path. Window functions over the groups show as a Window step over the HAVING and the aggregate:
EXPLAIN SELECT category, SUM(value), RANK() OVER (ORDER BY SUM(value) DESC) FROM products GROUP BY category HAVING COUNT(*) > 1;
Output:
plan
----
Window RANK() OVER (ORDER BY SUM(value) DESC)
Having (COUNT(*) > 1)
Aggregate category, SUM(value) by category
SeqScan products
A DISTINCT ON keeps the first group of each key, as the sort under it orders them; a key the SELECT list does not compute is computed over each group beside it:
EXPLAIN SELECT DISTINCT ON (category) category, name, SUM(value) FROM products GROUP BY category, name ORDER BY category, SUM(value) DESC;
Output:
plan
----
DistinctOn category
Sort category ASC, SUM(value) DESC
Aggregate category, name, SUM(value) by category, name
SeqScan products
A window that names a WINDOW definition shows as the Window step any window does, Window RANK() OVER w. A grouped statement with a subquery prints the format of the path that runs it.
An aggregate over a view, a CTE, a derived table, a VALUES list or a table function groups the rows its source gives, read once, with the source’s plan under it:
EXPLAIN SELECT category, COUNT(*) FROM (SELECT category FROM products WHERE value > 0) p GROUP BY category;
Output:
plan
----
Aggregate category, COUNT(*) by category
Derived p
Project category
IndexScan products (value > 0)
Index idx_value (value range)
Views, CTEs and Derived Tables
A statement over a view, a CTE, a derived table, a VALUES list or a table function reads the rows its source gives and judges each by the whole WHERE, shown after the source’s name. The plan of the statement the rows come from is indented under it, in the format of the path that runs it:
EXPLAIN SELECT name FROM (SELECT id, name, value FROM products) p WHERE value > 15 ORDER BY name LIMIT 2;
Output:
plan
----
Limit 2
TopN name ASC
Project name
Derived p (value > 15)
SELECT
Columns: id, name, value
-> Seq Scan on products
A CTE read once, with no column list, runs as a derived table and shows as one. Any other CTE runs before the statement and shows as Cte, with its query’s plan under it.
Window functions over a source’s rows show as a Window step over the source, with the ORDER BY and LIMIT above it, as over a table:
EXPLAIN SELECT name, ROW_NUMBER() OVER (ORDER BY value DESC) AS rn
FROM (SELECT name, value FROM products WHERE value > 10) p ORDER BY rn LIMIT 2;
Output:
plan
----
Limit 2
TopN rn ASC
Window ROW_NUMBER() OVER (ORDER BY value DESC)
Derived p
Project name, value
IndexScan products (value > 10)
Index idx_value (value range)
Subqueries Over One Source
A SELECT over one table, view, CTE, derived table, VALUES list or table function, or with no FROM, whose WHERE or select list holds a subquery is planned once too, and EXPLAIN prints its plan, then Prepared: yes and its subqueries. Over a source that is not a table, the rows the source gives are each judged by the whole WHERE, its subqueries included, shown after the source’s name as in Views, CTEs and Derived Tables. Over a table, a subquery that reads no enclosing row and is compared with a column in a top-level AND, or any other such value that reads no row, is read once per run and taken by the scan as a parameter is, so the plan’s index is the one the run reads. It shows as written:
EXPLAIN SELECT name FROM products WHERE value > (SELECT AVG(amount) FROM orders);
Output:
plan
----
Project name
IndexScan products (value > (SELECT AVG(amount) FROM orders))
Index idx_value (value range)
Prepared: yes
Subqueries:
1: scalar over orders, once per run
A column tested with IN, ANY or ALL against a subquery, or an EXISTS that reads the row, reads its rows by the members or keys the subquery gives, which only a run knows. The scan shows what it takes before them, and each run picks its read once they are known:
EXPLAIN SELECT name FROM products WHERE id IN (SELECT product_id FROM orders WHERE customer_id = 7);
Output:
plan
----
Project name
Filter
Scan products
read chosen per run, over its sets
Prepared: yes
Subqueries:
1: IN over orders, once per run
Every CTE of the statement runs before it. One that is not the statement’s source, such as a CTE only a subquery reads, is listed after the subqueries under Ctes:, with its query’s plan under its name:
EXPLAIN WITH big AS (SELECT amount FROM orders WHERE amount > 100)
SELECT name FROM products WHERE value > (SELECT AVG(amount) FROM big);
Output:
plan
----
Project name
IndexScan products (value > (SELECT AVG(amount) FROM big))
Index idx_value (value range)
Prepared: yes
Subqueries:
1: scalar over big, once per run
Ctes:
big
Project amount
SeqScan orders (amount > 100)
Hash Join
When joining tables:
EXPLAIN SELECT o.id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id;
Output:
plan
----
SELECT
Columns: o.id, c.name
-> Hash Join (INNER Join) (cost=1.10 rows=1)
Join Cond: (o.customer_id = c.id)
-> Seq Scan on orders
Alias: o
-> Seq Scan on customers
Alias: c
Sorting
When using ORDER BY:
EXPLAIN SELECT * FROM products ORDER BY value DESC;
Output:
plan
----
Sort value DESC
Project id, name, category, value
SeqScan products
Under a LIMIT, an ORDER BY of one column reads the rows in that order instead of sorting them all: through the column’s index or the primary key (IndexOrder), from a lower bound on the primary key (Keyset), or by the table’s top-k scan (TopK). A run the table cannot answer that way takes the next line after else: the top-k scan, then a TopN over the read beneath it, which keeps only the rows the LIMIT keeps:
EXPLAIN SELECT * FROM products ORDER BY value DESC LIMIT 5;
EXPLAIN SELECT * FROM products WHERE id > 1 ORDER BY id LIMIT 5;
Output:
plan
----
Limit 5
Project id, name, category, value
IndexOrder products value DESC
else TopK products value DESC
else TopN value DESC
SeqScan products
plan
----
Limit 5
Project id, name, category, value
Keyset products id ASC (id > 1)
else TopK products id ASC (id > 1)
else TopN id ASC
IndexScan products (id > 1)
Index __pk_products_id (id range)
With no WHERE, an ORDER BY of several columns in one direction, all ASC or all DESC, reads the rows the same way through a multi-column index whose leading columns are those columns, in that order: IndexOrder names them all, and each column’s NULLs go where the ORDER BY puts them. Under an OFFSET that reaches past a quarter of the table’s rows, inside a transaction holding rows of its own, over a FLOAT column or over sealed rows, the run takes the TopN after else instead:
CREATE INDEX idx_category_value ON products (category, value);
EXPLAIN SELECT * FROM products ORDER BY category, value LIMIT 5;
Output:
plan
----
Limit 5
Project id, name, category, value
IndexOrder products category, value ASC
else TopN category ASC, value ASC
SeqScan products
A LIMIT or OFFSET given as a parameter or a subquery is read on each run and takes the same plan; EXPLAIN shows it as written:
EXPLAIN SELECT * FROM products ORDER BY value DESC LIMIT $1;
Output:
plan
----
Limit $1
Project id, name, category, value
IndexOrder products value DESC
else TopK products value DESC
else TopN value DESC
SeqScan products
A WHERE with a term that reads no column, such as a parameter or a literal comparison, reads no row on a run where that term is not true:
EXPLAIN SELECT * FROM products WHERE 1 = 0 AND value > 15;
Output:
plan
----
Project id, name, category, value
Empty unless (1 = 0)
else IndexScan products ((1 = 0) AND (value > 15))
Index idx_value (value range)
A DISTINCT is taken over the projected rows, before the ORDER BY. A DISTINCT of one column with no WHERE reads the distinct values the table keeps for that column in its index or its volumes, and scans when it keeps none, or when the transaction running it has written to the table:
EXPLAIN SELECT DISTINCT category FROM products;
Output:
plan
----
Distinct
Project category
DistinctValues products (category)
else SeqScan products
A DISTINCT ON keeps the first row of each key once the rows are sorted:
EXPLAIN SELECT DISTINCT ON (category) category, name FROM products ORDER BY category, value DESC;
Output:
plan
----
DistinctOn category
Sort category ASC, value DESC
Project category, name
SeqScan products
Window functions are computed over every row the read passes, and the SELECT list with them. With no WHERE, a read through an index may give the rows already grouped by the one PARTITION BY column every window shares, or ordered by the first window’s ORDER BY column when that column holds no NULL. Under a LIMIT with no ORDER BY, DISTINCT or DISTINCT ON, the partitions are read one at a time until the LIMIT has its rows:
EXPLAIN SELECT name, RANK() OVER (PARTITION BY category ORDER BY value DESC) AS r FROM products LIMIT 5;
Output:
plan
----
Limit 5
Window RANK() OVER (PARTITION BY category ORDER BY value DESC)
PartitionsByIndex products (category) one at a time to the LIMIT
else SeqScan products
The ORDER BY, DISTINCT and LIMIT are taken over the window step’s rows:
EXPLAIN SELECT name, SUM(value) OVER (PARTITION BY category) AS total FROM products WHERE value > 10 ORDER BY total DESC;
Output:
plan
----
Sort total DESC
Window SUM(value) OVER (PARTITION BY category)
IndexScan products (value > 10)
Index idx_value (value range)
A set operation over a table shows its first branch’s plan and, beside it, each other branch as EXPLAIN shows that statement on its own. The ORDER BY and LIMIT above it are the whole statement’s:
EXPLAIN SELECT name FROM products WHERE value > 15
UNION SELECT category FROM products ORDER BY name LIMIT 5;
Output:
plan
----
Limit 5
TopN name ASC
UNION
Project name
IndexScan products (value > 15)
Index idx_value (value range)
SELECT
Columns: category
-> Seq Scan on products
A UNION ALL without ORDER BY or DISTINCT needs at most LIMIT + OFFSET rows from its first branch, which shows that bound as a Limit step of its own, Limit (2 + 1) under LIMIT 2 OFFSET 1; a grouped branch reads only the groups the bound needs where the table’s index walk can stop.
EXPLAIN ANALYZE
Executes the query and shows actual runtime statistics.
EXPLAIN ANALYZE SELECT * FROM products WHERE value > 15;
Output:
plan
----
SELECT (actual time=1.53ms, rows=2)
Columns: *
-> Seq Scan on products (actual rows=2)
Filter: (value > 15)
Understanding EXPLAIN ANALYZE Output
| Field | Description |
|---|---|
| actual time | Total execution time |
| rows | Number of rows returned |
| actual rows | Rows processed at each step |
| Filter | WHERE condition applied |
Subqueries
For a statement that runs its subqueries as prepared subqueries, EXPLAIN ANALYZE ends with a Subqueries: section: one line per subquery, numbered from 1, nested ones included, and so are the subqueries of its LIMIT and OFFSET and of its derived tables, set operation branches, CTEs and join sides that the run prepared. A view keeps its subqueries in its own plan, which EXPLAIN ANALYZE of the view’s query lists. A subquery that runs as a statement of its own lists that statement’s subqueries under its line, numbered after it, such as 1.1. Each line says what the subquery tests, what it reads and how it answers:
EXPLAIN ANALYZE SELECT id FROM t WHERE EXISTS (SELECT 1 FROM w WHERE w.k = t.k);
SELECT (actual time=1.58ms, rows=2)
Columns: id
-> Seq Scan on t (actual rows=2)
Filter: EXISTS (SELECT 1 FROM w WHERE (w.k = t.k))
Subqueries:
1: EXISTS over w, per call: a keyed set, or where the index on k makes probing cheaper: its pushed conditions
| Answer | Meaning |
|---|---|
| once per run | It reads no enclosing query: computed once per execution |
| once per row of N | It reads only values of subquery N’s enclosing row: computed once for each such row |
| per call | Computed for each row that asks it |
| a statement | It runs as a statement of its own, such as one over a join |
| a keyed set, or where the index on k makes probing cheaper: … | Each execution chooses on its first call: a probe per call when an index on k answers it and the enclosing rows are few or its LIMIT is small, else the keyed set, whose rows of every key are read once and each call looks its key up. A subquery whose WHERE asks more than the key probes only while the table holds no sealed rows, and its probes read at most a quarter of the table’s rows in all, since a probe reads every row of a key the rest rejects: each call counts its key’s rows in the index first, and a key past what is left switches the execution to the keyed set |
| the count the index on k holds, else a scan | A COUNT(*) of one equality is counted from the index where it answers for the call |
| the row its key names | The equality on the primary key reads that one row |
| its pushed conditions, a scan | The rows the conditions the scan takes pass, or every row |
Comparing Estimated vs Actual
EXPLAIN ANALYZE helps identify inaccurate estimates:
EXPLAIN ANALYZE SELECT * FROM large_table WHERE rare_condition = true;
If estimated rows differs significantly from actual rows:
- Run
ANALYZE table_nameto update statistics - Consider adding an index for the condition
Plan Components
Access Methods
For a planned single-table SELECT:
| Component | Description |
|---|---|
| SeqScan | Full table scan; the condition after the table name is the one each row is judged by |
| PkGet | Reads the one row the primary key names |
| UniqueGet | Reads the one row a unique index of one column names for its equality |
| IndexScan | Reads the rows the indexes under it name, each judged by the condition after the table name |
| Index | One index and what it reads: an equality (=), an IN list, a LIKE prefix or a range |
| CompositeIndex | A multi-column index: equalities on its leading columns, a range on the next one |
| PkKey, PkKeys | Rows a primary key equality or IN list names, inside an IndexScan |
| Intersect, Union | The rows every index under it names, or any of them names |
| MixedOr | A read that stops at a LIMIT, of a WHERE that is one OR: the rows its indexed operands name, then a scan asking the others (Scan) |
| IndexOrder, Keyset, TopK | Rows read in ORDER BY order under a LIMIT, with no sort of every row; for window functions, rows in their ORDER BY column’s order |
| DistinctValues | The distinct values the table keeps for one column, for a DISTINCT of that column with no WHERE |
| PartitionsByIndex | Rows grouped by the window functions’ PARTITION BY column through its index, or a partition at a time to the LIMIT |
| Derived, View, Cte | The rows of a derived table, a view or a CTE, each judged by the condition after its name; the plan of its query is under it |
| Values, Function | The rows of a VALUES list or a table function, each judged by the condition after its name |
| OneRow | The one row a SELECT without FROM reads |
| Aggregate | The one row of COUNT, SUM, AVG, MIN and MAX over a table or a source, or its groups after by, from the line under it |
| Stats | Those values from what the table keeps: row count, distinct count, column sum and bounds |
| IndexBound | A MIN or MAX from the first or last key of the column’s index |
| StorageFiltered | The calls the table computes over the rows the WHERE passes |
| StorageGrouped | The groups the table computes over its rows |
| SortedStream | The groups in the order of the column’s index, each read by its rows |
| Having | The groups the HAVING keeps |
| Empty unless | The WHERE’s terms that read no column; a run where they are not true reads no row |
| else | The read a run takes when the table cannot answer the line above it |
| Filter | The part of the WHERE judged after the scan |
| Project | The output columns |
| Window | The window functions over the rows the read passes, and the SELECT list over them |
| Distinct | The first row of each output value |
| DistinctOn | The first row of each DISTINCT ON key, over the sorted rows |
| UNION, UNION ALL, INTERSECT, EXCEPT | A set operation over its first branch’s rows and each other branch’s |
| TopN | The ORDER BY under a LIMIT, keeping only the rows the LIMIT keeps |
| Sort | The ORDER BY over every row |
| Limit | The LIMIT and OFFSET |
For other statements:
| Component | Description |
|---|---|
| Seq Scan | Full table scan (reads all rows) |
| IndexScan | Reads the rows its keys or indexes name, listed under it |
| Filter | Condition applied after scan |
Join Methods
| Component | Description |
|---|---|
| Hash Join | Builds hash table, probes with other table |
| Nested Loop | Loops over each row combination |
| Join Cond | Join condition |
Modifiers
| Component | Description |
|---|---|
| Group By | Columns for aggregation |
| Order By | Sort specification |
| Limit | Row count limit |
| Alias | Table alias in query |
| Prepared | For a WHERE with a subquery, or a join whose ON holds one: yes when the statement runs as a prepared subquery, otherwise no and the reason |
Cost Information
A join printed in the general format carries two numbers:
-> Hash Join (INNER Join) (cost=1.10 rows=1)
- cost: a rough figure from the row counts of the two tables
- rows: the estimated number of rows
Both are for reading only: they do not choose the join’s method or order (see Query Planning).
Use Cases
Query Optimization
Use EXPLAIN to understand query behavior:
-- Check if index is being used
EXPLAIN SELECT * FROM orders WHERE customer_id = 100;
-- If showing Seq Scan, consider adding an index
CREATE INDEX idx_customer ON orders(customer_id);
-- Verify index is now used
EXPLAIN SELECT * FROM orders WHERE customer_id = 100;
Performance Debugging
Use EXPLAIN ANALYZE to find slow operations:
-- Find where time is spent
EXPLAIN ANALYZE SELECT o.*, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE o.amount > 1000;
Look for:
- Large “actual rows” values
- Operations taking significant time
- Mismatch between estimated and actual rows
Index Effectiveness
Compare plans with and without indexes:
-- Without index
EXPLAIN ANALYZE SELECT * FROM products WHERE category = 'Electronics';
-- Add index
CREATE INDEX idx_category ON products(category);
-- With index
EXPLAIN ANALYZE SELECT * FROM products WHERE category = 'Electronics';
Best Practices
- Use EXPLAIN first: Check plan before running expensive queries
- Use EXPLAIN ANALYZE for optimization: Get actual numbers for tuning
- Check for Seq Scan on large tables: Consider adding indexes
- Verify index usage: Ensure expected indexes are being used
- Update statistics: Run ANALYZE after significant data changes
Limitations
- EXPLAIN shows the planned execution, not necessarily the exact runtime behavior
- Cost values are relative, not absolute time measurements
- Some runtime optimizations may not appear in the plan