Repository navigation
Description
Summary
After migrating from the ClickHouse Java client v1 to v2 we observed a major performance regression: more than 2 times less throughput. We use ClickHouse as a pre-aggregation layer, and run analytical queries that may return 10^6-10^9 rows for later processing. Because our pipeline depends on low-latency, low-overhead reads, even small regressions translate into major throughput losses.
Reproduction
The benchmark suite in this repository measures the general query performance differences between v1 and v2 but does not cover the most performance-sensitive scenario: retrieving column values using strongly-typed getters (e.g., the reason java.sql.ResultSet#getLong() exists). In such cases, the new v2 client exhibits a ~100% performance drop. This regression affects any user who relies on getXxx() methods and migrates to the new ClickHouse JDBC driver.
I’ve submitted a PR that introduces 2 new benchmarks using strongly-typed getters. Below are the results from running them on my local machine:
- OpenJDK version: 24.0.2
- macOS 15.5, Apple M3 Pro
mvn compile exec:exec -Dexec.executable=java -Dexec.args="-classpath %classpath com.clickhouse.benchmark.BenchmarkRunner -m 3 -b q -l 300000"
QueryClient.queryV1 110 276.252 ± 31.343 ms/op
QueryClient.queryV2 125 245.277 ± 20.057 ms/op
QueryClient.queryV1WithTypes 144 209.260 ± 17.059 ms/op
QueryClient.queryV2WithTypes 68 454.188 ± 36.329 ms/op
As you can see, QueryClient.queryV1 and QueryClient.queryV2 perform similarly. However, QueryClient.queryV2WithTypes is more than 2x slower than QueryClient.queryV1WithTypes.
While the gap between QueryClient.queryV2 and QueryClient.queryV1WithTypes is around 20%, memory allocations are significantly higher in v2, increasing GC pressure which usually run concurrently.
Root cause
Two main differences in v2 contribute to the regression, both of which are not present in v1:
- It creates a new
Object[]for every row. This means every read triggers an array allocation, and all primitive values are boxed. This increases GC pressure and negatively impacts data locality.
In contrast, v1 reuses a single array with mutable wrappers to store values, avoiding these allocations entirely (seeClickHouseClientOption#REUSE_VALUE_WRAPPER). - In v2, reading a primitive value like
getLong(int)involves a chain of unnecessary hash table lookups:com.clickhouse.client.api.metadata.TableSchema#columnIndexToName->nameToIndex->nameToIndex.com.clickhouse.jdbc.ResultSetImpl#getLong(int)adds one more lookup on top of that chain. This results in 4HashMap.get()calls per column access by index to read every primitive column value.
Activity
chernser commentedon Aug 14, 2025
Good day, @alekkol !
Thank you for analysis! We agree with this.
alekkol commentedon Sep 8, 2025
@chernser
Using
Object[]forces wrapping of all primitive values. Unfortunately, Java value types are not available yet, so the internal representation is suboptimal. Compressed object headers help a little, but only in recent JVM versions.One possible alternative would be a byte-based representation. For example, the PostgreSQL JDBC driver uses
byte[][]for rows, where the first index represents a column. This allows primitives to be encoded efficiently (8 bytes for a long, 4 for an int, etc.).Just sharing the idea — glad to help!
dolfinus commentedon Mar 31, 2026
On 0.9.8
QueryClient.queryV2performance is twice as high thanQueryClient.queryV1.withTypesis just a bit slower:GC allocation looks higher, but error is larger than actual measure:
Raw data:
jmh-results-local-1774951135807.json
jmh-results-local-1774951135807.out.tar.gz
chernser commentedon Mar 31, 2026
@dolfinus
Thank you for the benchmark!
I keep this issue open until we implement all improvements.
Obviously we can do better.
Kvel4 commentedon Sep 10, 2026
We are seeing what appears to be the same performance problem after migrating the Trino ClickHouse connector from JDBC
0.7.1-patch1to0.10.0. For scan-heavy TPC-H queries, execution time increases by approximately1.94x.There is an important difference between this scenario and the
QueryClient.queryV2WithTypesbenchmark discussed above. That benchmark usesClickHouseBinaryFormatReaderdirectly, while Trino reads data through JDBCResultSet.The low-level reader supports direct indexed access, but many indexed getters in
ResultSetImpl, includinggetLong(int),getInt(int)andgetObject(int), still convert the column index to a name and delegate to the name-based implementation. The name-based path then resolves the name back to an index, sometimes multiple times for the same value.Trino has a long-standing JDBC access pattern where it first checks whether a value is NULL:
For a non-null value, Trino then calls the corresponding typed getter:
This pattern was also used with the v1 driver. However, v1 accessed the current row directly by column index, so both operations were relatively cheap. With JDBC v2, the same pattern causes repeated index-to-name and name-to-index conversions, map lookups, and generic object conversion for every non-null value.
Profiling of the affected workload shows these schema map lookups as a major CPU hotspot, while they are not present with the legacy driver.
Could you clarify whether completing direct indexed access in JDBC
ResultSetImplis covered by this issue? The remaining indexed getters could usereader.hasValue(columnIndex)and the correspondingreader.getXxx(columnIndex)method directly, similar to the existing implementation ofgetBytes(int).chernser commentedon Sep 10, 2026
Good day, @Kvel4 !
Thank you for bringing this!
This is planned for upcoming release.
However I suspect this is not a root cause of slowness.
Would you please share:
If possible please contribute to
performancesub-project in this repository. Thank you!chernser commentedon Sep 10, 2026
@polyglotAI-bot please implement it. Also update
performanceproject to have JDBC read with and without compression.Kvel4 commentedon Sep 11, 2026
Good day,
Thank you for the response.
To be transparent, this was not a fully isolated benchmark environment. The benchmark process, both Trino JVMs, and ClickHouse Server
24.3.14.35running in Docker were all located on the same host.This setup was intentionally chosen as a quick initial check for noticeable performance problems. If needed, I can run a properly isolated benchmark in a cloud environment.
Both Trino JVMs were running simultaneously, one with ClickHouse JDBC
0.7.1-patch1and the other with0.10.0. However, queries were executed against one Trino endpoint at a time, so the second instance was idle during each measurement. Both variants used the same ClickHouse server and the same physical tables.Dataset
The dataset was standard TPC-H at scale factor 4, approximately 4 GB of nominal source data and 34,636,634 rows in total:
regionnationsuppliercustomerpartpartsupporderslineitemBenchmark sequence
Before collecting measurements, the complete suite was executed once as a warm-up for each driver version.
The measured runs then used 10 alternating blocks with the following sequence in every block:
Each position in this sequence represented one complete execution of the ten-query suite. This produced:
The benchmark measured end-to-end wall-clock time through Trino.
Individual SF4 queries took approximately:
0.5–6.9 secondswith JDBC0.7.1-patch1;0.8–14.3 secondswith JDBC0.10.0.The median execution time for the complete ten-query suite was approximately:
29.5 secondswith JDBC0.7.1-patch1;58.3 secondswith JDBC0.10.0.This corresponds to the approximately
1.94xsuite-level degradation mentioned above.Host and resource configuration
-Xms1G -Xmx8Gper instancechernser commentedon Sep 11, 2026
@Kvel4
Thank you for sharing the info!
I will try to reproduce same setup to see how it affects the results. I just have a few thought:
What is the complete JVM opts string? is there GC selected?
Did you monitor with jmxconsole how heap and memory works?
Do you use JMH? can you share result files with gc allocation and similar information? just for comparison.
Thank you!
PS: we started working on the issue.
Kvel4 commentedon Sep 11, 2026
Both Trino JVMs and ClickHouse were running on the same host, but only one JVM received queries at a time. The other remained idle. The setup was not fully isolated, but JFR reported approximately 14.9% average host CPU utilization, with a maximum below 47%, so there was no CPU saturation.
The complete JVM options were:
-Xms1G -Xmx8G -XX:+UseG1GC -Xlog:gc*,safepoint:file=<file>:time,uptime,level,tagsBoth JVMs used OpenJDK 23.0.2 with G1GC.
I did not use JConsole. Instead, I recorded both JVMs with JFR using the
profileconfiguration and enabled unified GC logging after the warm-up.I also did not use JMH. The benchmark was executed with an internal end-to-end query runner that submits SQL queries to Trino sequentially and saves Trino's server-side query statistics.
I am attaching the JFR-derived reports and GC logs. The raw JFR recordings are also available if needed.
jfr-derived-reports.tar.gz