Reducing Part Bloat From Merges and TTL
Aligning partitions with TTL and tuning merges beats rewriting.

Part bloat from merges and TTL is a solvable configuration problem, but an expiry rule alone will not solve it. Scheduling, partition alignment and vertical merge optimization let engineers keep part counts, memory and I/O in check without forcing costly rewrites over and over.
How parts accumulate and why TTL alone does not clean them up
Every insert into a MergeTree table lands as a new immutable part on disk. Background merges are the mechanism that collapses those parts back down. When merges run less often than data arrives, parts accumulate, and when TTL-triggered merges are throttled too aggressively, expired rows remain inside them. Part bloat is therefore primarily a merge scheduling problem that poorly configured TTL can worsen.
High-frequency ingestion makes that imbalance easier to reach. CDC feeds and Kafka consumers keep sending micro-batches, each of which lands as its own part. The table can accumulate parts faster than the default merge schedule drains them, even though the TTL expression correctly identifies when rows should expire. Expiry eligibility and physical cleanup are separate events.
When part counts are high, scans slow down and hold up merges, which lets part counts rise further. Expired data then remains on disk while the work needed to remove it competes with the work needed to keep ingestion moving. The scheduling decision is not just how soon to remove old rows, but how to do so without starving ordinary merges.
What merge_with_ttl_timeout actually controls and where its default goes wrong
merge_with_ttl_timeout sets the minimum interval between consecutive TTL-triggered merges on the same table. Its default is calibrated for general workloads. It is a scheduling limit. A row can pass its TTL boundary and remain on disk because the merge that would remove it has not yet run.
A timeout that is too high leaves TTL-eligible rows sitting in parts long after expiry. Disk usage stays elevated, and part counts do not fall through that cleanup. Lowering the timeout allows TTL-triggered merges to run more often, but those merges consume CPU and I/O that inserts and ordinary background merges also need. Faster scheduling is not free cleanup. If the interval is too short, repeated rewrites can add pressure to the pipeline that is already falling behind.
Tables with TTLs measured in months, partitioned by day or month, can tolerate a looser timeout because avoiding unnecessary merge work can outweigh the lag in expiry. Short TTLs call for closer attention to that lag. The aim is to match the opportunity for cleanup to the rate at which data expires.
Tightening the timeout does little if the background merge thread pool is already occupied by ordinary merges. The TTL work simply waits behind them. A team reviewing expiry lag should therefore examine the timeout and merge thread availability together, because permission to schedule more work is not the same as capacity to execute it.
Why partition alignment determines whether TTL merges do any work at all
MergeTree merges operate within a single partition, so rows in different partitions are never merged together. That boundary limits what any scheduling change can achieve. A TTL rule can delete a row during a merge only when that merge covers the part containing it. Lowering the timeout cannot fix a layout that keeps parts with expired rows out of merges when they need to run.
An unrelated partition key can create exactly that mismatch. If TTL refers to a date column but partitioning follows another dimension, a partition containing expired rows may remain stable, with no new inserts and no size threshold crossed. A merge may then never be triggered at the needed time, leaving expired rows indefinitely. The expiry expression can be correct while cleanup still fails.
Alignment means that the partition boundary and TTL boundary use the same time column at roughly the same granularity. A TTL expressed in months works cleanly with toYYYYMM(event_date), while one expressed in days works cleanly with toYYYYMMDD(event_date). Once the maximum timestamp of an aligned partition is past the TTL, ttl_only_drop_parts allows its wholly expired parts to be dropped, removing the partition as a unit without a row-level rewrite.
Avoiding a rewrite eliminates I/O proportional to column count, particularly when rows have many columns. Finer partitioning does raise a fair objection, since it can create many partitions and raise part counts itself. But partition count and parts per partition are separate problems, and a well-tuned merge schedule can keep the latter low even when the former is high.
How vertical merge optimization reduces the memory cost of TTL on wide tables
A part with both expired and retained rows still needs cleanup that removes only the expired rows. On a wide table, that raises a second concern. Even if the merge runs at the right time, can it remove those rows without processing a large amount of column data at once?
Before vertical merge optimization, TTL DELETE on wide tables was prohibitively memory-intensive because horizontal merging required all columns to be processed together. Horizontal merges read and write every column in the parts being merged at the same time. Peak memory thus grew with the total number of columns multiplied by part size. A table with many metadata columns paid for its width during cleanup as well as during ingestion or queries.
vertical_merge_optimize_ttl_delete removes that all-at-once constraint by processing columns in groups and applying TTL logic group by group. This cuts peak memory usage on wide tables by a large amount. The setting is enabled by default. Wide event schemas and audit tables with dozens of metadata columns can use less memory without a change to their settings, though the rewrite work that remains still needs CPU and I/O.
Whole-part removal skips the row-level rewrite entirely. These optimizations address different costs, so the availability of vertical merging does not remove the reason to align partitions for cheap drops.
The TTL metadata bug that shows why these interactions need explicit testing
Merge efficiency is not the only concern when TTL interacts with other table features. Lightweight updates through ALTER TABLE … UPDATE in lightweight mode change only the affected columns and create a patch part, leaving the whole original part intact. A later merge covering both the original and its patch must reconcile their TTL metadata. That reconciliation is the bug surface.
The failure affects correctness. Depending on merge ordering, rows that should be retained may expire, or rows that should expire may survive. A merge completing is therefore not sufficient evidence that retention behaved correctly. This complication limits any simple recipe that treats a lower timeout and lower disk usage as proof of success.
TTL behavior needs explicit verification after changing TTL settings or introducing lightweight updates on a TTL-governed table. A targeted query on a sample partition should confirm that row counts fall as expected over the first expiry window. A falling count is only useful evidence when it matches the rows that should have expired.
This interaction also supports keeping TTL expressions simple and colocated with the partition key. More complex rules enlarge the reconciliation surface during merges. Simplicity gives the retention check a clearer basis. The goal is to remove expired rows on time and keep rows whose retention window is still open.
Why OPTIMIZE TABLE FINAL is the wrong response to TTL lag and what to do instead
When expired data remains on disk, OPTIMIZE TABLE … FINAL can look like a direct way to make the engine catch up. It forces all parts in each partition into a single part per partition, guaranteeing TTL execution and deduplication. But that guarantee comes with a much broader merge than TTL cleanup alone requires. A problem with when merges run becomes one large I/O operation.
On a large table under active ingestion, that operation competes directly with incoming writes for disk bandwidth. In the worst case, it can block inserts, cause insert timeouts or produce part count spikes as buffered inserts land while FINAL runs. The attempted cure can intensify the pressure.
MATERIALIZE TTL is the safer alternative for applying TTL logic. It targets parts that need that work, respects the existing merge schedule and does not force unrelated parts to merge.
For recurring lag, the long-term response is a configuration review. That review should check whether to tighten merge_with_ttl_timeout, make sure partition alignment is right, enable ttl_only_drop_parts where it applies and check that merge threads are free. Those controls address why cleanup falls behind. MATERIALIZE TTL can apply the rule, but it does not replace a workable balance between incoming parts and background merge capacity.
FINAL still has a legitimate, bounded use. One-time cleanup on a table that will not receive new inserts, such as a retired partition or a migration target, removes the conflict with continuing ingestion. In that setting, the cost is bounded and the guarantee can be useful.

