CodeBlanks Logo Icon CodeBlanks

Seq scan vs index scan

Learn table scan vs index scan.

Seq scan vs index

A Seq scan scans the whole table. An Index scan uses the index, which is faster.

-- Seq Scan: WHERE price > 0 (tüm tablo)
-- Index Scan: WHERE id = 5 (indeks)

Seq slow, index fast.

Seq scan vs index — details

Seq scan is fine on small tables. On large tables, adding an index speeds things up.

For example, if you search a table with millions of orders by customer_id, an index on that column turns a Seq Scan into an Index Scan.

Practice Seq scan vs index

Use EXPLAIN to see if a query uses a seq scan or an index scan.

Starter code

query.sql
-- inspect plan
EXPLAIN

Sample solution

query.sql
EXPLAIN SELECT * FROM products WHERE id = 5;

Expected output

Index Scan using idx_price

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Index scan

Question
____ → uses an index
Answer
Index scan → uses an index

Explanation: Index scan is the right answer.

2 When is an index scan fast?

  • On a small table
  • On a large table Correct answer
  • Always
  • Never

Explanation: An index scan is fast on large tables.

3 Seq scan vs index scan: match the terms

Term Meaning
Seq scan full table
Index scan index
large table index (2)
small table scan

All lessons in Advanced