Skip to main content
This page describes ClickHouse 26.10 and later.
With automatic parallel replicas, ClickHouse decides for each query whether to run it on all replicas of your service or on a single one. It looks at the statistics of earlier runs of the same query, and uses parallel replicas only where they are expected to make the query faster.

Why automatic parallel replicas

In ClickHouse Cloud every replica of a service can read all of its data, but by default a query runs on the one replica that received it. Parallel replicas let one query use all replicas at once: each replica reads a share of the data, processes it, and sends its partial result to the replica that received the query, which combines them. Whether that pays off depends on the query:
  • A query that reads a lot of data and reduces it to a small result, such as a query with very selective filters or a low-cardinality aggregation, can run several times faster with parallel replicas.
  • A query that reads little data, or whose replicas have to send back almost as much as they read, such as a GROUP BY over a very high-cardinality column, usually gets slower: the extra network transfer and the final merge cost more than the parallel reading saves.
Turning parallel replicas on with enable_parallel_replicas = 1, for a user, a profile, or the whole service, is all-or-nothing: the setting applies to every query, the ones that get faster and the ones that get slower. Automatic parallel replicas make the choice per query instead.

What it does

When automatic parallel replicas are enabled:
  • The first time a query runs, it runs on one replica as usual. While it runs, ClickHouse collects certain execution statistics and remembers them in an in-memory cache.
  • The next time the same query runs, ClickHouse compares two estimates, one replica against all replicas, and picks the faster one.
  • When in doubt, the query runs on one replica, exactly as without the feature. A query is never moved to parallel replicas unless the model predicts that it pays off.
You don’t change your queries. The ones that would get slower with parallel replicas keep running on one replica, thus the name “automatic”.
The runs of one query: the first run collects statistics on one replica, the second switches to three replicas, later runs reuse the decision, and a large change in the data collects the statistics again

Enabling automatic parallel replicas

Automatic parallel replicas build on the regular parallel replicas setup, plus one setting:
In ClickHouse Cloud, cluster_for_parallel_replicas is default. max_parallel_replicas is the largest number of replicas a query may use; values above the number of replicas in the service are capped to it. To enable it for every query of a user, put the settings into a settings profile:

Modes

enable_parallel_replicas = 2 forces parallel replicas for a query and turns the automatic decision off. Use it to ensure that the query will use parallel replicas regardless of the automatic decision. To enforce the opposite (execution on a single replica), use SETTINGS enable_parallel_replicas = 0. SETTINGS automatic_parallel_replicas_mode = 0 is not the same: it turns the automatic decision off, and with enable_parallel_replicas = 1 the query then uses parallel replicas whenever it supports them.

Examples

The examples below come from ClickBench and TPC-H runs on ClickHouse Cloud services. Times are the fastest of several runs. With parallel replicas forced on, two ClickBench queries that read about as much data move in opposite directions. The difference is how much the replicas have to send back:
Two ClickBench queries with parallel replicas forced on: one reads 428 MB, sends back 0.2 MB per replica and gets 2.3 times faster; the other reads 0.94 GB, sends back 706 MB per replica and gets 2.8 times slower
This simple observation became the foundation for the current version of the cost model. During the first execution ClickHouse measures how much data was read and how much data replicas would send to the replica that received the query if parallel replicas were used. This information is enough to tell these two cases apart.

A query that switches

The query reads 428 MB, but the filter and the GROUP BY reduce that to about 0.5 MB of partial results. On a service with three replicas, splitting the reading three ways saves far more than sending 0.5 MB costs, so ClickHouse switches to parallel replicas: the query goes from ~200 ms on one replica to ~100 ms.

A query that stays on one replica

This query reads ~1 GB, but almost every (WatchID, ClientIP) pair is unique, so the partial results the replicas would send back are ~2 GB, more than they read. ClickHouse keeps the query on one replica: it takes ~500 ms, the same as without automatic parallel replicas. Forcing parallel replicas on this query makes it 2.8 times slower, ~1,400 ms.

A query that reads the same data on every replica

For some queries parallel replicas split the reading of only one table between the replicas. Every other table in the same query is read in full by each replica. The time estimate alone may say parallel replicas would be faster, but the lion’s share of the reading would be duplicated on every replica. In such cases, ClickHouse also keeps the query on one replica.

How it works

