Skip to content

Command for targeted merging of small parts in a partition #79697

Closed

Description

@cergxx

Company or project name

We use ClickHouse for storing structured logs.

The primary key looks like this (columnA, columnB, time), and partition by is toStartOfHour(time), we use max_bytes_to_merge_at_max_space_in_pool = 15032385536.
Each shard stores 36h of data on ssd and a few days on object_storage.
We do not insert any rows to partitions older than 24 hours.

Use case

We noticed that timings of reading from a single partition on object_storage are proportional to the number of parts in that partition, so we've been trying to minimize the number of parts.
Since we do not insert any rows to partitions older than 24 hour and the data retention period on SSD is 36 hours we have a 12-hour window to optimize the data before it is moved to cold storage.

As far as I understood ClickHouse has a heuristic that excludes small parts from merges during merge planning. This makes sense in general, but for us it is an obstacle in optimizing reads from object_storage disk.

If nothing is done, by the time the data is moved to S3, a typical partition may have parts of the following sizes:

┌─name───────────────────┬─formatReadableSize(bytes_on_disk)─┐
│ 1745773200_196_284_2   │ 11.51 GiB                         │
│ 1745773200_120_195_2   │ 9.70 GiB                          │
│ 1745773200_408_704_4   │ 8.74 GiB                          │
│ 1745773200_55_119_2    │ 8.18 GiB                          │
│ 1745773200_350_407_2   │ 7.42 GiB                          │
│ 1745773200_0_54_2      │ 6.24 GiB                          │
│ 1745773200_285_332_2   │ 5.35 GiB                          │
│ 1745773200_705_834_3   │ 2.19 GiB                          │
│ 1745773200_835_953_3   │ 1.74 GiB                          │
│ 1745773200_333_341_1   │ 1.12 GiB                          │
│ 1745773200_342_349_1   │ 1.08 GiB                          │
│ 1745773200_954_986_2   │ 641.47 MiB                        │
│ 1745773200_987_1023_2  │ 505.73 MiB                        │
│ 1745773200_1024_1054_2 │ 383.91 MiB                        │
│ 1745773200_1055_1092_2 │ 368.24 MiB                        │
│ 1745773200_1093_1120_2 │ 194.47 MiB                        │
│ 1745773200_1121_1126_1 │ 26.95 MiB                         │
│ 1745773200_1127_1132_1 │ 23.02 MiB                         │
│ 1745773200_1133_1138_1 │ 16.10 MiB                         │
│ 1745773200_1139_1144_1 │ 5.91 MiB                          │
│ 1745773200_1145_1153_2 │ 2.65 MiB                          │
└────────────────────────┴───────────────────────────────────┘

If we take action – for example increasing max_bytes_to_merge_at_max_space_in_pool or using the min_age_to_force_merge_seconds setting — we can achieve the following distribution:

┌─name───────────────────┬─formatReadableSize(bytes_on_disk)─┐
│ 1744304400_38_264_3    │ 29.73 GiB                         │
│ 1744304400_265_754_5   │ 26.99 GiB                         │
│ 1744304400_0_37_2      │ 4.50 GiB                          │
│ 1744304400_755_878_3   │ 2.10 GiB                          │
│ 1744304400_879_1011_3  │ 1.86 GiB                          │
│ 1744304400_1012_1143_3 │ 1.62 GiB                          │
│ 1744304400_1144_1183_2 │ 412.63 MiB                        │
│ 1744304400_1184_1217_2 │ 219.44 MiB                        │
│ 1744304400_1226_1250_2 │ 104.10 MiB                        │
│ 1744304400_1218_1225_1 │ 53.48 MiB                         │
│ 1744304400_1251_1265_2 │ 45.11 MiB                         │
│ 1744304400_1266_1284_2 │ 38.97 MiB                         │
│ 1744304400_1285_1310_5 │ 22.91 MiB                         │
│ 1744304400_1311_1313_1 │ 176.70 KiB                        │
└────────────────────────┴───────────────────────────────────┘

What stands out is that there is a very long tail of parts smaller than 1 GiB. In the second example, we could merge all parts smaller than 5 GiB into a single 10 GiB part and end up with just 3 parts. But these small parts are skipped because of the heuristic.

Describe the solution you'd like

I suggest to add a new optimize command that would trigger a single merge in the specified partition, selecting X smallest parts so that they are merged into one part not exceeding max_bytes_to_merge_at_max_space_in_pool in size.

An alternative solution I'd like would be a setting like min_age_to_force_merge_seconds which would be defined in terms of partition's age, that would say "this partition is not going to change, please optimize its long tail of parts".

Describe alternatives you've considered

I tried disabling heuristic merge_selector_enable_heuristic_to_remove_small_parts_at_right and setting min_age_to_force_merge_seconds to a rather small number, together they did what I wanted, but they affected virtually all my partitions, not only those that I wanted. As a result there were meaningless merges taking place.

Disabling heuristic alone did not work unless using min_age_to_force_merge_seconds.

Additional context

No response

Version info

Activity

  1. added
    comp-mergetree
    on Sep 4, 2026
  2. added
    external
    on Sep 11, 2026
  3. added
    comp-merges
    and removed
    comp-mergetree
    on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-mergesexternalfeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions