ClickHouse projection: full copy vs _part_offset index, and when each one pays off

A lightweight ClickHouse projection finds 10,000 matching rows in a billion, then still reads 7.9% of the table. A full copy reads 3 granules. The math.

A table can only be sorted one way. When half your queries filter by user_id and the table is sorted by timestamp, ClickHouse offers a second sort order inside the same table: a projection. Since 25.5 there are two kinds. A full projection keeps a second, re-sorted copy of the rows. A lightweight projection (_part_offset, or INDEX ... TYPE basic since 26.1) keeps only the sort key and a pointer back into the table.

The usual advice is that the lightweight kind “reduces storage overhead at the cost of slightly more I/O at query time” (docs). This post puts a number on “slightly”. The pointer leads back to the base table, where the matching rows are exactly as scattered as before. Look up a value with 10,000 matching rows in a billion-row table, and a lightweight projection still reads 7.9% of the table’s granules: 79 million rows. A full projection reads 3 granules.

This is day 7 of the ClickHouse series. I read the projection code at tag v26.10.2.103-stable, the docs and ClickHouse’s own published examples. The percentages below are arithmetic on those definitions, not benchmarks. Measured numbers are other people’s and are linked.

The short answer

Full projection Lightweight projection No projection
Stores every selected column, re-sorted sort key + _part_offset (UInt64) —
Extra disk about one more copy of the selected columns the key column plus a pointer per row 0
Lookup with m matching rows reads about ⌈m/8,192⌉ + 1 granules reads every base granule that holds a match reads every granule
1,000 of 1 billion rows match 2 granules 996 granules (0.8%) 122,071
100,000 match 14 granules 68,266 granules (56%) 122,071
Pays off when many rows match, or disk is cheap very few rows match (IDs, rare values) —

Both kinds are skipped by FINAL, SAMPLE and pending mutations, and both block lightweight DELETE and ReplacingMergeTree merges by default. The list is further down.

What each kind stores

A projection is “an additional, hidden table to the original table” (docs), built part by part: every insert writes the part and, next to it, one projection part per projection. Merges merge both. Parts that don’t have the projection, for example parts written before ADD PROJECTION and never MATERIALIZEd, are read the normal way: “If a specific part lacks the corresponding projection part, query execution falls back to the original part” (ALTER PROJECTION).

CREATE TABLE events
(
    ts       DateTime,
    user_id  UInt64,
    url      String,
    duration UInt32,
    -- full: a second copy of all columns, sorted by user_id
    PROJECTION by_user_full (SELECT * ORDER BY user_id),
    -- lightweight, 26.1+ syntax: only user_id + _part_offset
    PROJECTION by_user INDEX user_id TYPE basic
)
ENGINE = MergeTree
ORDER BY ts;

The INDEX ... TYPE basic form is shorthand. In the source it builds exactly SELECT _part_offset ORDER BY user_id (ProjectionIndexBasic.cpp). The offset is stored as a UInt64 column called _parent_part_offset (ProjectionsDescription.cpp). The _part_offset form was allowed from 25.5 behind a setting and became the default in 25.8 (allow_part_offset_column_in_projections, MergeTreeSettings.cpp).

How a lightweight projection reads

The query WHERE user_id = 42 runs in two steps:

  1. In the projection. Its primary index on user_id finds the few projection granules that can hold 42. They hold user_id and offsets only, so this is cheap.
  2. In the base table. The offsets say which rows of the part match. Since 26.1 ClickHouse uses them for granule-level pruning of the base table (ClickHouse blog, 2026-01-09). Every base granule that holds at least one matching offset is read; the others are skipped.

Step 2 is the catch. The base table is sorted by ts. User 42’s rows sit wherever their timestamps put them. A projection can’t move them; only a full copy can.

The same 5 matching rows, read two waysfullprojectiona sorted copy of the rows: the 5 matches sit in 1 granule, read 1lightweightprojectionkey + offsetbase tablethe projection finds 5 offsets, then the base table reads every granule that holds one: 5granule readgranule skipped
A full projection pays in disk to keep matching rows together. A lightweight one stores only key + offset and goes back to the base table, where the matches are as scattered as the table's ORDER BY leaves them. Not to scale.

The formula: the same one as skip indexes

This is the arithmetic from day 6 on skip indexes. A value that matches m of N rows, at random positions in the table’s sort order, appears in a given 8,192-row granule with probability 1 − (1 − m/N)8192. That is the share of base granules a lightweight projection reads.

A full projection doesn’t have this problem. Its rows are sorted by user_id, so the m matches are contiguous and fill about m/8,192 granules, plus one when they straddle a boundary.

For a 1-billion-row table (122,071 granules of 8,192 rows, one part):

