Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a sequential scan in PostgreSQL?
A sequential scan reads every row in a table one by one to find matching rows. It is simple but can be slow for large tables.
Click to reveal answer
beginner
What is an index scan in PostgreSQL?
An index scan uses an index to quickly find rows that match a condition, without reading the whole table. It is faster for selective queries.
Click to reveal answer
intermediate
When does PostgreSQL prefer a sequential scan over an index scan?
PostgreSQL prefers a sequential scan when a large portion of the table needs to be read, because reading the whole table can be faster than using the index.
Click to reveal answer
beginner
How does an index scan improve query performance?
An index scan improves performance by quickly locating only the rows that match the query condition, reducing the number of rows read from the table.
Click to reveal answer
intermediate
What is a downside of using an index scan?
Index scans can be slower if the query matches many rows because it may require many random reads, which are slower than reading sequentially.
Click to reveal answer
What does a sequential scan do in PostgreSQL?
AUses an index to find rows quickly
BReads every row in the table one by one
COnly reads the first 10 rows
DReads rows randomly
✗ Incorrect
A sequential scan reads every row in the table one by one to find matching rows.
When is an index scan usually faster than a sequential scan?
AWhen the query matches very few rows
BWhen the table has no indexes
CWhen the table is empty
DWhen the query matches many rows
✗ Incorrect
Index scans are faster when the query matches very few rows because it can quickly locate them using the index.
Why might PostgreSQL choose a sequential scan over an index scan?
ABecause the query is very simple
BBecause the table is very small
CBecause the index is corrupted
DBecause the query matches a large portion of the table
✗ Incorrect
PostgreSQL chooses a sequential scan when the query matches a large portion of the table, making it faster to read all rows sequentially.
What is a disadvantage of index scans?
AThey can be slower if many rows match the query
BThey always read the whole table
CThey do not use indexes
DThey only work on small tables
✗ Incorrect
Index scans can be slower if many rows match because they may cause many random disk reads.
Which scan type reads rows in the order they are stored on disk?
AIndex scan
BBitmap scan
CSequential scan
DHash scan
✗ Incorrect
Sequential scans read rows in the order they are stored on disk, one after another.
Explain the difference between a sequential scan and an index scan in PostgreSQL.
Think about how each method reads data and when each is faster.
You got /4 concepts.
Describe when PostgreSQL might choose a sequential scan instead of an index scan.
Consider the cost of reading many rows randomly vs sequentially.
You got /4 concepts.
Practice
(1/5)
1. What does a sequential scan do in PostgreSQL?
easy
A. Reads only the first 10 rows of a table
B. Uses an index to find specific rows quickly
C. Deletes rows based on a condition
D. Reads every row in the table one by one
Solution
Step 1: Understand sequential scan behavior
A sequential scan reads all rows in the table from start to end without using any index.
Step 2: Compare with other options
Using an index is an index scan, reading only first 10 rows or deleting rows are unrelated actions.
Final Answer:
Reads every row in the table one by one -> Option D
Quick Check:
Sequential scan = full table read [OK]
Hint: Sequential scan reads all rows, no index used [OK]
Common Mistakes:
Confusing sequential scan with index scan
Thinking sequential scan reads only some rows
Assuming sequential scan deletes rows
2. Which of the following is the correct way to see if PostgreSQL uses an index scan or sequential scan?
easy
A. SELECT * FROM table WHERE id = 1;
B. EXPLAIN SELECT * FROM table WHERE id = 1;
C. CREATE INDEX ON table(id);
D. DROP INDEX index_name;
Solution
Step 1: Identify command to check query plan
The EXPLAIN command shows how PostgreSQL executes a query, including scan type.
Step 2: Eliminate other options
SELECT runs query but doesn't show plan; CREATE and DROP INDEX modify indexes, not show plans.
Final Answer:
EXPLAIN SELECT * FROM table WHERE id = 1; -> Option B
Quick Check:
EXPLAIN shows scan type [OK]
Hint: Use EXPLAIN before query to see scan type [OK]
Common Mistakes:
Running SELECT without EXPLAIN to check scan
Confusing index creation with scan checking
Using DROP INDEX to check scans
3. Given a table users(id SERIAL PRIMARY KEY, name TEXT) with 1 million rows, which scan is PostgreSQL likely to use for this query? SELECT * FROM users WHERE id = 500000;
medium
A. Index scan using the primary key index
B. Sequential scan scanning all 1 million rows
C. Bitmap heap scan reading random rows
D. No scan, query will fail
Solution
Step 1: Analyze query condition and table size
The query filters by primary key id, which has an index, and the table is large (1 million rows).
Step 2: Determine efficient scan type
PostgreSQL uses an index scan to quickly find the single matching row instead of scanning all rows.
Final Answer:
Index scan using the primary key index -> Option A
Quick Check:
Selective query on indexed column = index scan [OK]
Hint: Selective query on indexed column uses index scan [OK]
Common Mistakes:
Assuming sequential scan for large tables with indexed filter
Confusing bitmap heap scan with index scan
Thinking query fails without reason
4. You wrote this query: SELECT * FROM orders WHERE customer_id = 123; But EXPLAIN shows a sequential scan instead of an index scan. What could be the reason?
medium
A. There is no index on customer_id
B. The table is empty
C. The query syntax is incorrect
D. PostgreSQL always uses sequential scan
Solution
Step 1: Check index presence on filter column
If no index exists on customer_id, PostgreSQL must scan all rows sequentially.
Step 2: Evaluate other options
Empty table would still show scan but no rows; syntax is correct; PostgreSQL chooses scan type based on indexes.
Final Answer:
There is no index on customer_id -> Option A
Quick Check:
No index on filter column = sequential scan [OK]
Hint: No index on filter column causes sequential scan [OK]
Common Mistakes:
Assuming syntax error causes scan type
Thinking PostgreSQL always uses sequential scan
Ignoring missing index as cause
5. You have a large table products with millions of rows and an index on category_id. You run: SELECT * FROM products WHERE category_id IN (1, 2, 3, 4, 5); PostgreSQL chooses a sequential scan instead of an index scan. Why might this happen?
hard
A. The index on category_id is corrupted
B. Sequential scan is always faster for any query
C. The query is not selective enough; many rows match
D. The IN clause is invalid syntax
Solution
Step 1: Understand query selectivity
The query matches many rows because it filters on multiple categories, reducing selectivity.
Step 2: Explain PostgreSQL scan choice
When many rows match, PostgreSQL prefers sequential scan as it can be faster than many index lookups.
Final Answer:
The query is not selective enough; many rows match -> Option C
Quick Check:
Low selectivity = sequential scan preferred [OK]
Hint: Low selectivity queries often use sequential scan [OK]