sharding jdbc do not support batch update
sharding jdbc do not support execute multiple SQL,like this:
mybatis
1.I don't think it will be supported in the future, because this kind of SQL may be routed to multiple data nodes, which will cause distributed transaction problems. 2. Just split batch SQL into a single sql , actually,batch update SQL with semicolon is just a pseudo batch-update , and the mysql server is still executed one by one, which can only cut a little network overhead, and the performance improvement is very limited.
which can only cut a little network overhead, and the performance improvement is very limited.
However, a single SQL must get the connection every time,In case of large quantity,The effect may be magnified
which can only cut a little network overhead, and the performance improvement is very limited.
However, a single SQL must get the connection every time,In case of large quantity,The effect may be magnified
Don't worry, this is usually not a problem because of connection pool.
you can show real SQL,if like update table set name =? where id =? in loop, is support.
Sorry, @fantacy2001 You means many of single SQL like update table set name = #{item.name,jdbcType=VARCHAR} where id = #{item.id,jdbcType=BIGINT} run at once?
I am not sure statement.executeBatch() is whether your concerns. Maybe not?
Sorry, @fantacy2001 You means many of single SQL like
update table set name = #{item.name,jdbcType=VARCHAR} where id = #{item.id,jdbcType=BIGINT}run at once? I am not surestatement.executeBatch()is whether your concerns. Maybe not?
The above ‘’foreach” is removed i mean this: mybatis mapper: < foreach collection="list" item="item" separator=";" > update table set name = #{item.name,jdbcType=VARCHAR} where id = #{item.id,jdbcType=BIGINT} < /foreach >
you can show real SQL,if like update table set name =? where id =? in loop, is support.
like this : < foreach collection="list" item="item" separator=";" > update table set name = #{item.name,jdbcType=VARCHAR} where id = #{item.id,jdbcType=BIGINT} < /foreach >
@wenweibin @fantacy2001
< foreach collection="list" item="item" separator=";" >
update table
set name = #{item.name,jdbcType=VARCHAR}
where id = #{item.id,jdbcType=BIGINT}
< /foreach >
If so, what SQL will be generated and sent to ShardingJDBC? Could you provide an actual SQL?
@wenweibin @fantacy2001
< foreach collection="list" item="item" separator=";" > update table set name = #{item.name,jdbcType=VARCHAR} where id = #{item.id,jdbcType=BIGINT} < /foreach >If so, what SQL will be generated and sent to ShardingJDBC? Could you provide an actual SQL?
Logic SQL: update stock_change_detail SET update_time=now() WHERE id = ? AND tenant_id = ?
and warehouse_no = ?
;
update stock_change_detail
SET update_time=now()
WHERE id = ?
AND tenant_id = ?
and warehouse_no = ?
Actual SQL: ds_1 ::: update stock_change_detail_023 SET update_time=now() WHERE id = ? AND tenant_id = ?
and warehouse_no = ?
;
update stock_change_detail
SET update_time=now()
WHERE id = ?
AND tenant_id = ?
and warehouse_no = ? ::: [1, jd, 26, 2, jd, 26]
two sql,same sharding value , but table is different
@fantacy2001 Hi thanks for your feedback. If the SQL below is one SQL request, I am sorry to say we currently do not support it and have no plan for it, as @wenweibin said.
update stock_change_detail
SET update_time=now()
WHERE id = ?
AND tenant_id = ?
and warehouse_no = ?
;
update stock_change_detail
SET update_time=now()
WHERE id = ?
AND tenant_id = ?
and warehouse_no = ?
@tristaZero @yu199195 @wenweibin @fantacy2001 I think batch update can be implemented by adding some restrictions. For example, multiple tables must be binding tables, and routing conditions must be the same to avoid distributed transaction problems. I want to take some time to implement this feature, because our company also needs this feature.
< foreach collection="list" item="item" separator=";" >
update table
set name = #{item.name,jdbcType=VARCHAR}
where id = #{item.id,jdbcType=BIGINT}
< /foreach > like this sql only first statement can rout right, i think this is an issue, and i want to know that anyone will fix it in feature?
< foreach collection="list" item="item" index="index" separator=";"> update t_table < set> < if test="item.buyPrice != null"> buy_price = #{item.buyPrice,jdbcType=DECIMAL}, < /if> < /set> < where> < if test="item.id != null"> and id = #{item.id,jdbcType=BIGINT} < /if> < if test="item.userId != null"> and user_id = #{item.userId,jdbcType=BIGINT} < /if> < if test="item.updateTime != null"> and update_time #{item.updateTime,jdbcType=TIMESTAMP} < /if> < /where> < /foreach>
my mybatis is like this. My database subtab key is user_id,The value of user_id is the same for all the batch SQL. Is this situation still not supported for batch updates?
To solve the batch update, you can set rewriteBatchedStatements to true, and then execute the following code.
public <T,U,R> int batchUpdateOrInsert(List<T> data, Class<U> mapperClass, BiFunction<T,U,R> function) {
int i = 1;
SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
U mapper = batchSqlSession.getMapper(mapperClass);
int size = data.size();
for (T element : data) {
function.apply(element,mapper);
if ((i % BATCH_SIZE == 0) || i == size) {
batchSqlSession.flushStatements();
}
i++;
}
batchSqlSession.commit(!TransactionSynchronizationManager.isSynchronizationActive());
} catch (Exception e) {
batchSqlSession.rollback();
throw new CustomException(e);
} finally {
batchSqlSession.close();
}
return i - 1;
}