Reordering Primary Keys With Projections
Projections give a second query pattern its own index, at a price.

Projections give a MergeTree table another access path while keeping its base sort order the same. Their value comes from giving a secondary query pattern its own primary index. Choosing one well means understanding what that index can skip, what the projection must store, and whether the query benefit pays for the extra writes.
Why a MergeTree table can only be physically sorted one way
A MergeTree table’s ORDER BY key sets the order in which its rows are stored. That order gives the sparse primary index its leverage, because nearby rows share a position in the sorted sequence. A query whose predicate aligns with the key can rule out stretches of data without reading them. A different predicate may offer no such shortcut. The same physical rows can have only one order at a time.
Consider an events table sorted by timestamp. A query for a time range benefits from that arrangement, while a query for one user_id may need events scattered throughout it. Without a useful indexed path for that user filter, the engine can end up scanning the full column across all parts. Choosing timestamp as the sort key serves one pattern but does not make the other disappear.
What projections are and the two forms they take
A projection is a hidden, query-optimized copy of data stored inside the same parts as the base table. It has its own sort order and sparse primary index. There are two distinct forms to understand. A normal projection stores the same rows in a different order, while an aggregating projection stores rolled-up rows. The first changes how data is reached, while the second also changes how much detail is stored.
For the events table, a normal projection sorted by user_id gives user lookups their own indexed path. An aggregating projection instead serves queries whose grouping and aggregate functions fit its precomputed results. These are different answers to different questions. Does the query need individual events, or does it need an aggregate over them? That distinction comes before any decision about how to arrange the projection.
The optimizer decides at query time whether the base table or a projection better serves the query. Applications therefore benefit from the alternative copy automatically, a convenience that has a condition. The projection must fit the query closely enough for the optimizer to choose it.
How granules and sparse indexes work inside a projection
The engine reads columnar data granule by granule. A sparse primary index records the key at the first row of each granule. That keeps the index small enough to fit in memory while still describing the boundaries of the sorted data. It is a map of ranges. Its usefulness depends on whether the predicate can exclude those ranges.
In the events projection sorted by user_id, rows for a user sit together in the projection’s order. For a query filtering on that user, the engine reads the projection’s index and identifies the relevant granule range. It then reads those granules and skips the ranges that do not match. The base table’s timestamp order cannot provide that same shortcut for the user-only filter. Reordering gives the index information it did not have before.
Without a useful sort order, the engine still has to read every granule that could satisfy the predicate, potentially the full column. Aggregating projections reduce work in a different way as well. Their rolled-up rows take up fewer granules than the raw data, so even reading an entire aggregating projection can read far less than computing the aggregate from the base table.
The storage and write costs that make projection choice non-trivial
A normal SELECT * projection stores a full second physical copy of every row in its new order. It roughly doubles per-replica storage for the table. With three such projections, there are four copies of the data. The extra copies are useful only to the extent that their different arrangements serve queries worth accelerating. Storage grows with each arrangement, whether queries use it or not.
The cost also reaches the write path. Every insert must maintain the projections, and mutations can amplify the work further. A delete that affects a lot of base data must also rewrite every projection that holds those rows. With more full-row projections, more bytes must be written and the mutation queue may grow longer. A faster read path is not a free addition to a busy table.
Replication multiplies that footprint across the cluster. A three-replica cluster with one normal full-row projection stores six copies of the data, and each replica must do the work to keep the projection up to date. Aggregating projections are typically much smaller because they store rolled-up rows. Where the query needs those aggregates rather than individual events, that difference can make them the better exchange. The required result determines which cost is justified.
The _part_offset projection type introduced in version 25.5 and what it changes