Rows that match Lightweight: base granules read Rows read from the base table Full projection: granules read
10 10 (0.01%) 82 thousand 2
100 100 (0.08%) 819 thousand 2
1,000 996 (0.82%) 8.2 million 2
10,000 9,601 (7.9%) 78.7 million 3
100,000 68,266 (56%) 559 million 14
1,000,000 122,037 (99.97%) ~1 billion 124

Granules read, from 1 − (1 − m/N)8192 and ⌈m/8,192⌉ + 1. Real tables have many parts, and both kinds read at least one granule in every part that holds a match.

WHERE key = x on 1 billion rows: share of granules read0%25%50%75%100%101001k10k100k1M10Mrows that match (scattered at random in the table's sort order)1M: projectionindex limitlightweight (_part_offset): 0.82% of granules at 1,000 matching rowslightweight (_part_offset): 7.87% of granules at 10,000 matching rowslightweight (_part_offset): 55.92% of granules at 100,000 matching rowsfull projection (SELECT *): 0.00% of granules at 1,000 matching rowsfull projection (SELECT *): 0.00% of granules at 10,000 matching rowsfull projection (SELECT *): 0.01% of granules at 100,000 matching rowsno projection: every granule (dashed)lightweight projection: base granules holding a match: 7.9% at 10k, 56% at 100kfull projection: matches sit together: 3 granules at 10k, 14 at 100k
Arithmetic, not a measurement: 1 − (1 − m/N)8192 for the lightweight projection, ⌈m/8192⌉ + 1 granules for the full one, one part assumed. A table with many parts reads at least one granule per part either way.

Up to about a thousand matches the two kinds are close: each extra granule is small, and both read only the columns the query asks for. From ten thousand matches on, the lightweight projection reads tens of millions of rows to return ten thousand. From a hundred thousand on, it is half a full scan.

ClickHouse knows this. Two settings, both new in 25.11 and both 1,000,000 by default, decide when the lightweight read is tried at all (Settings.cpp):

  • max_projection_rows_to_use_projection_index: use the offsets only if at most this many rows are read from the projection.
  • min_table_rows_to_use_projection_index: and only if at least this many rows would be read from the part without it.

Both are checked per part (projectionsCommon.cpp). Two consequences follow:

  • On parts smaller than about a million rows, a lightweight projection never prunes granules. It can still drop whole parts that hold no match (that part-level filtering has existed since 25.6, optimize_use_projection_filtering), but inside a part it reads everything. Fresh tables with many small parts, and tables partitioned by day with small partitions, get much less from it than the examples suggest.
  • Above a million projection rows it stops being used as an index, which on a billion-row table is where it would read 99.97% anyway.

What the published examples show

ClickHouse’s own measurements use the UK property prices dataset (about 30 million rows):

Source Query No projection Full Lightweight
Mark Needham, ClickHouse video, 25.6 filter on a non-key column 131 ms 6 ms 13 ms
Tom Schreiber, ClickHouse blog, 26.1, two lightweight projections combined town + date filter 0.077 s, 30.73 million rows — 0.010 s, 16.38 thousand rows

The video adds that the lightweight projections “use half the storage space” of the full ones. Both examples are selective filters, the left side of the chart, where lightweight projections do well. In the blog, the date projection selects 7 granules, the town projection 5, and ClickHouse intersects them. Neither example tests a value that matches 1% of a billion rows.

The storage side: what “lightweight” costs

A full SELECT * projection is one more copy of every column. Re-sorted, each column compresses differently: better for columns that follow the new key, worse for the ones that followed the old one. Expect roughly a doubling. You can project only the columns your queries need (SELECT user_id, url, duration ORDER BY user_id), and the projection is then used only for queries that need no other column.

A lightweight projection stores the key column plus a UInt64 per row: 8 bytes per row before compression. That pointer is also hard to compress. Within each key value, the offsets increase, but across the whole column they jump around the part. Information theory puts a floor under it: a pointer column for a key uncorrelated with the table order needs at least as many bits per row as the key’s entropy. For a unique key in a part of 10 million rows, that is log2(107) − 1.44 ≈ 21.8 bits, 2.7 bytes per row, whatever the codec. For a key with 1,000 equally common values, about 10 bits. (A lower bound, not a measurement: how close LZ4 or ZSTD get to it on real offsets, I could not find published.)

So “lightweight” is light next to a wide table. Next to a narrow one, say an events table at 10-20 compressed bytes per row, the projection can add a noticeable fraction anyway. Check it:

SELECT name,
       formatReadableSize(sum(data_compressed_bytes))   AS on_disk,
       formatReadableSize(sum(data_uncompressed_bytes)) AS raw,
       sum(rows) AS rows
FROM system.projection_parts
WHERE table = 'events' AND active
GROUP BY name;

Inserts pay for both kinds: each projection is sorted and written as its own part on every insert, and merged on every merge. I found no published measurement of that insert cost for either kind, so I won’t put a number on it. One related setting from 26.4 says what grows: finalize_projection_parts_synchronously exists because keeping every projection’s output open during an INSERT “increases peak memory proportional to the number of projections” (Settings.cpp).

When ClickHouse won’t use your projection

canUseProjectionForReadingStep in projectionsCommon.cpp lists the cases where no projection is considered at all. Among them (26.10):

Query or table state Why
SELECT ... FINAL “the query uses FINAL”
SAMPLE “the query uses SAMPLE”
a pending ALTER UPDATE / DELETE mutation, or patch parts “the table has unmaterialized mutations or patch parts”, for the whole table until it finishes
parallel replicas without parallel_replicas_local_plan projection support needs the local plan
aggregate_functions_null_for_empty = 1 changes aggregate functions implicitly

Then, among the projections that remain, ClickHouse picks the one that “requires the least amount of data to be read” (docs). A projection that would read more marks than the table is ignored.

And two table-level rules, both throw by default (MergeTreeSettings.cpp):

  • lightweight_mutation_projection_mode = throw: lightweight DELETE FROM fails on a table with projections. Set drop (the touched parts lose the projection until rebuilt) or rebuild.
  • deduplicate_merge_projection_mode = throw (since 24.8, before that ignore): creating a projection on ReplacingMergeTree, CollapsingMergeTree and the other non-plain engines fails, because a merge that removes rows would leave the projection out of sync. Before 24.8 it was allowed and “might result in incorrect answer”.

To see why a projection was skipped, force it. With force_optimize_projection = 1 the query fails with PROJECTION_NOT_USED and a message of the form “projection name is rejected because reason” (optimizeTree.cpp):

EXPLAIN projections = 1, indexes = 1
SELECT count() FROM events WHERE user_id = 42;

SELECT count() FROM events WHERE user_id = 42
SETTINGS force_optimize_projection = 1;   -- fails with the reason if none is used

SELECT query, projections FROM system.query_log
WHERE type = 'QueryFinish' AND has(tables, 'default.events')
ORDER BY event_time DESC LIMIT 5;

Which one to build

  • Count your matches first. SELECT count() FROM events WHERE user_id = 42 for a typical value. Under about 1 in 100,000 rows (10,000 in a billion), a lightweight projection reads under 8% of the table and costs little disk. Above 1 in 10,000, it reads most of the table; build a full projection or rethink the sort key.
  • A full projection for “group by / filter on a second dimension”. Dashboards that read 1% of rows by country need the rows together, not pointers to them.
  • A lightweight projection for ID lookups. User IDs, order IDs, trace IDs on a table sorted by time: few matches per value, and the pointer is cheaper than a second copy.
  • Consider a skip index too. For rare values a bloom_filter reads about the same granules (plus ~2.4% false positives) at about 1 byte per distinct value per granule, and works on any engine. A lightweight projection has no false positives and handles ranges (user_id BETWEEN ...), which a Bloom filter can’t.
  • Check the engine and your deletes before you add any projection. On ReplacingMergeTree, or with lightweight deletes, decide on drop or rebuild first.
  • Run MATERIALIZE PROJECTION after ADD PROJECTION. Until then old parts are read without it.

FAQ

What is a _part_offset projection in ClickHouse? A projection that stores only its sort key and each row’s position in the part (_part_offset), instead of a copy of the row. It works like a secondary index: find positions in the projection, then read those rows from the table. Since 26.1 it can be written PROJECTION p INDEX col TYPE basic.

Is a lightweight projection slower than a full one? For selective lookups only a little: ClickHouse’s video shows 13 ms vs 6 ms on 30 million rows. For values with many matches, much slower: it reads every base granule that holds a match, 56% of the table at 1 in 10,000 rows.

How much disk does a projection use? A full SELECT * projection is about one more copy of the table. A lightweight one is the key column plus a UInt64 offset per row: 8 bytes raw, and at least about log2(rows per part) − 1.44 bits per row for a unique key, before the key column. Check system.projection_parts.

Why is my ClickHouse projection not used? Most often FINAL, SAMPLE, a pending mutation, parts without the projection (not materialized), or a projection that would read more than the table. Run the query with force_optimize_projection = 1 to get the reason.

Can I use projections with ReplacingMergeTree? Only after setting deduplicate_merge_projection_mode to drop or rebuild; the default throw refuses. Queries with FINAL never use projections.

What I could not verify

  • The granule counts assume matches at random positions with respect to the table’s ORDER BY. Correlated columns (an order ID that grows with time) cluster and read far fewer granules.
  • I found no published measurement of projection insert overhead, of real _parent_part_offset compression, or of a lightweight projection on a value with many matches. The storage floor is information theory, not a test.
  • The docs’ projections page still says multi-projection pruning “only prunes entire parts”; the 26.1 blog post describes granule-level pruning. I follow the blog and the 26.10 source.
  • Settings and behaviour are from the 26.10.2.103 source; the published timings are from 25.6 (video) and 26.1 (blog).

Sources

Next in the series: partition by day, month or nothing.