Arman Pouyaei Start a conversation
Lesson 11  ·  Oct 16, 2026
Data Engineering Fundamentals

Finding rows without reading everything: indexes

Database management, lesson three. Schemas organized tables; keys protected relationships; today’s tool makes big tables fast. Without an index, a filter reads every single row — a full table scan. On ten million rows, that’s a slow query. An index is the database’s book index: a separate, sorted lookup structure on one column that lets the engine jump straight to the matching rows.

The code

-- Without an index, the database checks EVERY row for country = 'Canada'
SELECT * FROM weather_stations
WHERE country = 'Canada';

-- Build a lookup structure on the country column, then re-run
CREATE INDEX idx_stations_country
ON weather_stations (country);

Reading it piece by piece:

  1. CREATE INDEX — “build me a lookup structure”. The first build scans the whole table once; afterwards the database keeps it up to date automatically.
  2. idx_stations_country — just a name. The idx_table_column convention is the industry standard, so use it and future-you will thank you.
  3. ON weather_stations (country) — the table and the column you filter on. Now the re-run of that SELECT skips the full scan and goes straight to Canada’s stations. On a tiny table you’ll feel nothing; on millions of rows it’s the difference between milliseconds and minutes.

In practice

A data engineer indexes the columns that get filtered, joined, or sorted — the WHEREs, ONs, and ORDER BYs of the warehouse’s most-run queries. But there’s a tradeoff: every INSERT, UPDATE, or DELETE now also has to update the index, so writes get a bit slower and the index eats storage. The rule of thumb: index hot read paths, don’t index everything, and don’t bother on tiny tables. Fun bonus: every PRIMARY KEY (lesson 10) already gets an index automatically — that’s partly why key-based joins are fast.

Python bridge: an index is a database’s version of a dict. Looking up my_dict['Canada'] jumps straight to the value; scanning a list to find every Canada entry is a full scan. pandas’ .set_index() gives you the same jump-to-it behavior on a DataFrame column.

Further reading

SQL Create Index Statement Explained with Examples — freeCodeCamp (short walkthrough of the CREATE INDEX syntax plus the key tradeoff: indexes speed up reads but can slow down writes when data changes)

Try it

Practice question: weather_stations has an elevation_m column, and your snowpack dashboard runs this filter constantly: WHERE elevation_m > 1000. Write the CREATE INDEX that speeds it up.

Show answer
CREATE INDEX idx_stations_elevation
ON weather_stations (elevation_m);

Result: Query OK — the index is created (0 rows affected, since DDL statements don’t return rows). From now on, the database jumps straight to stations above 1000 m instead of scanning the whole table. Note the pattern: you indexed the column the query filters on, not the columns it returns.

← All posts