Nojus Morkūnas

9 min read

Benchmarking five databases with 1.4 billion energy readings

During my internship at CAPE, our team of four worked on a question that looked simple enough: should one of our clients move its energy data out of PostgreSQL, and if so, where should it go?

We recommended ClickHouse. It handled the joins the client's dashboards depended on, and we estimated it would cost about $10,400 less per year to run. The trade-off was taking over backups, upgrades and monitoring from Azure.

Jump to the results or the recommendation.

The answer turned into a benchmark involving five databases, 17 queries and about 1.4 billion rows. My teammates researched the literature, and I proposed the benchmark, built the tool and ran it. It became the most useful thing I built during the internship.

The database was outgrowing its server

CAPE works with an energy company that receives smart-meter readings every 15 minutes. Each reading records either consumption (E17) or injection back into the grid (E18). Across thousands of meters, that adds about 1.9 million rows a day. Two years of history comes to roughly 1.4 billion rows.

The data lived in PostgreSQL on Azure. By the time our project started, the database occupied 174 GB and ordinary dashboard queries were running it out of memory. CAPE had recently doubled the server from 16 GB to 32 GB of RAM, which added about $100 a month to each of its three environments. More data arrived every day, so another hardware upgrade would only buy time.

We needed to decide whether a time-series database would handle this workload better. Our candidates were PostgreSQL, TimescaleDB, InfluxDB 2, QuestDB and ClickHouse.

A generic benchmark would answer the wrong question

Our initial plan was to review the literature, compare the candidates on a standard set of criteria and recommend one. That would have produced a respectable report, but I was not convinced it would produce a useful decision.

Benchmarks such as TPC-H and TSBS measure generic workloads. CAPE's workload was unusually specific. Its dashboard had to serve quick lookups for one meter, scan the full dataset for aggregate reports and group readings through a supplier hierarchy. Those hierarchy queries joined the measurements to metadata with time-validity windows, because a meter's place in the hierarchy could change over time. The client also ran ad-hoc queries, so precomputing results inside PostgreSQL would only have covered part of the workload.

Ingest speed mattered less. The client received readings in daily batches and had enough time to load them before they became visible in the dashboard. Query behaviour, especially the hierarchy joins, was the real constraint.

I wanted the recommendation to rest on that workload rather than on whichever database won a benchmark designed for something else, so I built our own benchmarking tool.

The benchmark

The main interface was a small FastAPI application running at localhost:8000. Its frontend was a single embedded HTML page with no build step or static-file server. I put it behind a Cloudflare tunnel so the team could use it from home or the office without opening ports on the test machine. From the UI, we could run individual queries, inspect charts, start the full benchmark and monitor data loading.

A separate CLI runner called the same API. It ran all 17 queries five times against each database, saved the results as JSON and generated comparisons for the first, best and median runs. Setup scripts handled the less visible work: converting the source data, generating a two-year dataset, building the hierarchy table and loading each database.

The client gave us a 4 GB JSON export as a starting point. My first attempt to inspect it ended with my editor crashing after I pressed Ctrl+A. That settled the question of whether I could work with it by hand.

I converted the export into a 28 MB Parquet seed containing 15 days of readings. Another script replayed that seed 48 times with shifted timestamps and a random variance of plus or minus 15 percent per block. The result was 1.3 GB of Parquet files representing about 1.4 billion rows and almost two years of history. Each database received the same generated dataset, and I verified every load with a row count.

Why InfluxDB 3 did not make the final benchmark

We also tried to include InfluxDB 3. It is a rewrite of InfluxDB in Rust, stores data with Apache Arrow and Parquet and adds SQL support. On paper, it looked like a promising successor to InfluxDB 2. In my test environment, I could not get it into a state that I trusted enough to benchmark.

The first problem was a limit in the free Core edition on the number of Parquet files a query could scan. Loading the full dataset produced 432 files, so every query failed. I moved to the 30-day Enterprise trial, which removed that limit.

The next problem arrived 266 million rows into the ingest: the process ran out of memory and the container died. This happened several times. I changed the memory limits, WAL flush intervals, compaction settings and a Parquet cache flag before the load finally completed. Those changes reduced the ingest rate from about 95,000 rows per second to 20,000, turning every retry into an overnight job.

After the load, I ran one verification pass per query. Some queries failed, but enough worked that a full benchmark still seemed possible. The following day I used docker compose up -d --force-recreate to apply a configuration change. The command created a new container instance with the existing node-id.

That instance wrote a blank compaction summary at the active pointer. The previous 1,882 compaction cycles were no longer visible, which also made 3,337 compacted Parquet files invisible to the query engine. There was no warning or CLI repair command that I could find. Queries only revealed the damage later, when even simple selects began exhausting memory.

