OPTIMIZE Statement

This query tries to initialize an unscheduled merge of data parts for tables.

Warning

OPTIMIZE can’t fix the Too many parts error.

Syntax

  1. OPTIMIZE TABLE [db.]name [ON CLUSTER cluster] [PARTITION partition | PARTITION ID 'partition_id'] [FINAL] [DEDUPLICATE [BY expression]]

The OPTMIZE query is supported for MergeTree family, the MaterializedView and the Buffer engines. Other table engines aren’t supported.

When OPTIMIZE is used with the ReplicatedMergeTree family of table engines, ClickHouse creates a task for merging and waits for execution on all replicas (if the replication_alter_partitions_sync setting is set to 2) or on current replica (if the replication_alter_partitions_sync setting is set to 1).

  • If OPTIMIZE does not perform a merge for any reason, it does not notify the client. To enable notifications, use the optimize_throw_if_noop setting.
  • If you specify a PARTITION, only the specified partition is optimized. How to set partition expression.
  • If you specify FINAL, optimization is performed even when all the data is already in one part. Also merge is forced even if concurrent merges are performed.
  • If you specify DEDUPLICATE, then completely identical rows (unless by-clause is specified) will be deduplicated (all columns are compared), it makes sense only for the MergeTree engine.

You can specify how long (in seconds) to wait for inactive replicas to execute OPTIMIZE queries by the replication_wait_for_inactive_replica_timeout setting.

Note

If the replication_alter_partitions_sync is set to 2 and some replicas are not active for more than the time, specified by the replication_wait_for_inactive_replica_timeout setting, then an exception UNFINISHED is thrown.

BY expression

If you want to perform deduplication on custom set of columns rather than on all, you can specify list of columns explicitly or use any combination of *, COLUMNS or EXCEPT expressions. The explictly written or implicitly expanded list of columns must include all columns specified in row ordering expression (both primary and sorting keys) and partitioning expression (partitioning key).

Note

Notice that * behaves just like in SELECT: MATERIALIZED and ALIAS columns are not used for expansion.
Also, it is an error to specify empty list of columns, or write an expression that results in an empty list of columns, or deduplicate by an ALIAS column.

Syntax

  1. OPTIMIZE TABLE table DEDUPLICATE; -- all columns
  2. OPTIMIZE TABLE table DEDUPLICATE BY *; -- excludes MATERIALIZED and ALIAS columns
  3. OPTIMIZE TABLE table DEDUPLICATE BY colX,colY,colZ;
  4. OPTIMIZE TABLE table DEDUPLICATE BY * EXCEPT colX;
  5. OPTIMIZE TABLE table DEDUPLICATE BY * EXCEPT (colX, colY);
  6. OPTIMIZE TABLE table DEDUPLICATE BY COLUMNS('column-matched-by-regex');
  7. OPTIMIZE TABLE table DEDUPLICATE BY COLUMNS('column-matched-by-regex') EXCEPT colX;
  8. OPTIMIZE TABLE table DEDUPLICATE BY COLUMNS('column-matched-by-regex') EXCEPT (colX, colY);

Examples

Consider the table:

  1. CREATE TABLE example (
  2. primary_key Int32,
  3. secondary_key Int32,
  4. value UInt32,
  5. partition_key UInt32,
  6. materialized_value UInt32 MATERIALIZED 12345,
  7. aliased_value UInt32 ALIAS 2,
  8. PRIMARY KEY primary_key
  9. ) ENGINE=MergeTree
  10. PARTITION BY partition_key
  11. ORDER BY (primary_key, secondary_key);
  1. INSERT INTO example (primary_key, secondary_key, value, partition_key)
  2. VALUES (0, 0, 0, 0), (0, 0, 0, 0), (1, 1, 2, 2), (1, 1, 2, 3), (1, 1, 3, 3);
  1. SELECT * FROM example;

Result:

  1. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  2. 0 0 0 0
  3. 0 0 0 0
  4. └─────────────┴───────────────┴───────┴───────────────┘
  5. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  6. 1 1 2 2
  7. └─────────────┴───────────────┴───────┴───────────────┘
  8. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  9. 1 1 2 3
  10. 1 1 3 3
  11. └─────────────┴───────────────┴───────┴───────────────┘

When columns for deduplication are not specified, all of them are taken into account. Row is removed only if all values in all columns are equal to corresponding values in previous row:

  1. OPTIMIZE TABLE example FINAL DEDUPLICATE;
  1. SELECT * FROM example;

Result:

  1. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  2. 1 1 2 2
  3. └─────────────┴───────────────┴───────┴───────────────┘
  4. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  5. 0 0 0 0
  6. └─────────────┴───────────────┴───────┴───────────────┘
  7. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  8. 1 1 2 3
  9. 1 1 3 3
  10. └─────────────┴───────────────┴───────┴───────────────┘

When columns are specified implicitly, the table is deduplicated by all columns that are not ALIAS or MATERIALIZED. Considering the table above, these are primary_key, secondary_key, value, and partition_key columns:

  1. OPTIMIZE TABLE example FINAL DEDUPLICATE BY *;
  1. SELECT * FROM example;

Result:

  1. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  2. 1 1 2 2
  3. └─────────────┴───────────────┴───────┴───────────────┘
  4. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  5. 0 0 0 0
  6. └─────────────┴───────────────┴───────┴───────────────┘
  7. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  8. 1 1 2 3
  9. 1 1 3 3
  10. └─────────────┴───────────────┴───────┴───────────────┘

Deduplicate by all columns that are not ALIAS or MATERIALIZED and explicitly not value: primary_key, secondary_key, and partition_key columns.

  1. OPTIMIZE TABLE example FINAL DEDUPLICATE BY * EXCEPT value;
  1. SELECT * FROM example;

Result:

  1. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  2. 1 1 2 2
  3. └─────────────┴───────────────┴───────┴───────────────┘
  4. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  5. 0 0 0 0
  6. └─────────────┴───────────────┴───────┴───────────────┘
  7. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  8. 1 1 2 3
  9. └─────────────┴───────────────┴───────┴───────────────┘

Deduplicate explicitly by primary_key, secondary_key, and partition_key columns:

  1. OPTIMIZE TABLE example FINAL DEDUPLICATE BY primary_key, secondary_key, partition_key;
  1. SELECT * FROM example;

Result:

  1. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  2. 1 1 2 2
  3. └─────────────┴───────────────┴───────┴───────────────┘
  4. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  5. 0 0 0 0
  6. └─────────────┴───────────────┴───────┴───────────────┘
  7. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  8. 1 1 2 3
  9. └─────────────┴───────────────┴───────┴───────────────┘

Deduplicate by any column matching a regex: primary_key, secondary_key, and partition_key columns:

  1. OPTIMIZE TABLE example FINAL DEDUPLICATE BY COLUMNS('.*_key');
  1. SELECT * FROM example;

Result:

  1. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  2. 0 0 0 0
  3. └─────────────┴───────────────┴───────┴───────────────┘
  4. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  5. 1 1 2 2
  6. └─────────────┴───────────────┴───────┴───────────────┘
  7. ┌─primary_key─┬─secondary_key─┬─value─┬─partition_key─┐
  8. 1 1 2 3
  9. └─────────────┴───────────────┴───────┴───────────────┘