使用JDBC在MySQL数据库中如何快速批量插入数据

作者:PlusPlus1 时间:2024-01-18 04:04:13 

使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢?

在JDBC编程接口中Statement 有两个方法特别值得注意:


void addBatch() throws SQLException

Adds a set of parameters to this PreparedStatement object's batch of commands.


int[] executeBatch() throws SQLException

Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch.

通过使用addBatch()和executeBatch()这一对方法可以实现批量处理数据。

不过值得注意的是,首先需要在数据库链接中设置手动提交,connection.setAutoCommit(false),然后在执行Statement之后执行connection.commit()。


package cyl.demo.ipsearcher;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DbStoreHelper {

private String insert_sql;
 private String charset;
 private boolean debug;

private String connectStr;
 private String username;
 private String password;

public DbStoreHelper() {
   connectStr = "jdbc:mysql://localhost:3306/db_ip";
   // connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";
   insert_sql = "INSERT INTO tb_ipinfos (iplong1,iplong2,ipstr1,ipstr2,ipdesc) VALUES (?,?,?,?,?)";
   charset = "gbk";
   debug = true;
   username = "root";
   password = "***";
 }

public void storeToDb(String srcFile) throws IOException {
   BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile), charset));
   try {
     doStore(bfr);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     bfr.close();
   }
 }

private void doStore(BufferedReader bfr) throws ClassNotFoundException, SQLException, IOException {
   Class.forName("com.mysql.jdbc.Driver");
   Connection conn = DriverManager.getConnection(connectStr, username,password);
   conn.setAutoCommit(false); // 设置手动提交
   int count = 0;
   PreparedStatement psts = conn.prepareStatement(insert_sql);
   String line = null;
   while (null != (line = bfr.readLine())) {
     String[] infos = line.split(";");
     if (infos.length < 5)  continue;
     if (debug) {
       System.out.println(line);
     }
     psts.setLong(1, Long.valueOf(infos[0]));
     psts.setLong(2, Long.valueOf(infos[1]));
     psts.setString(3, infos[2]);
     psts.setString(4, infos[3]);
     psts.setString(5, infos[4]);
     psts.addBatch();     // 加入批量处理
     count++;      
   }
   psts.executeBatch(); // 执行批量处理
   conn.commit(); // 提交
   System.out.println("All down : " + count);
   conn.close();
 }

}

执行完成以后:

All down : 103498 
Convert finished. 
All spend time/s : 47 

一共10W+,执行时间一共花费 47 秒.

这个效率仍然不高,似乎没有达到想要的效果,需要进一步改进。

在MySQL JDBC连接字符串中还可以加入参数,

rewriteBatchedStatements=true,mysql默认关闭了batch处理,通过此参数进行打开,这个参数可以重写向数据库提交的SQL语句。

useServerPrepStmts=false,如果不开启(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement进行本地SQL拼装,最后送到db上就是已经替换了?后的最终SQL.

在此稍加改进,连接字符串中加入下面语句(代码构造方法中去掉注释):
connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";

再次执行如下:

All down : 103498 
Convert finished. 
All spend time/s : 10 

同样的数据量,这次执行只花费了10秒 ,处理效率大大提高.

标签:mysql,jdbc,批量
0
投稿

猜你喜欢

  • MySQL 字符串模式匹配 扩展正则表达式模式匹配

    2024-01-20 14:47:41
  • python登录pop3邮件服务器接收邮件的方法

    2021-09-21 18:05:15
  • 对Python3中列表乘以某一个数的示例详解

    2023-05-05 03:10:40
  • python flask解析json数据不完整的解决方法

    2021-07-12 01:45:00
  • Langchain集成管理prompt功能详解

    2022-12-13 22:56:31
  • Python OpenCV图像处理之图像滤波特效详解

    2021-06-07 04:36:02
  • python求众数问题实例

    2022-02-06 22:25:40
  • JavaScript数组去重的方法总结【12种方法,号称史上最全】

    2024-05-09 15:02:46
  • 行转列之SQL SERVER PIVOT与用法详解

    2024-01-28 10:41:37
  • go和python变量赋值遇到的一个问题

    2023-11-15 01:54:40
  • Python制作简易注册登录系统

    2022-08-25 14:46:28
  • Python实现滑动平均(Moving Average)的例子

    2023-02-10 22:36:58
  • 构建Python包的五个简单准则简介

    2023-05-06 11:38:10
  • Golang 拷贝Array或Slice的操作

    2024-04-30 10:02:40
  • WAP中的ASP技术

    2008-05-18 13:30:00
  • MySQL decimal unsigned更新负数转化为0

    2024-01-14 20:59:36
  • Git中tag标签的使用教程

    2022-06-26 08:15:00
  • 使用apidoc管理RESTful风格Flask项目接口文档方法

    2022-11-24 10:05:14
  • 如何在页面中对不同的数据进行相同的处理?

    2010-06-26 12:30:00
  • python方向键控制上下左右代码

    2022-01-27 01:44:22
  • asp之家 网络编程 m.aspxhome.com