• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Mysql批量插入拔高性能_mysql

mysql 搞代码 7年前 (2018-06-08) 162次浏览 已收录 0个评论

mysql批量插入提高性能

 

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

手动打开mysql批量插入的开关,性能才能表现出来,大家试试就知道啦。。

加上“?useServerPrepStmts=false&rewriteBatchedStatements=true

 

package com.xcfcky.demo;   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 = "****";       }          private void doStore() throws ClassNotFoundException, SQLException, IOException {           Class.forName("com.mysql.jdbc.Driver");           Connection conn = DriverManager.getConnection(connectStr, username,password);           conn.setAutoCommit(false); // 设置手动提交           PreparedStatement psts = conn.prepareStatement(insert_sql);           String line = null;                   //开始执行时间         long begin = System.currentTimeMillis();                  for(int i=0; i<500000; i++){           psts.setInt(1, i);           psts.setInt(2, i);             psts.setString(3, i + "ipstr1");             psts.setString(4, i + "ipstr2");             psts.setString(5, i + "ipstr3");             psts.addBatch();          // 加入批量处理           }          psts.executeBatch(); // 执行批量处理           conn.commit();  // 提交                    System.out.println("共用去时间" + (System.currentTimeMillis() - begin));         conn.close();       }                 public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException{      DbStoreHelper dbsh = new DbStoreHelper();      dbsh.doStore();     } }     插入50万条数据只需要14秒,性能明显提高  
在MySQL JDBC连接字符串中还可以加入参数,
rewriteBatchedStatements=truemysql默认关闭了batch处理,通过此参数进行打开,这个参数可以重写向数据库提交的SQL语句,具体参见:http://www.cnblogs.com/chenjianjx/archive/2012/08/14/2637914.html
useServerPrepStmts=false如果不开启(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement进行本地SQL拼装,最后送到db上就是已经替换了

 

欢迎大家阅读《Mysql批量插入拔高性能_mysql》,跪求各位点评,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Mysql批量插入拔高性能_mysql
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址