You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
jdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)performancequeriesArea: how the client builds and runs statements (parameters, quoting, batches)
Analytical SQL's and insert statements can be huge. When PreparedStatement is used this also complicated by composing SQL on client with all parameters. Heavy load happens when application need to insert big batch of information and uses, for example, convert function that makes impossible to use RowBinary format.
In this case we need to make certain optimizations.
Recommendation: Keep open — review PR #3524
Draft PR #3524 closes this issue. With its diff, a 200,000-row batch with a function in VALUES allocates 18 MB in place of 305 MB, in about 90 ms in place of 210 ms. It covers only single-group INSERT … VALUES: a SELECT with ? still builds the full SQL string.
Analysis:main keeps the full SQL text in memory several times: addBatch copies each VALUES tuple into a StringBuilder (PreparedStatementImpl.java:323-330), executeBatch joins all tuples into one more StringBuilder and String (PreparedStatementImpl.java:369-377), JDBC escape processing copies it again (StatementImpl.java:101, called at :407), and the HTTP layer copies it to a byte[] (HttpAPIClientHelper.java:663). Client.query takes only a String, so jdbc-v2 cannot stream a statement on main.
Reproduction: complete program and output
Allocations of the calling thread (ThreadMXBean); the table has the Null engine, so the server stores nothing.
importjava.lang.management.ManagementFactory;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.Statement;
importjava.util.Properties;
publicclassRepro {
staticfinalStringURL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
staticfinalStringPASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
staticfinalintROWS = 200_000;
staticfinalStringS = "some text value of about forty characters";
staticfinalStringTS = "2026-12-04 10:00:00";
staticfinalcom.sun.management.ThreadMXBeanMX =
(com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
publicstaticvoidmain(String[] args) throwsException {
Propertiesprops = newProperties();
props.setProperty("user", "default");
props.setProperty("password", PASSWORD);
StringjdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
try (Connectionconn = DriverManager.getConnection(jdbcUrl, props)) {
try (Statementst = conn.createStatement()) {
st.execute("DROP TABLE IF EXISTS triage_3523");
st.execute("CREATE TABLE triage_3523 (id UInt64, s String, ts DateTime) ENGINE = Null");
}
// The SQL size of one batch: each row is the VALUES template with the literals in it.longsqlChars = "INSERT INTO triage_3523 VALUES ".length();
for (inti = 0; i < ROWS; i++) {
sqlChars += ("(" + i + ", '" + S + "', toDateTime('" + TS + "'))").length() + 1;
}
System.out.printf("rows per batch: %d, SQL size: %.1f MB%n", ROWS, sqlChars / 1e6);
// A function in VALUES, so the RowBinary writer cannot be used.Stringsql = "INSERT INTO triage_3523 VALUES (?, ?, toDateTime(?))";
try (PreparedStatementps = conn.prepareStatement(sql)) {
System.out.println("PreparedStatement class: " + ps.getClass().getSimpleName());
for (intrun = 0; run < 4; run++) {
longtid = Thread.currentThread().getId();
longa0 = MX.getThreadAllocatedBytes(tid);
longt0 = System.nanoTime();
for (inti = 0; i < ROWS; i++) {
ps.setLong(1, i);
ps.setString(2, S);
ps.setString(3, TS);
ps.addBatch();
}
int[] res = ps.executeBatch();
longms = (System.nanoTime() - t0) / 1_000_000;
longmb = (MX.getThreadAllocatedBytes(tid) - a0) / 1_000_000;
System.out.printf("%s: %d rows, %d ms, %d MB allocated (%.1fx the SQL size)%n",
run == 0 ? "warm-up" : "batch " + run, res.length, ms, mb, mb * 1e6 / sqlChars);
}
}
try (Statementst = conn.createStatement()) {
st.execute("DROP TABLE triage_3523");
}
}
}
}
Output on main (f16f8e1a2), ClickHouse 26.11.7.10:
jdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)performancequeriesArea: how the client builds and runs statements (parameters, quoting, batches)
Activity
polyglotAI-bot commentedon Dec 4, 2026
Recommendation: Keep open — review PR #3524
Draft PR #3524 closes this issue. With its diff, a 200,000-row batch with a function in
VALUESallocates 18 MB in place of 305 MB, in about 90 ms in place of 210 ms. It covers only single-groupINSERT … VALUES: aSELECTwith?still builds the full SQL string.main: Reproduced on main — one batch makes 17.7 MB of SQL text, and the calling thread allocates about 305 MB (17×) for it.Analysis:
mainkeeps the full SQL text in memory several times:addBatchcopies eachVALUEStuple into aStringBuilder(PreparedStatementImpl.java:323-330),executeBatchjoins all tuples into one moreStringBuilderandString(PreparedStatementImpl.java:369-377), JDBC escape processing copies it again (StatementImpl.java:101, called at:407), and the HTTP layer copies it to abyte[](HttpAPIClientHelper.java:663).Client.querytakes only aString, so jdbc-v2 cannot stream a statement onmain.Reproduction: complete program and output
Allocations of the calling thread (
ThreadMXBean); the table has theNullengine, so the server stores nothing.Output on
main(f16f8e1a2), ClickHouse 26.11.7.10:Output with the diff of PR #3524 (
246ecece5) applied onmain:Automated triage by Polyglot AI. A maintainer makes the final decision.