Applying these controls in a streaming ingestion context where parts accumulate fastest
Streaming ingestion through CDC or Kafka puts these choices under sustained pressure. CDC pipelines that emit every intermediate row state, rather than only the final state, multiply the work reaching the table. Each update lands as a new insert. A single logical record change can therefore produce multiple parts before any merge runs, making ordinary consolidation a continuous demand.
For these tables, timeout tuning and thread availability need to be reviewed together. A tight merge_with_ttl_timeout is wasted when ordinary merges from micro-batch inserts saturate the merge thread pool. TTL work still has to wait. Running TTL rewrites more often also uses those same resources, so a shorter interval helps only when the plan leaves room for the merges that inserts need.
Partition granularity shapes how much cleanup must rewrite data. An hourly partition on a high-volume stream produces many small partitions that whole-part removal can clear cheaply. A monthly partition gathers rows from many ingestion cycles and makes part-level TTL drops rare. The choices need to be compared by how well the boundary fits the expiry window and whether merges can keep parts per partition low.
The practical response is to align the partition key with the TTL expression and enable ttl_only_drop_parts where whole-part expiry is applicable. The timeout should match the TTL window, with tighter scheduling for short TTLs, while the review confirms that insert-driven merges have not saturated merge thread availability. On wide tables, vertical merge optimization reduces the memory burden when only some rows have expired. Each control handles a different constraint.
After the first expiry window, row-count validation closes the loop. It checks that the chosen layout and schedule keep and remove rows as intended, including when lightweight updates are used. The target is a pipeline that can keep receiving data, consolidate incoming parts and remove expired rows without turning every retention boundary into an expensive rewrite.


