Learn SQL/Advanced/Query Performance/SQL Single-Column Index

SQL Single-Column Index

The simplest, most common shape — one column, indexed on its own.

What & Why

A single-column index covers exactly one column. It's the right choice whenever queries filter or sort on that column alone, without needing other columns alongside it.

See How It Works

BUSINESS QUESTION

Marketing wants a single-column lead-source index proposal and the distribution used to evaluate it.

idcampaign_idemailcreated_atqualified_atconverted_atlead_scoresourcecountry
3011ana@example.com2024-01-21 09:10:00+002024-01-22 11:00:00+002024-02-02 10:00:00+0086google_adsUS
3021ben@example.com2024-01-24 12:40:00+00NULLNULL52google_adsCA
3032chloe@example.com2024-02-16 08:30:00+002024-02-18 14:20:00+00NULL74emailGB
3043dev@example.com2024-03-20 17:15:00+002024-03-21 09:00:00+002024-04-04 16:00:00+0091linkedinUS
EXAMPLE QUERY
-- Proposed DDL: CREATE INDEX idx_marketing_leads_source ON marketing.leads (source);
WITH distribution AS (
  SELECT source, COUNT(*) AS matching_rows
  FROM marketing.leads
  GROUP BY source
)
SELECT
  source,
  matching_rows,
  ROUND(100.0 * matching_rows / NULLIF(SUM(matching_rows) OVER (), 0), 2) AS row_percentage
FROM distribution
ORDER BY matching_rows DESC, source;
RESULT — lead-source distribution
sourcematching_rowsrow_percentage
google_ads250.00
email125.00
linkedin125.00

The distribution is the real Queryflo input for a single-column index decision.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario