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
- Write the command creating the index on
actor_idand the query finding IDs linked toHERO-ID. - Say what
SCANbefore andSEARCH ... USING INDEXafterward 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.
How to check your work
CREATE INDEX idx_authorizations_actor ON authorizations(actor_id);and aSELECT ... 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;