Skip to content

[performance][jdbc] Insert operation when RowBinary not available #3332

Open

Description

@chernser

Description

The main pain point in V1 and V2 is wiring big batches when RowBinary insert cannot be used automatically.
However many parts of this process can be optimized:

  • avoid creating strings on each set
  • streamline writing big SQL statements

Activity

  1. added this to the 0.12.0 milestone on Nov 11, 2026
  2. added theissue type on Nov 11, 2026
  3. added
    jdbc
    planned_work
    on Nov 11, 2026
  4. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — implementation needed
    Planned work of epic #3324. The run on main shows where a batch without RowBinary allocates: about 13 bytes for each character of SQL text in set* + addBatch, and about 7 more in executeBatch.

    Analysis: Each set* call makes a SQL text String (encodeObject, PreparedStatementImpl.java:764-862), and addBatch copies the VALUES tuple of the row into a new StringBuilder (PreparedStatementImpl.java:322-330). executeBatch appends all tuples to one more StringBuilder and makes a String of it (PreparedStatementImpl.java:368-377), JDBC escape processing copies the text again (StatementImpl.java:101-107, called at StatementImpl.java:407), and the HTTP layer copies it to a byte[] (HttpAPIClientHelper.java:663). The RowBinary writer runs only with beta.row_binary_for_simple_insert=true and one VALUES list (ConnectionImpl.java:460-480).

    Reproduction: complete program and output

    Allocations of the calling thread (ThreadMXBean), after a warm-up run of the same batch.

    import com.clickhouse.jdbc.StatementImpl;
    
    import java.lang.management.ManagementFactory;
    import java.lang.reflect.Method;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.util.Properties;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
        static final com.sun.management.ThreadMXBean MX =
                (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
        static final String INSERT = "INSERT INTO t3332 (id, name) VALUES (?, ?)";
        static final String NAME = "name-0123456789";
        static final int N = 100_000;
    
        static long allocated() {
            return MX.getThreadAllocatedBytes(Thread.currentThread().getId());
        }
    
        static void batch(Connection conn, String name) throws Exception {
            try (PreparedStatement ps = conn.prepareStatement(INSERT)) {
                for (int round = 0; round < 2; round++) { // round 0 is the warm-up
                    long before = allocated();
                    for (int i = 0; i < N; i++) {
                        ps.setLong(1, i);
                        ps.setString(2, NAME);
                        ps.addBatch();
                    }
                    long filled = allocated();
                    ps.executeBatch();
                    long done = allocated();
                    if (round == 0) continue;
                    String line = String.format("%s (%s): set+addBatch %d B/row, executeBatch %d B/row",
                            name, ps.getClass().getSimpleName(), (filled - before) / N, (done - filled) / N);
                    if (ps.getClass().getSimpleName().equals("PreparedStatementImpl")) {
                        // the SQL text that executeBatch sent (StatementImpl#getLastStatementSql is protected)
                        Method m = StatementImpl.class.getDeclaredMethod("getLastStatementSql");
                        m.setAccessible(true);
                        String sql = (String) m.invoke(ps);
                        line += String.format(", SQL text %d chars (%d chars/row)", sql.length(), sql.length() / N);
                    }
                    System.out.println(line);
                }
            }
        }
    
        static Connection connect(String extra) throws Exception {
            Properties props = new Properties();
            props.setProperty("user", "default");
            props.setProperty("password", PASSWORD);
            String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default" + extra;
            return DriverManager.getConnection(jdbcUrl, props);
        }
    
        public static void main(String[] args) throws Exception {
            try (Connection conn = connect("")) {
                conn.createStatement().execute("DROP TABLE IF EXISTS t3332");
                conn.createStatement().execute("CREATE TABLE t3332 (id Int64, name String) ENGINE = Memory");
                batch(conn, "default");
            }
            // contrast: the RowBinary writer of jdbc-v2 (beta option)
            try (Connection conn = connect("?beta.row_binary_for_simple_insert=true")) {
                batch(conn, "RowBinary writer");
                conn.createStatement().execute("DROP TABLE t3332");
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    default (PreparedStatementImpl): set+addBatch 351 B/row, executeBatch 182 B/row, SQL text 2688925 chars (26 chars/row)
    RowBinary writer (WriterStatementImpl): set+addBatch 163 B/row, executeBatch 28 B/row
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  5. added
    queries
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jdbcperformanceplanned_workqueries

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions