SQL CREATE INDEX

The command that actually builds one.

What & Why

CREATE INDEX builds the index structure from the previous lesson. Once created, it's maintained automatically — you never manually update it yourself.

See How It Works

BUSINESS QUESTION

Growth wants a concrete event-name index proposal backed by the representative event distribution.

iduser_idevent_namecreated_atproperties
1001101signup2024-01-15 09:12:00+00{"source":"organic"}
1002101activated2024-01-16 14:25:00+00{"step":"workspace"}
1003102signup2024-02-10 11:08:00+00{"source":"paid"}
1004103report_viewed2024-03-12 16:40:00+00{"report":"retention"}
EXAMPLE QUERY
-- Proposed DDL: CREATE INDEX idx_growth_events_event_name ON growth.events (event_name);
WITH distribution AS (
  SELECT event_name, COUNT(*) AS matching_rows
  FROM growth.events
  GROUP BY event_name
)
SELECT
  event_name,
  matching_rows,
  ROUND(matching_rows::numeric / NULLIF(SUM(matching_rows) OVER (), 0), 4) AS table_fraction
FROM distribution
ORDER BY matching_rows DESC, event_name;
RESULT — event-name distribution
event_namematching_rowstable_fraction
signup20.5000
activated10.2500
report_viewed10.2500

This read-only result helps evaluate whether an event-name index would be selective.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario