sql语句导致jdbctemplate.batchupdate分批插入缓慢,rewriteBatchedStatements也没有用

在项目中遇到使用jdbcTemplate的batchUpdate方法批量插入5万条数据缓慢的问题,通过分析源码和调整rewriteBatchedStatements参数并未显著提升速度。问题根源在于每条插入语句包含一个selectmax(objid)查询以获取最大主键。移除这个查询后,批量插入性能大幅提升,5万条数据插入仅需几秒钟。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我最近项目中遇到一个jdbctemplate的batchupdate方法批量插入的问题,5万条数据分批插入,每次批量插入1000条,结果半个小时数据还没插完。跟踪了一下batchupdate源码:

发现有判断这个rewriteBatchedStatements参数,于是在数据库连接url中加上

rewriteBatchedStatements=true

经过断点调试,发现执行了第二个批量插入,但是速度还是出奇的慢,于是我开始怀疑是不是插入的sql语句有什么问题。

看了一下查询sql:

INSERT INTO table_name (OBJID,XZSJ,XGSJ,PCH,H56000,H57000,H58000,H59000,H5A000) 
VALUES ((select ifnull(max(objid),0)+1 from table_name),now(),now(),'1300000000_3_20230531_02_Q',:H56000,:H57000,:H58000,:H59000,:H5A000)

 发现了猫腻,由于特殊原因,表没有使用自增主键,这里的插入语句values里面又用了select max()去查询最大主键,导致每次插入都会执行查询,于是我把这里的select max去掉,发现批量插入的速度就很快了,5万数据插入只花了几秒钟

clickhouse数据库jdbc0.9.0版本报错org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [INSERT INTO ads_test_licheng.dwd_sop_sale_order_test VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ]; SQL state [HY000]; error code [0]; -2110258699 at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1549) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:677) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:701) at org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:1047) at com.licheng.erpToWms.utils.ClickHouseUtils.Insert(ClickHouseUtils.java:47) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:351) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor.invoke(DynamicDataSourceAnnotationInterceptor.java:57) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:717) at com.licheng.erpToWms.utils.ClickHouseUtils$$SpringCGLIB$$0.Insert(<generated>) at com.licheng.erpToWms.utils.AssignUtils.batchInsert(AssignUtils.java:24) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:351) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:717) at com.licheng.erpToWms.utils.AssignUtils$$SpringCGLIB$$0.batchInsert(<generated>) at com.licheng.erpToWms.service.impl.SyncSaleOrderServiceImpl.lambda$syncSaleOrder$0(SyncSaleOrderServiceImpl.java:259) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) at java.base/java.lang.Thread.run(Thread.java:1583) Caused by: java.sql.SQLException: -2110258699 at com.clickhouse.jdbc.internal.ExceptionUtils.toSqlState(ExceptionUtils.java:69) at com.clickhouse.jdbc.internal.ExceptionUtils.toSqlState(ExceptionUtils.java:42) at com.clickhouse.jdbc.StatementImpl.executeUpdateImpl(StatementImpl.java:191) at com.clickhouse.jdbc.PreparedStatementImpl.executeInsertBatch(PreparedStatementImpl.java:331) at com.clickhouse.jdbc.PreparedStatementImpl.executeBatch(PreparedStatementImpl.java:297) at com.alibaba.druid.filter.FilterChainImpl.statement_executeBatch(FilterChainImpl.java:3101) at com.alibaba.druid.filter.FilterAdapter.statement_executeBatch(FilterAdapter.java:2506) at com.alibaba.druid.filter.FilterEventAdapter.statement_executeBatch(FilterEventAdapter.java:273) at com.alibaba.druid.filter.FilterChainImpl.statement_executeBatch(FilterChainImpl.java:3099) at com.alibaba.druid.proxy.jdbc.StatementProxyImpl.executeBatch(StatementProxyImpl.java:198) at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeBatch(DruidPooledPreparedStatement.java:551) at org.springframework.jdbc.core.JdbcTemplate.lambda$getPreparedStatementCallback$6(JdbcTemplate.java:1609) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:658) ... 35 more Caused by: com.clickhouse.client.api.ClientException: -2110258699 at com.clickhouse.client.api.internal.HttpAPIClientHelper.executeRequest(HttpAPIClientHelper.java:442) at com.clickhouse.client.api.Client.lambda$query$10(Client.java:1723) at com.clickhouse.client.api.Client.runAsyncOperation(Client.java:2156) at com.clickhouse.client.api.Client.query(Client.java:1766) at com.clickhouse.client.api.Client.query(Client.java:1667) at com.clickhouse.jdbc.StatementImpl.executeUpdateImpl(StatementImpl.java:184) ... 45 more Caused by: java.lang.NegativeArraySizeException: -2110258699 at java.base/java.lang.String.encodeUTF8_UTF16(String.java:1326) at java.base/java.lang.String.encodeUTF8(String.java:1299) at java.base/java.lang.String.encode(String.java:867) at java.base/java.lang.String.getBytes(String.java:1818) at com.clickhouse.client.api.Client.lambda$query$9(Client.java:1724) at com.clickhouse.client.internal.apache.hc.core5.http.io.entity.EntityTemplate.writeTo(EntityTemplate.java:82) at com.clickhouse.client.internal.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:253) at com.clickhouse.client.internal.apache.hc.core5.http.impl.io.HttpRequestExecutor.execute(HttpRequestExecutor.java:141) at com.clickhouse.client.internal.apache.hc.core5.http.impl.io.HttpRequestExecutor.execute(HttpRequestExecutor.java:218) at com.clickhouse.client.internal.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager$InternalConnectionEndpoint.execute(PoolingHttpClientConnectionManager.java:712) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.InternalExecRuntime.execute(InternalExecRuntime.java:216) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.MainClientExec.execute(MainClientExec.java:116) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.ConnectExec.execute(ConnectExec.java:188) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.ProtocolExec.execute(ProtocolExec.java:192) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.HttpRequestRetryExec.execute(HttpRequestRetryExec.java:96) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.ContentCompressionExec.execute(ContentCompressionExec.java:152) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.RedirectExec.execute(RedirectExec.java:115) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.InternalHttpClient.doExecute(InternalHttpClient.java:170) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:87) at com.clickhouse.client.internal.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:55) at com.clickhouse.client.internal.apache.hc.client5.http.classic.HttpClient.executeOpen(HttpClient.java:183) at com.clickhouse.client.api.internal.HttpAPIClientHelper.executeRequest(HttpAPIClientHelper.java:415) ... 50 more MyException(errorCode=DEFAULT_ERROR) at com.licheng.erpToWms.utils.ClickHouseUtils.Insert(ClickHouseUtils.java:66) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:351) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor.invoke(DynamicDataSourceAnnotationInterceptor.java:57) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:717) at com.licheng.erpToWms.utils.ClickHouseUtils$$SpringCGLIB$$0.Insert(<generated>) at com.licheng.erpToWms.utils.AssignUtils.batchInsert(AssignUtils.java:24) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:351) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:717) at com.licheng.erpToWms.utils.AssignUtils$$SpringCGLIB$$0.batchInsert(<generated>) at com.licheng.erpToWms.service.impl.SyncSaleOrderServiceImpl.lambda$syncSaleOrder$0(SyncSaleOrderServiceImpl.java:259) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) at java.base/java.lang.Thread.run(Thread.java:1583)
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值