Typorithm · hard

PREWHERE

prewhereclickhousecolumn-storefilter-pushdown
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn PREWHERE

0%

Indexing & Partitioning

PREWHERE reads only its filter columns first to find surviving rows, then reads the remaining columns just for those rows — saving I/O in the column store.

In a column store, reading fewer columns for fewer rows is the main lever for speed, and PREWHERE exploits exactly that.

PREWHERE first reads only the columns in its condition, identifies which rows pass, and then reads the other selected and WHERE columns only for those surviving rows.

ClickHouse can move WHERE conditions into PREWHERE automatically, but writing PREWHERE explicitly lets you put the cheapest, most selective filter first.

01

PREWHERE reads filter columns first.

02

Other columns read only for survivors.

03

Best for cheap, selective filters.

SELECT
    request_id,
    latency
FROM requests
PREWHERE host = 'web-1'
WHERE latency > 100;

Read the implementation

Connect each part of the code to the concept before you type.

1. Read filter column first

PREWHERE reads only the host column first to find surviving rows.

PREWHERE host = 'web-1'

2. Then the rest

WHERE applies to remaining columns only for rows that survived PREWHERE.

WHERE latency > 100

3. Projected columns

Projected columns are read last, only for the surviving rows.

request_id,
    latency

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

PREWHERE: Lesson & Practice · Typorithm | ApplyDjinn