Essential Monitoring Queries - part 2 - SELECT Queries
A set of SQL queries against ClickHouse system tables (system.parts, system.query_log, system.processes, system.stack_trace) for monitoring and troubleshooting SELECT workloads: cluster size overview, most expensive queries, ProfileEvents diffs between two query versions, hourly duration trends, per-client query counts, and diagnosing TOO_MANY_SIMULTANEOUS_QUERIES. Setting send_logs_level = 'trace' in clickhouse-client exposes per-query execution logs; in the UK price paid example, adding a projection ordered by town and price cut marks read from 3401 to 266 and latency from 1.2s to 0.028s. Queries use clusterAllReplicas for ClickHouse Cloud and can be adapted for self-managed clusters.
Related posts
- System Tables and a Window into the Internals of ClickHouse
ClickHouse mediumAdept
ClickHouse system tables are read-only virtual tables in the system database that expose server state, settings, metrics, errors, parts and query history. Most live in memory, while log tables such as query_log, part_log and metric_log persist on MergeTree storage. A set of practical diagnostic queries shows how to use them: system.settings for non-default values, query_log with normalizedQueryHash and ProfileEvents for slow, memory-heavy and failed queries, system.errors for frequent error codes with traces, and part_log to confirm inserts are producing parts. These queries can serve as a starting toolkit for troubleshooting and understanding cluster usage.
- Common issues you can solve using advanced monitoring dashboards
ClickHouse mediumAdept
ClickHouse ships a built-in Advanced Dashboard at /dashboard that charts query, MergeTree, OS and S3/cache metrics. Its panels are SQL queries over system.metric_log and system.asynchronous_metric_log. Setup means enabling those logs on self-hosted servers and creating a dashboard user with SELECT on the system tables plus REMOTE and CREATE TEMPORARY TABLE grants. Custom charts can be persisted in a separate dashboards table and merged with the defaults. Example diagnoses: unbatched inserts show as Max Parts for Partition spiking while Inserted Rows/sec stays low, and a Selected Rows/sec spike traced through system.query_log reveals a poor primary key (151M vs 6M rows read for the same query).
- Essential Monitoring Queries - part 1 - INSERT Queries
ClickHouse mediumAdept
ClickHouse system tables such as part_log, query_log, asynchronous_insert_log, asynchronous_metric_log and parts can be queried with SQL to monitor and troubleshoot INSERT workloads. Ready-to-use queries cover new part creation rate, bulk insert counts versus rows written, insert duration percentiles, per-insert memory and CPU, MaxPartCountForPartition, parts per partition, and part-level errors. Comparing insert request counts to rows written, or watching part counts climb, gives early warning of small-insert patterns and high-cardinality partition keys that lead to 'Too many parts' errors. Duration and resource metrics are only accurate for synchronous inserts, not async_insert or the Buffer engine, and alerting on these queries through tools like Grafana is recommended.
- Monitor SLAs and scale ClickHouse Cloud with clickhousectl and agents
ClickHouse mediumAdept
Tagging frontend queries with `log_comment` lets you compute a specific latency SLA, such as p99 under 200 ms, directly from `system.query_log` across replicas. You can then diagnose a breach by comparing query volume against latency and checking Prometheus CPU, memory and concurrency metrics. High concurrency points to adding replicas, while memory pressure points to bigger replicas. `clickhousectl` handles the scaling, either from a simple cron that scales out on breach or by handing the breach to a Claude agent prompted to investigate and apply exactly one scaling action. Every action is recorded in the activity log for auditing.
- A simple guide to ClickHouse query optimization: part 1
ClickHouse mediumAdept
Slow ClickHouse queries can be diagnosed with system.query_log (duration, rows read, memory, CPU, cache metrics) and the EXPLAIN statement, illustrated on the NYC Taxi dataset in the ClickHouse Playground. A table created with an inferred schema (all Nullable columns, empty ORDER BY) forces every example query into a full scan of 329 million rows taking over a second and roughly 400–550 MiB of memory. Turning off the filesystem cache makes timings reproducible. The first part of the series then moves to basic optimizations and choosing primary keys so ClickHouse can skip data instead of scanning it.