Looking up one customer’s orders in a million-row table took 63 milliseconds in SQLite. After one CREATE INDEX statement, the same query took 0.03 milliseconds, about 2,000 times faster, and the query itself didn’t change by a single character. What changed was a line of output most people never look at: the query plan went from SCAN orders to SEARCH orders USING INDEX. The tests below ran on a one-million-row orders table in both SQLite and PostgreSQL 16, to show what indexes do, when the database ignores them on purpose, and what they cost.
The test table
A million orders
The table had five columns: an integer id, a customer_id between 1 and 50,000, a status (about 90 percent “shipped,” 9 percent “pending,” 1 percent “refunded”), a total, and a created date in 2025. In SQLite, the file came to 78 MB.
Two ways to ask how a query will run
SQLite uses EXPLAIN QUERY PLAN before a query. PostgreSQL uses EXPLAIN, or EXPLAIN ANALYZE to actually run the query and report real row counts and timings. PostgreSQL’s documentation warns that EXPLAIN ANALYZE really does execute the statement, side effects included, and recommends wrapping data-modifying statements in a transaction that gets rolled back.
The two words that matter
SQLite’s EXPLAIN QUERY PLAN documentation defines them: “‘SCAN’ is used for a full-table scan,” while “‘SEARCH’ indicates that only a subset of the table rows are visited.” The same page notes that the output format “may change between SQLite releases,” so it is for reading by people, not parsing by programs.

One index, one query
Before the index
SELECT * FROM orders WHERE customer_id = 4242;
plan: SCAN orders
63.41 ms, 18 rows
SQLite read all one million rows to find 18.
After the index
CREATE INDEX idx_cust ON orders(customer_id);
plan: SEARCH orders USING INDEX idx_cust (customer_id=?)
0.03 ms, 18 rows
An index is a separate, sorted structure (a B-tree) that maps each customer_id to the rows holding it. Finding 4242 means walking down a few levels of that tree instead of reading the whole table.
The same in PostgreSQL
Without an index, PostgreSQL 16 ran a parallel sequential scan with two worker processes, each removing about 333,000 non-matching rows, and finished in 48.6 ms. With the index, it switched to a Bitmap Index Scan feeding a Bitmap Heap Scan and finished in 0.2 ms. PostgreSQL’s documentation explains the bitmap approach: it collects the matching row locations, sorts them into physical order and then reads the table pages, to “minimize the cost of separate fetches.”
Column order in multicolumn indexes
A two-column index
A query filtering refunded orders from December:
SELECT * FROM orders WHERE status = 'refunded' AND created >= '2025-12-01';
no index: SCAN orders, 54.28 ms
INDEX ON orders(status, created): SEARCH ... (status=? AND created>?), 1.88 ms
873 rows, found in under 2 ms once the index matched the query’s shape.
The leftmost rule
That same index did nothing for a query on created alone:
SELECT * FROM orders WHERE created >= '2025-12-01';
plan: SCAN orders, 199.47 ms, 83,390 rows
PostgreSQL’s page on multicolumn indexes states the principle: a B-tree index on several columns “is most efficient when there are constraints on the leading (leftmost) columns.” Without a condition on status, the created values are scattered through the index, one sorted run per status. The documentation adds that such an index could in principle still be scanned in full, but “in most cases the planner would prefer a sequential table scan.”
Choosing the order
Columns compared with = usually go first, and a range condition (>=, BETWEEN) goes last. An index on (status, created) serves “this status in this date range,” while an index on (created, status) would serve “this date range” with or without a status.

