ClickHouse Performance: Real-Time Monitoring of Expensive Queries


Expensive queries in ClickHouse can have a significant impact on overall system performance, consuming valuable resources like CPU, memory, and disk I/O. These resource-intensive queries can lead to increased latency and reduced throughput, hampering the efficiency of the ClickHouse system. To ensure optimal performance, it is crucial to monitor and optimize these expensive queries effectively.

Python script for Expensive Query Monitoring

Below is a comprehensive Python script that allows real-time monitoring of query performance in ClickHouse and provides insights into query latency, rows processed, bytes read, and memory usage:

import clickhouse_driver
import time

# Connect to ClickHouse
connection = clickhouse_driver.connect(host='hostname', port='port', user='username', password='password')

while True:
        # Execute query to get current query performance
        query = "SELECT query_id, query, elapsed, rows, bytes, memory_usage FROM system.query_log WHERE query != 'SELECT' AND is_initial_query = 1 ORDER BY elapsed DESC LIMIT 10"
        result = connection.execute(query)

        # Print results sorted by latency
        print("\nQuery ID \t Query \t\t\t Elapsed \t Rows \t\t Bytes \t Memory Usage")
        for row in result:
            print(row[0], "\t\t", row[1][:35], "...", "\t\t", row[2], "\t\t", row[3], "\t\t", row[4], "\t", row[5])
    except Exception as e:
        print(f"Error: {e}")


In this enhanced script, we use the clickhouse_driver library to establish a connection to the ClickHouse database. The query retrieves data from the system.query_log table, which records information about executed queries. The results are then sorted by elapsed time in descending order to identify the most time-consuming queries. The top 10 results are displayed, showing the query ID, truncated query text, elapsed time, rows processed, bytes read, and memory usage.

The script continues to run in an infinite loop, refreshing the query results every 5 seconds. This enables real-time monitoring of query performance, providing valuable insights into resource-consuming queries that might be impacting system performance.

By regularly monitoring expensive queries and analyzing their resource utilization, you can identify performance bottlenecks and optimize queries accordingly. Optimizations may include:

  • Adding appropriate indexes
  • Revising data models
  • Rewriting complex queries to reduce resource consumption and enhance overall system efficiency

Remember to tailor the script to match your ClickHouse configuration, including the hostname, port, username, and password for your ClickHouse database.

With this enhanced real-time monitoring script, you can proactively address expensive queries and fine-tune your ClickHouse system for optimal performance, ensuring a smooth and efficient data analytics experience.


Efficiently monitor and optimize resource-intensive queries in ClickHouse using a Python script for real-time performance insights, enabling proactive identification of bottlenecks and optimization opportunities, ultimately enhancing system efficiency and query throughput.

To read more about ClickHouse performance, do consider reading the below articles

About Shiv Iyer 222 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.