bhylu.com

Advertisements

SQL Query Plans

A query plan describes how the database intends to find and combine rows. Use it to investigate measured performance problems rather than guessing that a shorter query must be faster.

Understand the idea

SQLite's EXPLAIN QUERY PLAN gives a readable overview. A table scan is not automatically bad: scanning four rows is cheap. With realistic data, inspect whether filters use a useful index, whether joins multiply intermediate rows, and whether sorting needs extra work. Server databases have their own EXPLAIN or execution-plan tools, sometimes with actual timing and row counts.

Where to try it

Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.

SQLite sample data · MySQL / MariaDB sample data · See the sample tables

A worked example

SQLite

CREATE INDEX idx_orders_status ON orders(status);
EXPLAIN QUERY PLAN
SELECT order_id, total_cents FROM orders WHERE status = 'paid';

Read the result

The result describes a search or scan chosen by SQLite. Exact wording and choices can vary with runtime version, statistics, and data size. The output is evidence about the plan, not a guarantee of elapsed time.

Try one change

Compare plans for status = 'paid' and customer_id = 1. Explain why the status index does not solve every filter.