A query plan can reveal when SQLite must sort rows for an ORDER BY that is not already provided by an index.

Program

Play the script to choose an event kind and inspect the plan before reading the date-ordered rows.

sort_temp_plan.sql
CREATE TABLE plan_choice AS WITH params(wanted_kind) AS (VALUES ()) SELECT wanted_kind FROM params;
CREATE TABLE events (id INTEGER, event_day TEXT, kind TEXT);
INSERT INTO events VALUES (1, '2026-05-03', 'deploy'), (2, '2026-05-01', 'build'), (3, '2026-05-02', 'deploy'), (4, '2026-05-04', 'build');
CREATE INDEX idx_events_kind ON events(kind);
EXPLAIN QUERY PLAN SELECT id, event_day FROM events WHERE kind = (SELECT wanted_kind FROM plan_choice) ORDER BY event_day;
SELECT id, event_day FROM events WHERE kind = (SELECT wanted_kind FROM plan_choice) ORDER BY event_day;
CREATE TABLE plan_choice AS WITH params(wanted_kind) AS (VALUES ()) SELECT wanted_kind FROM params;
CREATE TABLE events (id INTEGER, event_day TEXT, kind TEXT);
INSERT INTO events VALUES (1, '2026-05-03', 'deploy'), (2, '2026-05-01', 'build'), (3, '2026-05-02', 'deploy'), (4, '2026-05-04', 'build');
CREATE INDEX idx_events_kind ON events(kind);
EXPLAIN QUERY PLAN SELECT id, event_day FROM events WHERE kind = (SELECT wanted_kind FROM plan_choice) ORDER BY event_day;
SELECT id, event_day FROM events WHERE kind = (SELECT wanted_kind FROM plan_choice) ORDER BY event_day;
ORDER BY `ORDER BY event_day` asks for a stable presentation order.
temporary sort When an index does not already provide that order, the plan can show extra sorting work.
planner detail The diagnostic rows help identify whether an index supports filtering, ordering, or both.