ClickHouse
clickhouse-client, databases and tables, inserting and exporting data, parts, mutations and slow queries.
Connecting
Connect to a server
clickhouse-client --host <host> --port 9000 --user <user> --password
Connect over TLS (ClickHouse Cloud)
clickhouse-client --host <host> --secure --user <user> --password
Run a query and exit
clickhouse-client --host <host> --query "SELECT version()"
Query over HTTP
curl 'http://<host>:8123/?query=SELECT%201'
Databases and tables
List databases
SHOW DATABASES;List tables
SHOW TABLES FROM <database>;
Table DDL
SHOW CREATE TABLE <database>.<table>;
MergeTree table
CREATE TABLE <database>.events ( ts DateTime, user_id UInt64, event LowCardinality(String) ) ENGINE = MergeTree PARTITION BY toYYYYMM(ts) ORDER BY (event, ts);
Table sizes on diskadvanced
SELECT database, table,
formatReadableSize(sum(bytes_on_disk)) AS size,
sum(rows) AS rows
FROM system.parts
WHERE active
GROUP BY database, table
ORDER BY sum(bytes_on_disk) DESC
LIMIT 20;Import and export
Insert a CSV file
clickhouse-client --host <host> --query "INSERT INTO <database>.<table> FORMAT CSVWithNames" < data.csv
Export a query to CSV
clickhouse-client --host <host> --query "SELECT * FROM <database>.<table> FORMAT CSVWithNames" > out.csv
Query a local file without a server
clickhouse-local --query "SELECT count() FROM file('data.csv', CSVWithNames)"Read Parquet straight from S3advanced
SELECT count() FROM s3('https://<bucket>.s3.amazonaws.com/path/*.parquet', 'Parquet');
Operations
Running queries
SELECT query_id, user, elapsed, query FROM system.processes ORDER BY elapsed DESC;Kill a query
KILL QUERY WHERE query_id = '<query_id>';
Slowest queries in the last houradvanced
SELECT query_duration_ms, read_rows, formatReadableSize(memory_usage) AS mem, query
FROM system.query_log
WHERE type = 'QueryFinish' AND event_time > now() - INTERVAL 1 HOUR
ORDER BY query_duration_ms DESC
LIMIT 10;Unfinished mutations (ALTER UPDATE/DELETE)advanced
SELECT database, table, mutation_id, command, latest_fail_reason FROM system.mutations WHERE NOT is_done;Force merge of partsadvanced
OPTIMIZE TABLE <database>.<table> FINAL;
Heavy on large tables — run off-peak.
Drop an old partitionadvanced
ALTER TABLE <database>.<table> DROP PARTITION '202401';