Version 25.5 introduced _part_offset projections, which store only sort key columns and a row pointer back into the base part. Their footprint drops to roughly that of a skip index while retaining most of the query-speed benefit of a full-row projection. The alternative order remains, but the duplicated payload disappears. That gives large tables an option where a full SELECT * projection would be too expensive.
The read path changes to make this possible. The engine uses the projection’s primary index to find matching row offsets, then reads every base granule that holds a matching offset to fetch the other columns. There is a second I/O hop. As a result, query cost is slightly higher than with a full-row projection, which already holds the requested row data in its alternative order. When every column is needed immediately and latency is the main constraint, the full-row form may still win.
This narrower representation changes the earlier mutation and replication calculation. A table with many mutations can get the other indexed order without a full copy of every row, and replicas store the smaller projection in place of another full payload. Maintenance costs remain part of what must be priced, giving engineers a middle option between a cheap but less precise skip index and a fast but expensive full-row projection.
How the optimizer decides when to use a projection
The optimizer evaluates the available paths at query time and favors the sort order that allows the most granule skipping for the predicates. The columns that come first in the sort order matter most when designing the projection. A projection sorted by columns the query does not filter on may not help it run faster. Its storage cost still applies.
What happens if the projection sorts on a boolean column? With very low cardinality, that order can offer poor granule skipping and may never earn selection. For a query that gains no skipping advantage from that order, the projection will not be used merely because it is available. Particular column types remain eligible despite this design warning.
Aggregating projections face a separate matching requirement. The query’s GROUP BY keys must be a subset of, or exactly match, the projection’s grouping keys, and its aggregation functions must match the precomputed aggregates. A mismatch sends the query to the base table instead. An apparently relevant rollup can therefore remain unused.
Projections versus skip indexes versus materialized views, and when each applies
Skip indexes work with the existing physical order. They are cheap to store and write, and they can be effective when the data has natural locality in that order. Their mechanisms differ. A bloom_filter offers probabilistic granule skipping, while minmax and set offer deterministic skipping. But when the access pattern has no link to the sort key, there may be little locality for a skip index to use.
A normal projection addresses that mismatch by creating the alternative order itself. Its sparse index can skip granules precisely along that order, making it much faster for access patterns that would otherwise scan most granules. Full-row storage is the trade-off. The _part_offset variant reduces that cost, with the extra base-data fetch discussed earlier. For a filter the base sort key does not cover, a normal projection is the simplest path when either storage form fits the requirement.
Materialized views answer needs beyond a different indexed order. They are appropriate when the access pattern requires joins, transformations that projection syntax cannot express, or a separate table with its own retention or partitioning policy. Aggregating projections offer a narrower alternative for slow GROUP BY queries. They precompute aggregates and are maintained automatically on insert inside the base table’s parts, without a separate ingestion pipeline. In exchange, they do not provide independent control over their lifecycle.
Backfilling projections on existing data and the operational steps involved
Once the design is chosen, deployment introduces a separate concern. ALTER TABLE … ADD PROJECTION adds the definition, but it applies only to new data inserted after the statement. Existing parts are not automatically rewritten. The statement can succeed right away while the projection is still unusable for old data. That gap is where an otherwise sound design can surprise operators.
Covering existing data requires an explicit MATERIALIZE PROJECTION command. Materialization rewrites existing parts to include the projection, making it a significant I/O and CPU operation on a large table. It runs in the background. The work uses the same resources as ongoing merges and inserts, so those resources need to be planned for before it starts.
Materialization is processed as a mutation, which makes the mutation queue the place to monitor its progress. An already-long queue combined with ongoing inserts can leave the operation running for a long time. Older parts may still use the base path while that work is running. The background mutation needs to be tracked until it finishes, even after the ALTER succeeds, to confirm that the backfill is done.
Deciding before you deploy — the cost calculation that prevents wasted space
Before deployment, the calculation starts with the size of the base table on each replica. For full-row projections, multiply that size by the number being added and then by the replica count to estimate additional cluster storage. Add the replicated base table to work out how much storage the whole setup will use once it is built. Is that cost acceptable for the query being served? If not, the next candidates are _part_offset or aggregating projections, depending on whether the query needs rows or rolled-up results.
The next check is rewrite amplification under the table’s actual update and delete workload. Estimate how much work each projection adds to each mutation cycle, especially when frequent deletes already cause a lot of work. High delete rates combined with multiple full-row projections can build a backlog that affects query freshness. Then test the intended read benefit on a sample with EXPLAIN. The leading predicate columns should align with the projection’s sort key, and the plan should show that the optimizer selects it.
Query frequency puts those costs in perspective. A query run once a day on a small result set is not worth the extra write amplification on every insert. Projections earn their cost by accelerating high-frequency or high-latency queries.
Deployment architecture also changes the storage calculation. ObsessionDB is one managed option with compute-storage separation over object storage, where data lives once in durable storage and a cache mesh serves reads. More compute can thus be added without more full copies of each table and its projections. That changes the replication multiplier, but not the need to justify the projection itself.


