A robust CLI tool for incrementally optimizing all partitions of ClickHouse tables
966
A robust CLI tool for incrementally optimizing all partitions of ClickHouse tables. Optimize large tables efficiently by processing partitions sequentially with comprehensive progress tracking and merge monitoring.
ClickHouse tables can accumulate many small parts over time, degrading query performance. The OPTIMIZE TABLE command typically processes all partitions at once, which can:
If you generate a file with a list of partitions to optimize, you can run the OPTIMIZE TABLE command manually. However, this approach is error-prone, failing when the OPTIMIZE TABLE command times out, and can be time-consuming.
This tool solves these problems by:
pip install clickhouse-optimizer
uvx clickhouse-optimizer --help
docker pull gavinmroy/clickhouse-optimizer
# Optimize all partitions of a table
clickhouse-optimizer --host ch.example.com --user admin --password secret --database mydb mytable
# Run with verbose logging to see detailed progress
clickhouse-optimizer --verbose --host ch.example.com --user admin --password secret --database mydb mytable
Set connection parameters via environment variables:
export CLICKHOUSE_HOST=ch.example.com
export CLICKHOUSE_USER=admin
export CLICKHOUSE_PASSWORD=secret
export CLICKHOUSE_DATABASE=mydb
clickhouse-optimizer mytable
# Using CLI arguments
docker run --rm gavinmroy/clickhouse-optimizer \
--host ch.example.com --user admin --password secret \
--database mydb mytable
# Using environment variables
docker run --rm \
-e CLICKHOUSE_HOST=ch.example.com \
-e CLICKHOUSE_USER=admin \
-e CLICKHOUSE_PASSWORD=secret \
-e CLICKHOUSE_DATABASE=mydb \
gavinmroy/clickhouse-optimizer mytable
# Custom timeouts and polling intervals
clickhouse-optimizer \
--optimize-timeout 7200 \
--poll-interval 10 \
mytable
| Option | Environment Variable | Description | Default |
|---|---|---|---|
--host | CLICKHOUSE_HOST | ClickHouse server hostname | Required |
--port | CLICKHOUSE_PORT | ClickHouse server port | 9440 |
--database | CLICKHOUSE_DATABASE | Database name | Required |
--user | CLICKHOUSE_USER | Username for authentication | Required |
--password | CLICKHOUSE_PASSWORD | Password for authentication | Required |
--secure | CLICKHOUSE_SECURE | Use secure connection | False |
--verbose | CLICKHOUSE_VERBOSE | Enable verbose logging | False |
--optimize-timeout | CLICKHOUSE_OPTIMIZE_TIMEOUT | Max seconds to wait for merges | 43200 (12h) |
--poll-interval | CLICKHOUSE_POLL_INTERVAL | Seconds between status checks | 5 |
system.parts to find all active partitionssystem.mergesPerfect for tables with hundreds of partitions where full optimization would be too resource-intensive.
Ideal for scheduled optimization during low-traffic periods with predictable progress tracking.
Quickly improve query performance on tables with many small parts without system overload.
Monitor optimization progress with detailed logging and progress tracking.
git clone https://github.com/gmr/clickhouse-optimizer
cd clickhouse-optimizer
uv sync
# Install pre-commit hooks
uv run pre-commit install
# Run linting
uv run ruff check
# Format code
uv run ruff format
# Run all pre-commit hooks
uv run pre-commit run --all-files
# Run tests with coverage
uv run coverage run -m pytest
# Show coverage report
uv run coverage report
# Generate HTML coverage report
uv run coverage html
# Build package distributions
uv build
BSD 3-Clause License - see LICENSE file for details.
Contributions are welcome! Please:
This tool follows a clean, modular architecture:
The optimizer discovers partitions, processes them sequentially, and monitors merge completion through ClickHouse system tables.
Content type
Image
Digest
sha256:fbac2ee10…
Size
43.3 MB
Last updated
7 months ago
docker pull gavinmroy/clickhouse-optimizer