I cannot say with certainty whether this was a product bug, a configuration mistake or an interaction between the two. My reading of the failure is that reusing the node-id allowed a new instance to replace the compactor index without checking the existing state. Recovering meant loading the full dataset again, and the project deadline did not leave room for another attempt.

I excluded InfluxDB 3 rather than publish results from an environment I could not reproduce. In the smaller tests that did run, its timings were close to those of InfluxDB 2, so its absence did not leave us without an InfluxDB comparison.

What the measurements showed

I ran each of the 17 queries five times per database. The tables below show the best run and the cold run. The best run indicates peak performance, and the cold run is the first of the five. Each database ran alone in an isolated Docker container with 20 GB of RAM on the same physical machine. PostgreSQL was configured for that limit: 6 GB of shared buffers, 256 MB of work memory and two parallel workers per query.

PostgreSQL remained excellent at indexed point lookups. It struggled when a query had to scan most of the 174 GB dataset, since only a small part of it could fit in memory.

TimescaleDB compressed the same data from 174 GB to 8 GB. That 22x reduction helped with full scans, but decompression made point lookups and queries that opened many chunks at once slower than plain PostgreSQL.

ClickHouse was the most consistent analytical database in the test. It was particularly strong on hierarchy joins and stayed competitive on range and full-dataset queries. Its weak spot was a lookup for one EAN: without a per-EAN index, it scanned more data and took just under a second.

QuestDB returned the fastest SQL point lookup at 2 ms and completed Q5 in 887 ms at its best. Its all-time hierarchy joins were much slower, taking between 96 and 101 seconds.

InfluxDB 2 won most queries in the first three tiers. Its problem was the workload that mattered most to the client. Flux materialised both sides of a join in memory, and the hierarchy queries took close to 3,000 seconds each.

Best run

Tier 1: Point queries (single meter, time-windowed)

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#1 Single meter: 1-day hourly6ms355ms936msSlowest2msFastest5ms
#2 Single meter: 1-month daily8ms351ms962msSlowest8ms6msFastest
#3 Single meter: 1-year monthly27ms374ms949msSlowest67ms9msFastest

Tier 2: Range aggregations (all meters, time-bucketed)

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#4 Monthly E17 vs E18 balance102.73sSlowest57.04s6.97s12.41s2.96sFastest
#5 Hourly aggregation (first 3 months)33.71s45.06sSlowest2.66s887msFastest27.57s
#6 Peak consumption hours77.02sSlowest52.42s3.30s10.01s2.48sFastest
#7 Daily aggregation by direction100.05sSlowest55.78s7.38sFastest15.02s21.72s

Tier 3: Full-scan aggregations (no time filter)

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#8 Top 20 meters by total consumption61.80sSlowest4.33s3.47s4.53s930msFastest
#9 Net energy balance per meter74.08sSlowest65.62s3.85s13.31s2.33sFastest
#10 Prosumer detection (E18/E17 ratio)74.31sSlowest65.81s3.84s14.23s2.28sFastest
#11 Active meters per day379.95s552.85sSlowest6.62sFastest11.94s15.32s

Tier 4: Hierarchy join queries (with time window)

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#12 Supplier total daily158.26s160.26s13.33sFastest49.31s3296.26s*Slowest
#13 By category (PRF/SMA) daily167.55s174.53s13.44sFastest46.79s3296.90s*Slowest
#14 Sub-category monthly (PRF)130.27s127.46s11.24sFastest35.44s2928.04s*Slowest

Tier 5: Hierarchy join queries (all time, no window)

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#15 Supplier total (all time)119.38s109.27s15.40sFastest97.93s3243.34s*Slowest
#16 By category all time (PRF/SMA)132.16s128.63s15.37sFastest101.27s3258.31s*Slowest
#17 Sub-category all time (PRF only)106.55s97.26s15.08sFastest95.83s3240.96s*Slowest

Cold run

Tier 1: Point queries

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#1 Single meter: 1-day hourly23ms409ms936msSlowest9msFastest11ms
#2 Single meter: 1-month daily16msFastest366ms968msSlowest37ms19ms
#3 Single meter: 1-year monthly90msFastest386ms949ms4.22sSlowest91ms

Tier 2: Range aggregations

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#4 Monthly E17 vs E18 balance120.13sSlowest60.94s6.97s12.41s2.99sFastest
#5 Hourly aggregation (first 3 months)57.55sSlowest45.30s2.71s2.20sFastest27.81s
#6 Peak consumption hours78.07sSlowest52.59s3.33s11.42s2.65sFastest
#7 Daily aggregation by direction100.05sSlowest56.64s7.38sFastest15.93s22.16s

