ClickHouse analyzes large volumes of events with SQL

ClickHouse stores data by column. A SaaS dashboard shows how sort keys, partitions, and inserts affect query performance and how to measure the difference.

A SaaS dashboard needs to show how many of a customer’s API requests ended in a server error over the last 24 hours. The database has to process many events, but it needs only a few fields: tenant, timestamp, and HTTP status. That is a common workload for ClickHouse, a column-oriented SQL database for analytics. It can read the columns the query needs. How much data it has to examine also depends on the table’s sort order. Columnar storage in ClickHouse

At ayedo, we offer ClickHouse as a managed app on Kubernetes in Germany. We start with typical queries, data volumes, and retention requirements, then use those to plan table design, storage, and replication. Managed ClickHouse at ayedo

Choose a sort key that matches your common filters

If the dashboard usually selects one tenant and a time window, (tenant_id, event_time) is a sensible starting point. Within each data part, events for a tenant sit together in time order:

CREATE TABLE api_events
(
    tenant_id   UInt32,
    event_time  DateTime('UTC'),
    status      UInt16,
    duration_ms UInt32
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(event_time)
ORDER BY (tenant_id, event_time);

With MergeTree, ClickHouse writes data to parts. Inside each part, the primary index records key values for groups of rows called granules. With a suitable filter, ClickHouse can skip entire granules and read the required columns for the remaining ranges. The sort key also defines the primary index unless you specify a separate PRIMARY KEY. Columnar storage and the primary index

A second dashboard that typically compares all tenants over short time windows has a different access pattern. For that dashboard, compare a table sorted by (event_time, tenant_id). Test both query patterns with a representative tenant distribution to choose a sort order for the shared dataset.

Measure the query with real data

For the tenant dashboard, use EXPLAIN indexes = 1 to inspect which parts and granules the plan selects:

EXPLAIN indexes = 1
SELECT
    toStartOfHour(event_time) AS hour,
    count() AS requests,
    countIf(status >= 500 AND status < 600) AS server_errors
FROM api_events
WHERE tenant_id = 42
  AND event_time >= toDateTime('2026-11-20 00:00:00', 'UTC')
  AND event_time <  toDateTime('2026-11-21 00:00:00', 'UTC')
GROUP BY hour
ORDER BY hour;

Then run the same query without EXPLAIN. With query logging enabled, system.query_log provides metrics such as read_rows, read_bytes, and query_duration_ms. Keep the sample, time window, and query parameters the same across runs. Compare elapsed time and data read to see whether the new sort order improves the target query. ClickHouse EXPLAIN reference · The query_log system table

Monthly partitions and batched inserts serve different purposes

The monthly partition in this example groups data by time. That makes it easier to remove old data a month at a time. For finding one tenant’s events within a month, the sort key does most of the work. Creating a partition per tenant and day can quickly produce many small parts. Background merges only combine parts within the same partition. ClickHouse guidance on partitioning and inserts

Writes also affect read performance. When an application synchronously inserts each API event on its own, new parts can accumulate faster than ClickHouse can merge them. Batch events in the client if the workload allows it. ClickHouse can also buffer compatible inserts on the server. With async_insert=1 and wait_for_async_insert=1, the client receives an acknowledgement after the buffer is flushed successfully. During load testing, watch active part counts and dashboard response times while data is coming in. ClickHouse guidance on batching and asynchronous inserts

Size Managed ClickHouse around a real query

The ayedo Polycrate block documents a starting setup with a single ClickHouse replica on Kubernetes, persistent storage, and credentials from an existing Kubernetes Secret. We use the workload to establish replication, storage, and recovery requirements for the operating model you choose. For a pilot, start with a representative sample of events, the most important dashboard queries, and a realistic ingestion workload. ClickHouse in the ayedo Polycrate Hub

Want to analyze events, usage data, or logs with ClickHouse? Talk to us about your queries, data volumes, and where to start with Managed ClickHouse.