Open lesson

Mission: find rows before the hearing

Command palette

This mission workspace keeps code and notes in this browser tab. Export them before closing it. Running code is practice, not an automatic grade for the mission.

Brief

Understand when an index may help a search, read a simple plan before and after its creation, and explain the cost during writes.

In the story, Imani wants to replay a search for your ID before the hearing. Voss claims an index will prove that the rows came from you. Mara gives you a small SQLite copy with A1 and A2 bearing HERO-ID, A3 bearing OTHER, and plans before and after an index on actor_id. Explain the change to a nontechnical team.

Your mission

  1. Write the command creating the index on actor_id and the query finding IDs linked to HERO-ID.
  2. Say what SCAN before and SEARCH ... USING INDEX afterward may mean.
  3. Name the rows expected before and after, and why the plan alone cannot prove a speed gain.
  4. Give one possible cost of the index and explain what it cannot prove about who created authorizations.

How to check your work

  • CREATE INDEX idx_authorizations_actor ON authorizations(actor_id); and a SELECT ... WHERE actor_id = 'HERO-ID' are correct.
  • The first plan scans the table; the second may use the index.
  • A1 and A2 remain the result; repeated measurement on a large copy is needed to judge speed.
  • The index uses space and maintenance; it cannot prove author identity.

Course example

CREATE TABLE authorizations (
    id TEXT PRIMARY KEY,
    actor_id TEXT NOT NULL,
    event_key TEXT NOT NULL
);
INSERT INTO authorizations VALUES ('A1', 'HERO-ID', 'EXIT-7');
INSERT INTO authorizations VALUES ('A2', 'HERO-ID', 'EXIT-8');
INSERT INTO authorizations VALUES ('A3', 'OTHER', 'EXIT-9');

EXPLAIN QUERY PLAN
SELECT id FROM authorizations WHERE actor_id = 'HERO-ID';
CREATE INDEX idx_authorizations_actor ON authorizations(actor_id);
EXPLAIN QUERY PLAN
SELECT id FROM authorizations WHERE actor_id = 'HERO-ID';
SELECT id FROM authorizations WHERE actor_id = 'HERO-ID' ORDER BY id;

Code

Use Tab to leave the editor. Ctrl+Enter runs the source. The lines beginning with -- are task reminders. SQL ignores them. Write your query beneath them.

SQL console

Loading laboratory…

Explore Python or SQL here; this is not an operating-system shell.

This is a disposable browser experiment. The code run here is not recorded as a completed mission.

Each command starts a fresh disposable worker: it runs a captured copy of your current source, then your command with the lesson fixtures. Changes are discarded afterward. Commands never save source or award XP. Ctrl+Enter executes the command.

Virtual workspace files

Source and declarative assets are readable here without JavaScript. Python input files are created at runtime; SQL setup initializes the disposable database.

source.sql

Current editor source. Inspection shows your current edits.

-- Write the command creating the index on `actor_id` and the query finding IDs linked to `HERO-ID`.
-- Say what `SCAN` before and `SEARCH ... USING INDEX` afterward may mean.
-- Name the rows expected before and after, and why the plan alone cannot prove a speed gain.
-- Give one possible cost of the index and explain what it cannot prove about who created authorizations.

exemple.sql

Declarative lesson asset; this is not a file created in the Python filesystem.

CREATE TABLE authorizations (
    id TEXT PRIMARY KEY,
    actor_id TEXT NOT NULL,
    event_key TEXT NOT NULL
);
INSERT INTO authorizations VALUES ('A1', 'HERO-ID', 'EXIT-7');
INSERT INTO authorizations VALUES ('A2', 'HERO-ID', 'EXIT-8');
INSERT INTO authorizations VALUES ('A3', 'OTHER', 'EXIT-9');

EXPLAIN QUERY PLAN
SELECT id FROM authorizations WHERE actor_id = 'HERO-ID';
CREATE INDEX idx_authorizations_actor ON authorizations(actor_id);
EXPLAIN QUERY PLAN
SELECT id FROM authorizations WHERE actor_id = 'HERO-ID';
SELECT id FROM authorizations WHERE actor_id = 'HERO-ID' ORDER BY id;