When the planner skips the index
Wrapping the column in an expression
SELECT count(*) FROM orders WHERE customer_id + 0 = 4242;
plan: SCAN orders USING COVERING INDEX idx_cust
45.67 ms
The index stores customer_id, not customer_id + 0. SQLite still read the index (smaller than the table) but had to check every entry, so a query that took 0.03 ms with a plain comparison took 45 ms. The same happens with functions such as lower(email) = ... or date(created) = ...: the column inside a function can’t use an ordinary index on that column. Both databases support indexes on expressions for exactly this case.
When most rows match
PostgreSQL showed the other reason the planner skips an index, on purpose:
| Query | Matching rows | Plan | Time |
|---|---|---|---|
status = 'refunded' |
9,921 | Index Scan using idx_status | 27.6 ms |
status = 'shipped' |
900,191 | Seq Scan | 193.0 ms |
The index existed for both. For 1 percent of the table, following it was worth it. For 90 percent, reading the table straight through was cheaper than jumping back and forth through an index, and the planner chose the scan. An index on a column with few distinct values helps only for the rare values.
Statistics drive the choice
PostgreSQL makes those decisions from table statistics gathered by ANALYZE (also run automatically by autovacuum). After loading lots of data, an explicit ANALYZE makes sure the planner knows what the table looks like.
Reading PostgreSQL’s output line by line
The full plan
Here is PostgreSQL’s EXPLAIN ANALYZE for the indexed customer lookup, with cost estimates turned off to keep it short:
Bitmap Heap Scan on orders (actual time=0.102..0.176 rows=12 loops=1)
Recheck Cond: (customer_id = 4242)
Heap Blocks: exact=12
-> Bitmap Index Scan on idx_cust (actual time=0.088..0.089 rows=12 loops=1)
Index Cond: (customer_id = 4242)
Planning Time: 0.259 ms
Execution Time: 0.199 ms
Inside out, bottom up
Plans are trees, and the indented node runs first. The Bitmap Index Scan found 12 matching entries in the index; the Bitmap Heap Scan above it fetched them from the table. Heap Blocks: exact=12 says 12 table pages were read, one per matching row, out of the thousands the table occupies.
The numbers in parentheses
actual time=0.102..0.176 gives the time until the node produced its first row and its last, in milliseconds. rows=12 is how many rows came out, and loops=1 says the node ran once. In the earlier unindexed plan, the line Rows Removed by Filter: 333329 was the giveaway: each of the three processes (the leader and two workers) read about a third of the table and threw nearly all of it away.
Estimates versus reality
With cost display on, each node also shows the planner’s estimated row count. A large gap between estimated and actual rows is the most common reason PostgreSQL picks a bad plan, and fresh statistics from ANALYZE usually close it. (The random test data here produced 12 rows for customer 4242 in PostgreSQL and 18 in SQLite, because each database generated its own table.)

Covering indexes
Answering from the index alone
When every column a query needs is in the index, the database doesn’t have to visit the table at all:
SELECT customer_id FROM orders WHERE customer_id BETWEEN 100 AND 200;
plan: SEARCH orders USING COVERING INDEX idx_cust (customer_id>? AND customer_id<?)
0.73 ms, 2,005 rows
SQLite’s documentation shows the same change from USING INDEX to USING COVERING INDEX when an index includes all of a query’s columns.
Selecting fewer columns
SELECT * forces a visit to the table for every match, because the index doesn’t hold every column. Asking only for the columns a program actually uses sometimes turns a search into a covering one, which is a quiet argument against SELECT * in application code.
What indexes cost
Slower writes
Every index must be updated on every insert, update and delete. Inserting 300,000 rows into a fresh SQLite table:
| Indexes on table | Insert time | File size |
|---|---|---|
| 0 | 0.27 s | 9.2 MB |
| 1 | 0.70 s | 12.9 MB |
| 3 | 1.39 s | 25.2 MB |
Three indexes made the load five times slower and the file nearly three times larger.
Space
In PostgreSQL, the 1-million-row table took 58 MB, and the two single-column indexes took 7.8 MB and 6.8 MB. Small here, but indexes on wide text columns or many columns grow quickly.
Making plans visible by default
In the sqlite3 command-line shell, two dot-commands save typing: .eqp on prints the query plan before every result, and .timer on prints the run time after it. With both on, a count(*) for customer 4242 printed SEARCH orders USING COVERING INDEX idx_cust (customer_id=?) above the answer, 18, so every query tried at the prompt shows its plan without any extra keyword.
Index for the queries that actually run
The useful habit is checking the plan for the slow queries an application really runs, then adding indexes that match their WHERE clauses and column order. An index nobody’s queries use is pure cost: slower writes and more disk, for nothing.
Sources
- EXPLAIN QUERY PLAN, SQLite documentation: https://www.sqlite.org/eqp.html
- Using EXPLAIN, PostgreSQL 16 documentation: https://www.postgresql.org/docs/16/using-explain.html
- Multicolumn Indexes, PostgreSQL 16 documentation: https://www.postgresql.org/docs/16/indexes-multicolumn.html
