How to Monitor Slow Queries in ClickHouse

Galaxy Glossary

How do I find and optimize slow queries in ClickHouse?

Use system tables and settings to detect, analyze, and optimize queries that exceed a desired execution time threshold.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Why should I track slow queries in ClickHouse?

Slow queries indicate inefficient SQL, insufficient indexing, or resource bottlenecks. Tracking them lets you tune schema, rewrite queries, and resize hardware before performance hurts users.

Which ClickHouse tables log slow queries?

The system.query_log and system.processes tables record every statement. Filter by query_duration_ms, type='QueryFinish', and time windows to isolate slowness.

How do I enable detailed query logging?

Set log_queries = 1 and adjust log_query_threads = 1, query_log_min_query_duration_ms, and query_log_min_type in config.xml or per session.Lower thresholds capture more data.

How can I list queries slower than 1 s today?

Run a SELECT against system.query_log filtering by event_time > today() and query_duration_ms > 1000. Sort by duration to surface worst offenders quickly.

How do I pinpoint slow joins on ecommerce tables?

Filter the log by query LIKE '%Orders% JOIN %Customers%'.Combine with arraySum(memory_usage) to find memory-hungry statements joining Orders and Customers.

What are best practices after finding slow queries?

Rewrite SQL to minimize full scans, pre-aggregate where possible, use appropriate table engines, and add MATERIALIZED VIEWs for frequently requested slices. Test with EXPLAIN and benchmark.

How can I alert on slow queries automatically?

Create a scheduled query exporting slow-query rows to a dashboard or send alerts via system.tasks and external scripts. Monitor trends and investigate spikes promptly.

.

Why How to Monitor Slow Queries in ClickHouse is important

How to Monitor Slow Queries in ClickHouse Example Usage


-- Detect "Orders" queries running over 200 ms this week
SELECT
    query_start_time,
    query_duration_ms,
    user,
    query
FROM system.query_log
WHERE type = 'QueryFinish'
  AND query_duration_ms > 200
  AND event_time >= toStartOfWeek(now())
  AND query ILIKE '%SELECT%FROM Orders%'
ORDER BY query_duration_ms DESC;

How to Monitor Slow Queries in ClickHouse Syntax


SELECT
    query_start_time,
    query_duration_ms,
    user,
    query
FROM system.query_log
WHERE type = 'QueryFinish'
  AND query_duration_ms > <duration_ms>
  AND event_date >= today()
ORDER BY query_duration_ms DESC;

-- Example for ecommerce: find "Orders" queries >200 ms
SELECT query_start_time, query_duration_ms, query
FROM system.query_log
WHERE type = 'QueryFinish'
  AND query_duration_ms > 200
  AND query ILIKE '%Orders%'
ORDER BY query_duration_ms DESC;

Common Mistakes

Frequently Asked Questions (FAQs)

Does enabling query logging slow ClickHouse?

Overhead is minimal when you log only finished queries over a reasonable threshold. Logging every statement can increase disk usage and CPU.

Can I view running slow queries in real time?

Yes. Query system.processes and filter by elapsed > desired_ms to see statements still executing.

How do I permanently change logging thresholds?

Edit config.xml (or a custom .d/ file), set the desired query_log_min_query_duration_ms, and restart the server.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.