ClickHouse on One Box: An OLAP Database for Logs You Actually Query
The columnar database that turns a night of grep into a two-second aggregate query

Contents
I hoard logs the way some people hoard receipts: nginx access logs, application JSON logs, firewall drops, all of it shipped somewhere and then, in practice, never queried again unless something is actively on fire. The problem was never storage, disk is cheap, it was that the tools capable of asking “how many 5xx responses did this endpoint return per hour over the last month, broken down by upstream” were either Elasticsearch, which wants a cluster and a memory budget I don’t have on one box, or grep and awk, which technically can answer that question and will make you regret asking it. ClickHouse is the database that finally closed that gap for me: a proper columnar OLAP engine that runs happily as a single process on ordinary hardware and turns a query like that into something that returns before you’ve finished reading it back.
Why a columnar database changes what’s affordable
A normal row-oriented database like Postgres stores a table as complete rows on disk — great for “fetch this one record by ID,” bad for “scan five hundred million rows and average one column.” To answer the average, it still has to read every column of every row even though it only needed one. ClickHouse stores each column separately and contiguously, so an aggregate query over one column touches only that column’s data on disk, and because log data compresses extremely well column-by-column (a status_code column is mostly the number 200 repeated billions of times), the amount actually read off disk for a typical aggregate is a small fraction of the table’s logical size.
This is the specific property that makes single-box ClickHouse viable where single-box Elasticsearch struggles: Elasticsearch builds an inverted index optimised for full-text search and free-form filtering, which is powerful but memory-hungry at scale, whereas ClickHouse is optimised for exactly the query shape logs and metrics actually need — group, filter on a handful of columns, aggregate over time — and pays for it with a much smaller footprint per row.

