欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDBC-批处理-rewriteBatchedStatements=true,让你的批处理速度增长无数倍,自制一个数据导入工具

程序员文章站 2022-07-13 15:37:30
...

前言

今天学习了JDBC预编译的批处理,预编译批处理适用于单个sql语句,不同值的多次处理。

关键字段

连接的时候,默认是不会重写你的Batch的,这个时候就要在连接的后面添上。
url=jdbc:mysql://localhost:3306/day14_customer?useSSL=true&rewriteBatchedStatements=true ,这样它就会重写你的批处理了,效果真的是惊人啊!

小试牛刀

批量插入简单的100000条数据

    @Test
    public void insertDemo() {
        long startTime = System.currentTimeMillis();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            connection = JdbcUtils.getConnection();
            String sql = "insert into test(id,name) values(?,?)";
            statement = connection.prepareStatement(sql);

            for (int i = 1; i < 100000; i++) {
                statement.setInt(1, i);
                statement.setString(2, "" + i);
                statement.addBatch();

                if (i % 1000 == 0) {
                    statement.executeBatch();
                    statement.clearBatch();
                }
            }
            statement.executeBatch();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.release(connection, statement, resultSet);
        }

        long endTime = System.currentTimeMillis();
        System.out.println((endTime - startTime) / 1000);
    }

运行结果

5

只要5秒呢= =。
JDBC-批处理-rewriteBatchedStatements=true,让你的批处理速度增长无数倍,自制一个数据导入工具

再展身手

我之前知道用text文本导入数据库数据会比较快,今天我发现了更快的方法,还是自制的。目标:100000条记录

1.写好要用的文本txt

    @Test
    public void writeText() {

        try {
            File file = new File("/home/dream/桌面/111.txt");
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            PrintWriter writer = new PrintWriter(file);
            for (int i = 0; i < 100000; i++) {
                for (int j = 0; j < 10; j++) {
                    int in = random.nextInt(10);
                    builder.append(in);
                }
                String content = builder.toString();
                writer.println(content + "," + content + "," + content);
                builder.delete(0, builder.length());
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

文本长这样
JDBC-批处理-rewriteBatchedStatements=true,让你的批处理速度增长无数倍,自制一个数据导入工具

长度:
JDBC-批处理-rewriteBatchedStatements=true,让你的批处理速度增长无数倍,自制一个数据导入工具

2.先用navicat 手动导入text文件。结果如下:
JDBC-批处理-rewriteBatchedStatements=true,让你的批处理速度增长无数倍,自制一个数据导入工具

用时间38s
查看数据库
JDBC-批处理-rewriteBatchedStatements=true,让你的批处理速度增长无数倍,自制一个数据导入工具

3.自己用批处理写一个小工具
思路:

  • 读文件 每一行用一个分隔符分割成String数组,用计数器count记住处理次数
  • 通过数组依次预编译sql语句,就是往里面填数据
  • 批处理:每添加1000条语句,执行一次批,之后清楚批处理语句。为了防止最后未达到1000的尾数,在最后再进行一次execute
@Test
    public void insertDemo2() {
        long startTime = System.currentTimeMillis();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            connection = JdbcUtils.getConnection();
            String sql = "insert into test4(conten1, contnet2, content3) values(?,?,?)";
            statement = connection.prepareStatement(sql);

            BufferedReader reader = new BufferedReader(new FileReader(new File("/home/dream/桌面/111.txt")));
            String line;
            int count = 0;
            while ((line = reader.readLine()) != null) {
                String[] oneLine = line.split(",");
                for (int i = 0; i < oneLine.length; i++) {
                    statement.setString(i+1, oneLine[i]);
                }
                statement.addBatch();

                if (count % 1000 == 0) {
                    statement.executeBatch();
                    statement.clearBatch();
                }
                count ++;
            }

            reader.close();
            statement.executeBatch();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.release(connection, statement, resultSet);
        }

        long endTime = System.currentTimeMillis();
        System.out.println((endTime - startTime) / 1000);
    }

效果:5秒多
JDBC-批处理-rewriteBatchedStatements=true,让你的批处理速度增长无数倍,自制一个数据导入工具

数据:
JDBC-批处理-rewriteBatchedStatements=true,让你的批处理速度增长无数倍,自制一个数据导入工具

这速度快了不止几倍呀- -,重写的功能真的强大!,这样只要我们简单的封装一下方法,抽取参数就可以作为工具使用啦~是不是很快呢?十万数据,只要5秒!

总结

1.最后的execute执行batch的时候,位置千万不能写错,如果你的数据库写入数据很慢,一般两个问题,你没有加重写字段,或者你的代码写错了。
2.这是一次批处理的实战,和往常的经验做的比较,速度已经可以了,在我的认知范围内是最好的。我相信应该还有别的更好的
3.n个insert语句合并,就是多个values拼接字符串不如这个快的。虽然也能提升插入速度。
4.忽然想起来,如果使用多线程这种并发速度应该可以的把?没试过,感觉5秒已经可以了。也是一种想法把。

以上就是我的这次总结了,如果有更好的,或者更专业的记得留下你的指教呀~

相关标签: JDBC 批处理