CodeBlanks Logo Icon CodeBlanks

Optimization

Read a plan with EXPLAIN and improve the query.

Optimization

In this challenge you will read a query plan with EXPLAIN and add an index.

EXPLAIN SELECT * FROM products WHERE price > 100;
-- Seq Scan → CREATE INDEX → Index Scan

Read plan, add index.

All columns are selected from products. WHERE keeps only the rows where price > 100 holds.

Practice Optimization

See the plan with EXPLAIN SELECT * FROM products WHERE price > 100.

Starter code

query.sql
-- optimization
EXPLAIN

Sample solution

query.sql
EXPLAIN SELECT * FROM products WHERE price > 100;

Expected output

Seq Scan on products

Questions and answers

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

1 Add EXPLAIN

Question
____ SELECT * FROM products WHERE price > 100;
Answer
EXPLAIN SELECT * FROM products WHERE price > 100;

Explanation: EXPLAIN is the right answer.

2 How is a Seq Scan fixed?

  • Delete the table
  • Create a view
  • Write a trigger
  • Add an index Correct answer

Explanation: A Seq Scan is fixed by adding an index.

All lessons in Advanced