Getting it running
ClickHouse ships a single static binary with no JVM and no external dependencies, which is most of why it’s pleasant to self-host.
| |
Or, if you’d rather keep it in your existing container stack:
| |
The nofile ulimit bump isn’t decorative — ClickHouse’s storage engine (MergeTree, covered below) opens a lot of file descriptors as it manages background merges, and the container default ulimit will cause mysterious startup warnings under sustained ingest without it.
The table engine that makes this work: MergeTree
Almost everything useful about ClickHouse for logs comes down to one table engine family, MergeTree, and specifically how you define its ordering key.
| |
PARTITION BY toYYYYMM(ts) splits the table into monthly chunks on disk, so a query scoped to a date range can skip entire partitions outright rather than scanning them and filtering. ORDER BY (service, level, ts) is the sort order data is physically stored in — pick this to match your most common filter pattern, because ClickHouse can skip large chunks of a column’s data when the query filters on a prefix of the sort key, the same principle as a leftmost-prefix index in a row store but applied at a much coarser, cheaper granularity called a sparse index. LowCardinality(String) tells ClickHouse to dictionary-encode a column with a small number of distinct values (log level, hostname, service name), which shrinks both storage and query time substantially for exactly the columns you’ll be filtering and grouping on constantly. And TTL ts + INTERVAL 90 DAY does automatic retention — old partitions get dropped in the background with no cron job or manual DELETE required.
Querying it
This is the part that sells the whole exercise. A query that would mean writing a script against raw log files, or waiting on an Elasticsearch aggregation against an index that’s outgrown its heap, is just SQL:
| |
On a table with hundreds of millions of rows and a reasonable ordering key, that returns in low single-digit seconds on modest hardware — an old rack-mount box with a handful of cores and spinning disk, not anything exotic. The countIf conditional-aggregate function is worth knowing on its own; it avoids a second pass or a subquery for the extremely common “count matching a condition, alongside a total” pattern that comes up in almost every dashboard query.
Getting logs in
ClickHouse’s native protocol is fast, but for shipping logs from existing infrastructure the HTTP interface with a batching client is usually simpler to wire up. If you’re already running a log shipper like Fluent Bit or Vector, both have first-class ClickHouse output plugins:
| |
Batch inserts rather than sending them row-by-row. ClickHouse is built around ingesting data in reasonably large batches (thousands of rows per insert rather than one) — inserting one row at a time creates one small on-disk part per insert, and MergeTree’s background merge process then has to spend cycles constantly consolidating a flood of tiny parts instead of doing useful work. Any serious log shipper batches by default; if you’re writing your own inserter, batch it explicitly.
Materialized views: pre-aggregating so dashboards don’t recompute everything
A dashboard that refreshes a “requests per minute per service” panel every ten seconds shouldn’t be re-scanning the raw logs table on every refresh, even if a single scan only takes a couple of seconds — that adds up fast with more than one viewer. ClickHouse’s materialized views solve this by running the aggregation incrementally as data arrives, rather than on read:
| |
Every batch inserted into logs triggers the view’s query against just that batch, and the SummingMergeTree engine on the target table automatically sums matching rows together in the background. The dashboard then queries logs_per_minute, a table that’s orders of magnitude smaller than the raw log stream, and stays fast even as the underlying logs table grows into the billions of rows. This is the single biggest lever for keeping a self-hosted ClickHouse box responsive under real dashboard load rather than just under ad-hoc queries, and it’s worth setting up for any panel that gets refreshed automatically rather than queried on demand.
Where this sits next to DuckDB
If you’ve read about DuckDB as an embeddable analytical database, ClickHouse solves an adjacent but different problem. DuckDB is brilliant for a one-off analytical query against a file or a dataset that fits comfortably in memory on the machine running the query — no server, no daemon, just a library and a Parquet file. ClickHouse is a standing server designed for continuous ingest and repeated querying against a dataset that keeps growing, with retention policies, background merging, and materialized views doing ongoing work whether or not anyone’s actively querying at that moment. If your logs arrive once as a batch and you want to explore them, reach for DuckDB. If your logs arrive continuously forever and multiple dashboards and people need to query them concurrently, that’s the job ClickHouse as a running service is actually built for.
Backups and replication on a single box
Running ClickHouse as one box, not a cluster, means you own the backup story yourself rather than relying on cluster replication for durability. The built-in BACKUP/RESTORE commands write consistent snapshots to a configured disk or S3-compatible target:
| |
Schedule this the same way you’d schedule any other backup job on the box — cron plus a retention policy on the backup directory — and test the restore path at least once before you need it for real, the same rule that applies to every backup system regardless of the database underneath it. A ClickHouse box with months of irreplaceable ingested history and no tested restore procedure is a snapshot away from becoming a very expensive lesson.
Troubleshooting
“Too many parts” errors under load. This is almost always row-at-a-time inserts. Check your ingest path is batching — the fix is nearly always there rather than in ClickHouse configuration. As a stopgap, SYSTEM STOP MERGES then SYSTEM START MERGES can unstick a server that’s fallen behind on merging, but fix the insert pattern rather than relying on that.
Queries slower than expected on a filter that “should” be fast. Check whether the filtered column is a prefix of your ORDER BY. Filtering on level when your sort key starts with service gets far less benefit from the sparse index than filtering on service does — ClickHouse still has to scan more granules than you’d expect. Reordering the sort key means rewriting the table (MergeTree doesn’t support changing it in place), so it’s worth getting this roughly right before you’ve loaded three months of data.
Disk filling up faster than the TTL should allow. SYSTEM MERGES runs the retention drop as a background operation rather than instantly on the exact TTL boundary — under heavy merge load it can lag by hours. Check system.parts for parts past their TTL that haven’t been dropped yet rather than assuming the TTL clause isn’t working.
Memory spikes on a big GROUP BY. ClickHouse defaults to fairly generous per-query memory limits designed for a dedicated analytics cluster rather than a shared single box also running other services. Set max_memory_usage per-query or per-user in users.xml to something appropriate for your hardware before a runaway aggregation query takes the box down with the rest of your stack on it.
Sizing a single box realistically
ClickHouse’s reputation for needing serious hardware comes from case studies at companies ingesting billions of rows a day across a cluster, which isn’t the workload most self-hosted setups have. For a homelab or small production ingest rate — tens of millions of log lines a day, not billions — a handful of CPU cores, a modest amount of RAM (background merges and query execution are the main consumers, and both scale with concurrent load rather than table size alone), and reasonably fast disk for the merge I/O is enough to keep query latency in the seconds range even against a table that’s grown past a billion rows. The place sizing actually bites is concurrent heavy queries rather than data volume — a single unbounded GROUP BY across the full table competing with three others at once will feel the RAM ceiling well before the disk does, which is exactly what the max_memory_usage setting mentioned above is there to contain.
Is it worth running
If your logs currently live in files you grep when something breaks and nowhere else, ClickHouse is a genuine upgrade in kind rather than merely in degree — the same questions that take a slow multi-minute scan through compressed archives become a SQL query that returns in seconds, and the retention/compression story means you can actually afford to keep months of history instead of rotating it away out of necessity. It’s not a replacement for Loki if what you want is fast full-text grep across recent logs with minimal setup — that’s a genuinely different and simpler tool for a narrower job. Where ClickHouse earns its keep is the moment you want structured, columnar, SQL-queryable analytics over logs or metrics at a scale a single box can carry, which for most homelabs and small production systems is a much higher ceiling than it sounds.




