mysql批处理

在调用prepareStatement.executeBatch();代码时 开启rewriteBatchedStatements会成倍提高执行效率

不开启则是单条sql提交
开启则是多条sql提交

但 提交的sql不能超过max_allowed_packet的设置值 不然报错

jdbc:mysql://localhost:3306/shopxxb2b2c?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false&rewriteBatchedStatements=true

存入500w条数据 8min

max_allowed_packet查询方法

进入mysql容器 运行

show VARIABLES like 'max_allowed_packet';
mysql> show VARIABLES like 'max_allowed_packet';
+--------------------+------------+
| Variable_name      | Value      |
+--------------------+------------+
| max_allowed_packet | 1073741824 |
+--------------------+------------+
1 row in set

max_allowed_packet的单位为字节:

-- 转化为Mb,就是1024Mb
mysql> select 1073741824/1024/1024;
+----------------------+
| 1073741824/1024/1024 |
+----------------------+
| 1024.00000000        |
+----------------------+
1 row in set

JdbcTemplate 插入100w条数据

import lombok.SneakyThrows;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

@Nonnull
private final JdbcTemplate jdbcTemplate;
    
    @SneakyThrows
    public void createProducts() {
        String sql = "INSERT INTO Student (id, version, introduction, name) VALUES (?,?,?,?)";
        DataSource dataSource = jdbcTemplate.getDataSource();

        try {
            Connection collection = null;
            try {
                collection = dataSource.getConnection();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            collection.setAutoCommit(true);
            PreparedStatement prepareStatement = collection.prepareStatement(sql);
            final int batchSize = 25000;
            AtomicInteger beginCount = new AtomicInteger();
            AtomicInteger endCount = new AtomicInteger();
            IntStream.rangeClosed(0, 39).forEach(index -> {
                beginCount.set(index * batchSize + 1);
                endCount.set(beginCount.get() + batchSize - 1);
                articleService.asyncInsertStudent(index, batchSize, prepareStatement, (long) beginCount.get(), (long) endCount.get());
            });
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
import org.springframework.scheduling.annotation.Async;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

//    @Async
    @Override
    public void asyncInsertStudent(int index, int batchSize, PreparedStatement prepareStatement, Long begin, Long end) {
        System.out.println("线程" + Thread.currentThread().getName() + " 执行异步任务:" + index);
        LongStream.rangeClosed(begin, end).forEach(i -> {
            try {
                prepareStatement.setLong(1, i);
                prepareStatement.setLong(2, 0L);
                prepareStatement.setString(3, "这是第" + i + "条测试数据!!!!!!!!");
                prepareStatement.setString(4, "测试华为手机" + i);

                prepareStatement.addBatch();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            if (i % batchSize == 0) {
                try {
                    prepareStatement.executeBatch();
                    prepareStatement.clearBatch();
                    System.out.println(i + "条数据执行完成");
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值