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
{{ message }}
Repository navigation
[performance][jdbc] Reduce memory allocation in PreparedStatement #3333
jdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)performanceplanned_workWork that is planned for nearest release. Good to takequeriesArea: how the client builds and runs statements (parameters, quoting, batches)
Recommendation: Keep open — implementation needed
Planned work of epic #3324. The run gives a baseline on main: prepareStatement (about 56 KB for each call), setTimestamp, and addBatch allocate the most.
Status on main: Reproduced on main — one row of 4 values allocates about 1,055 B in set* + addBatch and 475 B in executeBatch; the RowBinary writer needs 459 B + 44 B.
Breaking change: No — can be done without a break.
Analysis: Each set* call converts its value to SQL text at once (encodeObject, PreparedStatementImpl.java:764-862); setTimestamp goes through a ZonedDateTime and an Instant to a fromUnixTimestamp64Nano(...) string (PreparedStatementImpl.java:498-501, 786-789). addBatch copies the VALUES template and the values into a new StringBuilder for each row (PreparedStatementImpl.java:322-330). prepareStatement parses the SQL on each call, and nothing caches the result (ConnectionImpl.java:458).
Reproduction: complete program and output
Allocations of the calling thread (ThreadMXBean), after a warm-up run of the same calls.
importjava.lang.management.ManagementFactory;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.Timestamp;
importjava.util.Properties;
publicclassRepro {
staticfinalStringURL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
staticfinalStringPASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
staticfinalcom.sun.management.ThreadMXBeanMX =
(com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
staticfinalStringINSERT = "INSERT INTO t3333 (id, name, ts, v) VALUES (?, ?, ?, ?)";
staticfinalStringNAME = "name-0123456789";
staticfinalTimestampTS = Timestamp.valueOf("2026-11-27 10:20:30.123");
staticfinalintN = 100_000;
interfaceOp { voidrun(PreparedStatementps, inti) throwsException; }
staticlongallocated() {
returnMX.getThreadAllocatedBytes(Thread.currentThread().getId());
}
staticvoidsetRow(PreparedStatementps, inti) throwsException {
ps.setLong(1, i);
ps.setString(2, NAME);
ps.setTimestamp(3, TS);
ps.setDouble(4, 1.5);
}
// bytes allocated by one call of op, after a warm-up run of the same N callsstaticvoidperCall(Connectionconn, Stringname, Opop) throwsException {
try (PreparedStatementps = conn.prepareStatement(INSERT)) {
setRow(ps, 0);
for (inti = 0; i < N; i++) op.run(ps, i);
ps.clearBatch();
longbefore = allocated();
for (inti = 0; i < N; i++) op.run(ps, i);
System.out.printf("%-40s %6d B/call%n", name, (allocated() - before) / N);
}
}
staticvoidbatch(Connectionconn, Stringname) throwsException {
try (PreparedStatementps = conn.prepareStatement(INSERT)) {
for (intround = 0; round < 2; round++) { // round 0 is the warm-uplongbefore = allocated();
for (inti = 0; i < N; i++) {
setRow(ps, i);
ps.addBatch();
}
longfilled = allocated();
ps.executeBatch();
longdone = allocated();
if (round == 1) {
System.out.printf("%-40s %6d B/row set+addBatch, %6d B/row executeBatch%n",
name + " (" + ps.getClass().getSimpleName() + ")",
(filled - before) / N, (done - filled) / N);
}
}
}
}
staticConnectionconnect(Stringextra) throwsException {
Propertiesprops = newProperties();
props.setProperty("user", "default");
props.setProperty("password", PASSWORD);
StringjdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default" + extra;
returnDriverManager.getConnection(jdbcUrl, props);
}
publicstaticvoidmain(String[] args) throwsException {
try (Connectionconn = connect("")) {
conn.createStatement().execute("DROP TABLE IF EXISTS t3333");
conn.createStatement().execute(
"CREATE TABLE t3333 (id Int64, name String, ts DateTime64(3), v Float64) ENGINE = Memory");
// prepareStatement parses the SQL for each callfor (intround = 0; round < 2; round++) {
longbefore = allocated();
for (inti = 0; i < 10_000; i++) conn.prepareStatement(INSERT).close();
if (round == 1) System.out.printf("%-40s %6d B/call%n", "prepareStatement (INSERT, 4 ?)", (allocated() - before) / 10_000);
}
perCall(conn, "setLong", (ps, i) -> ps.setLong(1, i));
perCall(conn, "setString (15 chars)", (ps, i) -> ps.setString(2, NAME));
perCall(conn, "setTimestamp", (ps, i) -> ps.setTimestamp(3, TS));
perCall(conn, "setDouble", (ps, i) -> ps.setDouble(4, 1.5));
perCall(conn, "addBatch (4 values set)", (ps, i) -> ps.addBatch());
batch(conn, "batch of " + N + " rows, default");
}
// contrast: the RowBinary writer of jdbc-v2 (beta option)try (Connectionconn = connect("?beta.row_binary_for_simple_insert=true")) {
batch(conn, "batch of " + N + " rows, RowBinary writer");
conn.createStatement().execute("DROP TABLE t3333");
}
}
}
jdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)performanceplanned_workWork that is planned for nearest release. Good to takequeriesArea: how the client builds and runs statements (parameters, quoting, batches)
Activity
polyglotAI-bot commentedon Nov 27, 2026
Recommendation: Keep open — implementation needed
Planned work of epic #3324. The run gives a baseline on
main:prepareStatement(about 56 KB for each call),setTimestamp, andaddBatchallocate the most.main: Reproduced on main — one row of 4 values allocates about 1,055 B inset*+addBatchand 475 B inexecuteBatch; the RowBinary writer needs 459 B + 44 B.set*andaddBatchcode, for large insert batches. Related: Potential performance issue with SQL parsing in jdbc-v2 #2585 — the CPU cost of the SQL parse in eachprepareStatement.Analysis: Each
set*call converts its value to SQL text at once (encodeObject,PreparedStatementImpl.java:764-862);setTimestampgoes through aZonedDateTimeand anInstantto afromUnixTimestamp64Nano(...)string (PreparedStatementImpl.java:498-501,786-789).addBatchcopies the VALUES template and the values into a newStringBuilderfor each row (PreparedStatementImpl.java:322-330).prepareStatementparses the SQL on each call, and nothing caches the result (ConnectionImpl.java:458).Reproduction: complete program and output
Allocations of the calling thread (
ThreadMXBean), after a warm-up run of the same calls.Output on
main(8480ca001), ClickHouse 26.11.2.4:Automated triage by Polyglot AI. A maintainer makes the final decision.