Table Structure

Keys & Indices

Foreign Keys

Partitioning

Housekeeping

Example

DROP INDEX IF EXISTS individuals_state_idx;
DROP INDEX IF EXISTS committees_cmte_st_idx;

-- wicked slow, running time forever...
-- note sequential scans
EXPLAIN SELECT sum(1) as N FROM committees c
JOIN individuals i ON c.cmte_st = i.state
WHERE i.state = 'MA';

-- Build index on big table
CREATE INDEX ON individuals (state);

-- now it uses the index, running time 
EXPLAIN SELECT sum(1) as N FROM committees c
JOIN individuals i ON c.cmte_st = i.state
WHERE i.state = 'MA';

-- Build index on small table
CREATE INDEX ON committees (cmte_st);

-- now it uses BOTH indexes, running time 
EXPLAIN SELECT sum(1) as N FROM committees c
JOIN individuals i ON c.cmte_st = i.state
WHERE i.state = 'MA';