Skip to main content
Since version 26.4, MergeTree tables collect column statistics automatically, and the query optimizer uses them to estimate filters and to choose the order of JOINs. If the compatibility setting of the default profile is set to a version below 26.4, an upgraded server keeps the old default and creates no statistics. You can still enable them table by table: set auto_statistics_types on the table and materialize the statistics for existing parts. The optimizer uses statistics as soon as they exist. In ClickHouse Cloud, the service-level compatibility value is fixed when the service is created and does not change on upgrades, see Version compatibility.

Why the tables have no statistics

compatibility reverts every default that changed after the given version. These are the relevant values in version 26.12: The default of auto_statistics_types became non-empty in 26.4, and allow_statistics was turned on in 26.3. use_statistics stays on with every value of compatibility, which is why statistics are used once they are there. Server-wide defaults of MergeTree settings are derived once from the compatibility value of the default profile. Raising compatibility for a user, a session or a query does not change them, so auto_statistics_types has to be set on each table. To see what your server uses:

Enable statistics on a table

The example below was run on version 26.12 with compatibility = '26.2'. Two tables are joined, and the query names the small table first:
Without statistics the optimizer has no estimate for the filtered customers table and keeps the order of the query, so the hash table is built from the one million rows of orders. First, declare the statistics. This changes only the table metadata and applies to all parts written from now on, by inserts and by merges:
For a new table, add SETTINGS auto_statistics_types = 'basic, uniq_v2' to CREATE TABLE. Then build statistics for the parts that already exist. This is a mutation that reads the columns of every part and writes their statistics. On a large table, start with the columns used in JOIN and WHERE conditions. A compatibility value below 26.3 sets allow_statistics = 0, so the statement needs allow_statistics = 1; without it, it fails with Alter table with statistics is disabled. Turn on allow_statistics.
mutations_sync = 1 makes the statement wait for the mutation; leave it out on large tables and follow the progress in system.mutations. The statistics column of system.parts_columns shows what each part has:
The same EXPLAIN now shows estimates, and the smaller side is used to build the hash table:

Things to know

  • Use a release with the fix for stuck statistics mutations. In earlier releases, MATERIALIZE STATISTICS and DROP STATISTICS started with allow_statistics = 1 in the query failed forever when the server profile had allow_statistics = 0, and blocked all later mutations of the table. This is fixed in 26.10 and in the patch releases 26.7.31, 26.8.27 and 26.9.19.
  • Backfill the join keys. If only some parts have statistics, estimates can be far off. In a test with statistics in one newly inserted part of 1,000 rows and none in the existing part of one million rows, the estimated join result was 1,001,000 rows; after the backfill of the join key it was 201,226.
  • You can backfill one partition at a time with a column list: ALTER TABLE orders MATERIALIZE STATISTICS customer_id IN PARTITION 202601. IN PARTITION cannot be combined with ALL.
  • Inserts build statistics for tables of any size. With compatibility below 26.4, materialize_statistics_on_insert_max_table_size is 0, which removes the 25 GiB limit of newer versions. To get the limit back, set it for the users that insert: ALTER USER etl_user MODIFY SETTING materialize_statistics_on_insert_max_table_size = 26843545600.
  • Other optimizer defaults stay at the old values too, for example enable_join_transitive_predicates and use_statistics_for_part_pruning. Enable the ones you want for a user with ALTER USER ... MODIFY SETTING; a setting that is set explicitly is not changed by compatibility. The alternative is a newer compatibility value for a user, a role or a session, as described on the Version compatibility page. That brings every other behavior change since the pinned version as well, so test your queries first.
  • To check that a query used statistics, look at ProfileEvents['LoadedStatistics'] in system.query_log. To compare plans, run the query with SETTINGS use_statistics = 0.