Identifying expensive queries by memory usage in ClickHouse
Using the system.query_log table
The following useful query shows which of your executed queries used the most memory.
A couple of comments about this query:
- the results are computed from the past day (
now() - toIntervalDay(1))) but you can easily modify the time interval - it assumes you have a cluster named
default, which is the name of your cluster in ClickHouse Cloud. Changedefaultto the name of your cluster - if you do not have a cluster, see the query listed at the end of this article
The response looks like:
If you do not have a system.query_log table, then you likely do not have query logging enabled. View the details of the query_log setting for details on how to enable it.
query_log contains more than the queries you submittedA single submitted query can appear as several rows: the initial query (is_initial_query = 1) plus derivative or internal steps (is_initial_query = 0), such as secondary queries for distributed execution or the internal queries used to evaluate views. Those internal rows have their own query_id values — assigned by ClickHouse, and sometimes including a label such as queryView... — so don't assume every query_id matches what your client reported. To attribute usage to the query as you ran it, filter on is_initial_query = 1 or match query_id = initial_query_id. See How to identify the most expensive queries and the query_log reference for details.
If you do not have a cluster, use can just query your one system.query_log table directly: