The symptom
A full scan reads every row of a table (or every entry of an index) and tests each one against the WHERE clause. It appears as Seq Scan in PostgreSQL, Table scan または type: ALL in MySQL, Clustered Index Scan または Table Scan in SQL Server, TABLE ACCESS FULL in Oracle and SCAN in SQLite. The scan itself is not the problem. Reading 400,000 rows to return 20 is.
The examples on this page run against a small shop schema: customers (20,000 rows), orders (400,000 rows) and order_items (800,000 rows). Paste your own plan into the query plan viewer to see the same numbers for your query.
Diagnose it
Count what the scan reads and what it keeps
Run the query with run-time statistics, so the plan shows actual row counts next to the estimates. On PostgreSQL:
EXPLAIN (ANALYZE, BUFFERS)
SELECT order_id, total FROM orders WHERE customer_id = 4242;
Gather (cost=1000.00..7567.43 rows=21 width=10) (actual time=0.493..6.755 rows=20.00 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=4482
-> Parallel Seq Scan on orders (cost=0.00..6565.33 rows=9 width=10) (actual time=0.235..3.881 rows=6.67 loops=3)
Filter: (customer_id = 4242)
Rows Removed by Filter: 133327
Buffers: shared hit=4482
The line to read is Rows Removed by Filter. When it is thousands of times larger than the rows the node returns, the scan is doing work an index would skip. In a parallel plan both numbers are averages per process, so multiply by loops: here three processes each removed 133,327 rows, which is the whole table, to find 20.
MySQL prints the same comparison with EXPLAIN ANALYZE. In the classic tabular EXPLAIN, the scan is type: ALL を伴う key: NULL.
EXPLAIN ANALYZE
SELECT order_id, total FROM orders WHERE customer_id = 4242;
EXPLAIN
SELECT order_id, total FROM orders WHERE customer_id = 4242;
-> Filter: (orders.customer_id = 4242) (cost=40225 rows=39864) (actual time=2.05..44.4 rows=20 loops=1)
-> Table scan on orders (cost=40225 rows=398641) (actual time=0.101..34.9 rows=400000 loops=1)
On SQL Server, turn on I/O statistics and the actual plan. A table with a clustered index is scanned as a Clustered Index Scan, which is the same full read under another name; the filter shows up as a Predicate on the scan instead of a Seek Predicate.
SET STATISTICS IO ON;
SET STATISTICS XML ON;
SELECT OrderId, Total FROM dbo.Orders WHERE CustomerId = 4242;
Table 'Orders'. Scan count 1, logical reads 2579, physical reads 0
Oracle marks the two kinds of condition in the Predicate Information section under the plan: access conditions are used to find rows, filter conditions are tested on rows already read. A full scan has only a filter.
EXPLAIN PLAN FOR
SELECT order_id, total FROM orders WHERE customer_id = 4242;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 20 | 300 | 650 (1)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| ORDERS | 20 | 300 | 650 (1)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter("CUSTOMER_ID"=4242)
SQLite has no row counts in its plan. SCAN means every row is read, SEARCH means an index or the primary key narrows the read.
EXPLAIN QUERY PLAN
SELECT order_id, total FROM orders WHERE customer_id = 4242;
QUERY PLAN
`--SCAN orders
An index read can discard rows too
The same waste hides behind an index when the index matches only part of the WHERE clause. Here the index finds every order of customer 1, and a filter then drops all but the pending ones:
CREATE INDEX orders_customer_idx ON orders (customer_id);
EXPLAIN (ANALYZE)
SELECT order_id FROM orders WHERE customer_id = 1 AND status = 'pending';
Bitmap Heap Scan on orders (cost=473.13..5580.33 rows=442 width=4) (actual time=1.054..6.091 rows=4000.00 loops=1)
Recheck Cond: (customer_id = 1)
Filter: ((status)::text = 'pending'::text)
Rows Removed by Filter: 36000
Heap Blocks: exact=4482
-> Bitmap Index Scan on orders_customer_idx (cost=0.00..473.02 rows=41680 width=0) (actual time=0.717..0.718 rows=40000.00 loops=1)
Index Cond: (customer_id = 1)
The plan has an Index Cond (what the index was searched for) and a Filter (what was tested afterwards), and Rows Removed by Filter counts the rows that were fetched for nothing. SQL Server shows the same split as Seek Predicate そして Predicate, Oracle as access そして filter, and MySQL as an index lookup under a Filter step.
Check that the scan is not the right plan
An index only pays when the condition is selective. In the sample data 96% of orders have status = 'shipped', and PostgreSQL does not use an index on status to find them: one pass over the table is cheaper than 384,000 separate visits.
CREATE INDEX orders_status_idx ON orders (status);
EXPLAIN SELECT order_id, total FROM orders WHERE status = 'shipped';
EXPLAIN SELECT order_id, total FROM orders WHERE status = 'pending';
The first query keeps its Seq Scan; the second, which matches 1% of the table, uses the index. Before you add an index, compare the rows the scan returns with the rows in the table. If the query needs most of the table, the fix is elsewhere: return fewer rows, or see the last section.
Fix it
Index the columns the filter uses
Create an index whose leading column is the column the query filters on with =. Then capture the plan again and check that the scan became a seek.
EXPLAIN (ANALYZE)
SELECT order_id, total FROM orders WHERE customer_id = 4242;
Bitmap Heap Scan on orders (cost=4.59..84.54 rows=21 width=10) (actual time=0.030..0.054 rows=20.00 loops=1)
Recheck Cond: (customer_id = 4242)
Heap Blocks: exact=20
-> Bitmap Index Scan on orders_customer_idx (cost=0.00..4.58 rows=21 width=0) (actual time=0.023..0.023 rows=20.00 loops=1)
Index Cond: (customer_id = 4242)
CREATE INDEX orders_customer_idx ON orders (customer_id);
EXPLAIN ANALYZE
SELECT order_id, total FROM orders WHERE customer_id = 4242;
-> Index lookup on orders using orders_customer_idx (customer_id=4242) (cost=7 rows=20) (actual time=0.0825..0.0843 rows=20 loops=1)
CREATE INDEX IX_Orders_CustomerId ON dbo.Orders (CustomerId);
SELECT OrderId, Total FROM dbo.Orders WHERE CustomerId = 4242;
Table 'Orders'. Scan count 1, logical reads 64, physical reads 0
The plan is now an Index Seek on the new index joined by Nested Loops to a Key Lookup that fetches Total from the clustered index, and logical reads fall from 2,579 to 64. The key lookup page shows how to remove that second step.
CREATE INDEX orders_customer_idx ON orders (customer_id);
EXPLAIN PLAN FOR
SELECT order_id, total FROM orders WHERE customer_id = 4242;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 20 | 300 | 21 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| ORDERS | 20 | 300 | 21 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | ORDERS_CUSTOMER_IDX | 20 | | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - access("CUSTOMER_ID"=4242)
CREATE INDEX orders_customer_idx ON orders (customer_id);
EXPLAIN QUERY PLAN
SELECT order_id, total FROM orders WHERE customer_id = 4242;
QUERY PLAN
`--SEARCH orders USING INDEX orders_customer_idx (customer_id=?)
On a busy production table, build the index without blocking writes: CREATE INDEX CONCURRENTLY PostgreSQL で、 WITH (ONLINE = ON) on SQL Server editions that support it, ONLINE on Oracle. The online index build page has the syntax for each engine.
Extend the index when a filter still discards rows
When the plan already uses an index but filters most of what it fetches, add the filtered column to the index key. Equality columns go first, then the range or sort column.
CREATE INDEX orders_customer_status_idx ON orders (customer_id, status);
EXPLAIN (ANALYZE)
SELECT order_id FROM orders WHERE customer_id = 1 AND status = 'pending';
Bitmap Heap Scan on orders (cost=8.95..1315.12 rows=442 width=4) (actual time=0.456..2.591 rows=4000.00 loops=1)
Recheck Cond: ((customer_id = 1) AND ((status)::text = 'pending'::text))
Heap Blocks: exact=3574
-> Bitmap Index Scan on orders_customer_status_idx (cost=0.00..8.84 rows=442 width=0) (actual time=0.217..0.217 rows=4000.00 loops=1)
Index Cond: ((customer_id = 1) AND ((status)::text = 'pending'::text))
Both conditions are now in the Index Cond and nothing is removed by a filter. The narrower index on customer_id alone is now redundant and can be dropped.
Find out why an existing index is skipped
If the table already has an index on the filtered column and the plan scans anyway, the predicate does not match the index. The usual causes each have their own page: a function wrapped around the column (non-sargable predicates), a type mismatch (implicit conversions), the wrong leading column or a row estimate that makes the scan look cheaper (why the optimizer ignores your index).
When to leave the scan alone
Leave it when the table is small, when the query really needs most of the rows, or when the scan feeds a hash join or an aggregate over the whole table. An index costs writes and space on every insert and update, so a scan in a nightly report is rarely worth one. Judge by rows read against rows returned and by the time the node takes, not by the word "scan".
The checklist
Work through it in order. Each step gives you the evidence for the next one.
- Capture the plan with actual row counts (EXPLAIN ANALYZE, SET STATISTICS XML ON, or DBMS_XPLAN.DISPLAY_CURSOR).
- Find the scan and compare the rows it reads with the rows it returns.
- Compare the rows returned with the size of the table: above a few percent, the scan may be the right plan.
- Check for an existing index on the filtered column; if there is one, find out why the predicate does not match it.
- Create an index led by the equality columns of the WHERE clause.
- Re-capture the plan and confirm the scan became a seek and the discarded rows are gone.
- Drop any index the new one made redundant.
Questions that come up
Is a full table scan always bad?
No. It is the cheapest way to read most of a table, and the only sensible way to read a small one. It is a problem when the scan reads many times more rows than the query returns, and the query runs often enough or long enough to matter.
Why does SQL Server show a Clustered Index Scan instead of a Table Scan?
A table with a clustered index is stored in that index, so reading the whole table is a scan of the clustered index. Table Scan appears only for heaps, tables without a clustered index. Both read every row.
The index exists. Why does the plan still scan the table?
Either the predicate cannot use the index (a function or a type conversion on the column, a leading wildcard, the wrong leading column) or the optimizer estimates that the query returns too large a share of the table for the index to be cheaper. Compare estimated and actual rows on the scan to tell the two apart.
What does Rows Removed by Filter mean in PostgreSQL?
It is the number of rows the node read and then discarded because they failed the Filter condition. In a parallel plan it is an average per process, so multiply by the loops value. A large number next to a small row count means an index on the filtered column would avoid most of the work.
Should I add every index the plan seems to want?
No. Each index slows down inserts, updates and deletes on the table and takes space. Add the index for queries that run often or block users, prefer one composite index that serves several queries over several single-column ones, and check after a week that the index is used.