Every time a query is planned, ClickHouse goes through these steps:
  1. Size check. ClickHouse estimates how much the query will read. Under N MiB per replica (automatic_parallel_replicas_min_bytes_per_replica, 1 MiB by default), parallel replicas can’t pay off, and the query runs on one replica without further work.
  2. Statistics. If there are no statistics for this query yet, it runs on one replica and ClickHouse records two numbers: the compressed bytes it reads, and the compressed bytes replicas would have to send back.
  3. Decision. If there are statistics, ClickHouse compares the time to read everything on one replica with the time to read a share on each replica plus sending the results back, and picks the faster one. A query that was estimated to be faster with parallel replicas, but that would also read most of its data on every replica in full, stays on one replica.
The first run of a query measures on one replica and stores statistics; the next run of the same query looks them up, compares the estimated cost on one replica and on three replicas, and switches to parallel replicas
Statistics are kept in memory on each replica, per query (see Limitations for more details). If the amount of data a query reads changes a lot, for example because the table grew, the statistics are collected again before the next decision. Making the decision costs time, because ClickHouse plans the query a second time (with parallel replicas enabled) to compare the two ways to run it. To keep this cheap for queries that end up on one replica, ClickHouse skips it for queries that are too small, and remembers queries that it already decided to keep on one replica. The defaults suit most workloads. The regular parallel replicas settings apply as well: max_parallel_replicas, cluster_for_parallel_replicas, and parallel_replicas_local_plan, which must stay enabled (the default). parallel_replicas_min_number_of_rows_per_replica is ignored when the automatic mode is on.

Observability

Each query records what automatic parallel replicas did in the ProfileEvents column of system.query_log. This query shows the latest queries with their events:
The most useful events: For more detail, the decisions are logged at Trace level by the AutoParallelReplicas logger, including the reason a query was skipped and the numbers the decision was based on. Run a query with SETTINGS send_logs_level = 'trace', or search system.text_log for logger_name = 'AutoParallelReplicas'.

Limitations

  • The first run doesn’t benefit. A query is decided on its second run on a replica. Queries that run only once always run on one replica.
  • Statistics are per replica and in memory. If your queries go to different replicas, each replica collects its own statistics first. The storage is an in-memory SLRU cache that can keep up to 1 million entries. The cache is lost when a replica restarts.
  • Only queries that parallel replicas support. For example, queries with FINAL always run on one replica. See the parallel replicas limitations.
  • Some query shapes stay on one replica. Queries with some steps, such as UNION, DISTINCT or ARRAY JOIN, or with GLOBAL IN and GLOBAL JOIN, are not considered by automatic parallel replicas yet. Support for more shapes will be added in new releases.
  • Compute-heavy queries can be missed. The decision is based on bytes read and sent back, not on processing time. A query that does heavy processing on little data can stay on one replica although it would be faster with parallel replicas. Setting enable_parallel_replicas = 2 forces parallel replicas for such a query.

What’s next

Automatic parallel replicas are generally available, and we continue to improve them. The next steps address the limitations above:
  • Count processing time, not only bytes. ClickHouse will also measure how much CPU time each part of a query takes, so that compute-heavy queries can be moved to parallel replicas too.
  • Support more query shapes. Queries with UNION, aggregations that spill to disk, and other shapes that stay on one replica today.

Troubleshooting

A query never uses parallel replicas

Run the query a few times, then look at its events with the query above and messages logged by the AutoParallelReplicas logger:
  • AutoParallelReplicasSkippedDueToSettings: check the setup in Enabling automatic parallel replicas.
  • AutoParallelReplicasNoStatistics on every run: the runs land on different replicas, or the query changes from run to run, for example due to a filter on a non-deterministic function.
  • AutoParallelReplicasSkippedEarly or AutoParallelReplicasRejectedByThreshold: the query is too small for parallel replicas.
  • AutoParallelReplicasRejectedByCostModel or AutoParallelReplicasRejectedByDuplicatedReads: ClickHouse estimated that one replica is faster. To check, run the query with SETTINGS enable_parallel_replicas = 2 and compare the time.
  • AutoParallelReplicasSkippedDueToQuery or AutoParallelReplicasPlansNotComparable: the query has a shape that isn’t supported. See Limitations.

A query got slower after switching to parallel replicas

Confirm with AutoParallelReplicasApplied that the query switched, then compare with the query on one replica by adding SETTINGS enable_parallel_replicas = 0. If one replica is faster, keep that setting for the query and report the case on GitHub with the comp-parallel-replicas label.

The same query is fast sometimes and slow other times

The first run of a query on each replica collects statistics and runs on one replica, so it can be slower than the following runs. The same happens again when the amount of data the query reads changes a lot and the statistics are collected again.

Small queries got a little slower

Deciding costs some planning time. Check AutoParallelReplicasOptimizationMicroseconds and AutoParallelReplicasPlanBuildMicroseconds. If small queries on your service are affected, raise automatic_parallel_replicas_min_bytes_per_replica so that more of them skip the decision.