Tier 3: Full-scan aggregations

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#8 Top 20 meters by total consumption104.60sSlowest4.35s3.51s9.55s947msFastest
#9 Net energy balance per meter75.43sSlowest65.62s3.87s13.31s2.44sFastest
#10 Prosumer detection (E18/E17 ratio)74.44sSlowest66.27s3.87s14.79s2.38sFastest
#11 Active meters per day389.15s552.85sSlowest6.66sFastest11.94s15.47s

Tier 4: Hierarchy join queries (with time window)

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#12 Supplier total daily168.36s160.59s13.47sFastest50.36s3296.26s*Slowest
#13 By category (PRF/SMA) daily167.55s176.31s13.52sFastest46.79s3296.90s*Slowest
#14 Sub-category monthly (PRF)130.93s127.80s11.24sFastest35.70s2928.04s*Slowest

Tier 5: Hierarchy join queries (all time, no window)

QueryPostgreSQLTimescaleDBClickHouseQuestDBInfluxDB 2
#15 Supplier total (all time)≥ 119.38s≥ 109.27s≥ 15.40s≥ 97.93s3243.34s*
#16 By category all time (PRF/SMA)≥ 132.16s≥ 128.63s≥ 15.37s≥ 101.27s3258.31s*
#17 Sub-category all time (PRF only)≥ 106.55s≥ 97.26s≥ 15.08s≥ 95.83s3240.96s*

* Tier 4 and 5 InfluxDB 2 queries were run once because of their extreme execution time. The same result appears in both tables.

Cold-run times for #15 to #17 aren't available for PostgreSQL, TimescaleDB, ClickHouse and QuestDB. A cold run is one of the five runs, so it can't be faster than the best run, and the best-run time is shown as a lower bound (≥).

Storage

DatabaseDisk usageCompared with PostgreSQL
PostgreSQL174 GB100%
TimescaleDB8 GB4.6%
ClickHouse13 GB7.5%
QuestDB32 GB18.4%
InfluxDB 25.5 GB3.2%

The fastest database was not the best choice

InfluxDB 2 had the best raw performance on most point, range and full-scan queries. We still did not recommend it as the main migration target.

The client's dashboard depends on aggregations by supplier, category and sub-category. To answer them correctly, the database must join readings to a validity-window table that records where each meter belonged at a given time. InfluxDB 2 took nearly an hour on those queries. Winning the simpler tiers did not compensate for failing the workload the dashboard used most.

Our final recommendation was ClickHouse. We scored each database on migration effort, resource efficiency, query language, performance, scalability and manageability, with performance weighted at 40%. ClickHouse scored 4.50 out of 5, ahead of QuestDB at 3.60 and TimescaleDB and InfluxDB 2 at 3.45. Its lowest mark was migration: its SQL dialect and indexing differ from PostgreSQL's, and we estimated the move at 12 to 14 hours of work.

TimescaleDB was the easiest migration, at 6 to 8 hours, because it keeps the PostgreSQL driver and SQL. On Azure its advantage is smaller than it looks. Azure's managed PostgreSQL only offers TimescaleDB's Apache-2 edition, which has neither the compression behind the 8 GB result nor continuous aggregates. Getting those would mean running TimescaleDB on a VM anyway.

ClickHouse would also be cheaper to run. The client pays for three managed PostgreSQL environments at $511 a month each, about $18,400 a year. Open-source ClickHouse on Azure VMs of the same size would cost about $222 a month each, or $8,000 a year, which cuts infrastructure costs by about $10,400 a year. The team would take on the backups, upgrades and monitoring that Azure handles today. ClickHouse Cloud, which can be billed through the Azure Marketplace, avoids that work but would cost more than all three PostgreSQL environments do now.

What the benchmark didn't test

The results were good enough for the decision we had to make, but they have limits worth knowing before anyone reuses them.

The data was replicated. The 1.4 billion rows came from 15 days of real readings, replayed 48 times with shifted timestamps and a variance of plus or minus 15 percent. Real data varies more than that, so the repeated patterns probably made compression look better than it would be in production. The storage figures, especially TimescaleDB's 22x, are likely optimistic.

Some InfluxDB 2 queries ran once. The tier 4 and 5 hierarchy queries took close to 3,000 seconds each, so I ran them once instead of five times. They were so far behind the other databases that more runs wouldn't have changed the ranking.

What changed for me

Once we translated the client's dashboard into 17 concrete queries, "Which database is fastest?" stopped being a useful question. The better one was "Which database is fast at the work this team cannot avoid?"

The project also felt different from a university assignment. Someone could act on our recommendation, which made every unexplained result and failed load harder to wave away.

The benchmark runner, setup scripts and queries are available in the tsdb-benchmark repository.