提高大数据量Sql插入方法(批处理指令)

本文对比了两种不同的方法来批量插入大量数据到MySQL数据库:逐条插入与使用批处理指令。通过具体实例展示了如何利用批处理提高数据插入效率,并讨论了相关注意事项。

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

首先随便建一个user表

 测试最低效的插入方法,直接一条一条的执行

/**
 * @Description:
 * @Author CHEN
 * @Date 2022/11/17 0017
 */
public class test1 {
    private final String url ="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai";
    private final String user = "root";
    private final String password = "root";
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    public void coon() throws Exception{
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        connection = DriverManager.getConnection(url,user,password);
    }
    public void close(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test(){
        long start = System.currentTimeMillis();
        String sql = "insert into user(name,pwd) VALUES (?,222)";
        try {
            coon();
            ps = connection.prepareStatement(sql);
            for (int i = 1; i <= 10000; i++) {
                ps.setObject(1, i);
                ps.execute();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        System.out.println("一万条数据插入用时:" + (System.currentTimeMillis() - start)+"【单位:毫秒】");
    }
}

此时插入一万条数据的消耗:

 2 加入批处理语句后

/**
 * @Description:
 * @Author CHEN
 * @Date 2022/11/17 0017
 */
public class test1 {
    private final String url ="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&&rewriteBatchedStatements=true";
    private final String user = "root";
    private final String password = "root";
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    public void coon() throws Exception{
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        connection = DriverManager.getConnection(url,user,password);
    }
    public void close(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test(){
        long start = System.currentTimeMillis();
        String sql = "insert into user(name,pwd) VALUES (?,222)";
        try {
            coon();
            ps = connection.prepareStatement(sql);
            for (int i = 1; i <= 10000; i++) {
                ps.setObject(1, i);
                ps.addBatch();
                if (i%100==0){
                    ps.executeBatch();
                    ps.clearBatch();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        System.out.println("一万条数据插入用时:" + (System.currentTimeMillis() - start)+"【单位:毫秒】");
    }}

此时的处理效率得到大大提高:

 此处注意在使用批处理指令时在连接串后面加上 rewriteBatchedStatements=true,代表允许重写批处理指令提交,以及此时的sql语句一定不能有分号,否则有【BatchUpdateException】异常.


     *      ps.addBatch();      将sql语句打包到一个容器中
     *      ps.executeBatch();  将容器中的sql语句提交
     *      ps.clearBatch();    清空容器,为下一次打包做准备

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金牌28号技师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值