欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用JDBC在MySQL數(shù)據(jù)庫中如何快速批量插入數(shù)據(jù)

 更新時(shí)間:2016年11月05日 16:54:41   作者:PlusPlus1  
這篇文章主要介紹了使用JDBC在MySQL數(shù)據(jù)庫中如何快速批量插入數(shù)據(jù),可以有效的解決一次插入大數(shù)據(jù)的方法,

使用JDBC連接MySQL數(shù)據(jù)庫進(jìn)行數(shù)據(jù)插入的時(shí)候,特別是大批量數(shù)據(jù)連續(xù)插入(10W+),如何提高效率呢?

在JDBC編程接口中Statement 有兩個(gè)方法特別值得注意:

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()這一對方法可以實(shí)現(xiàn)批量處理數(shù)據(jù)。

不過值得注意的是,首先需要在數(shù)據(jù)庫鏈接中設(shè)置手動(dòng)提交,connection.setAutoCommit(false),然后在執(zhí)行Statement之后執(zhí)行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); // 設(shè)置手動(dòng)提交 
    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(); // 執(zhí)行批量處理 
    conn.commit(); // 提交 
    System.out.println("All down : " + count); 
    conn.close(); 
  } 
 
} 

執(zhí)行完成以后:

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

一共10W+,執(zhí)行時(shí)間一共花費(fèi) 47 秒.

這個(gè)效率仍然不高,似乎沒有達(dá)到想要的效果,需要進(jìn)一步改進(jìn)。

在MySQL JDBC連接字符串中還可以加入?yún)?shù),

rewriteBatchedStatements=true,mysql默認(rèn)關(guān)閉了batch處理,通過此參數(shù)進(jìn)行打開,這個(gè)參數(shù)可以重寫向數(shù)據(jù)庫提交的SQL語句。

useServerPrepStmts=false,如果不開啟(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement進(jìn)行本地SQL拼裝,最后送到db上就是已經(jīng)替換了?后的最終SQL.

在此稍加改進(jìn),連接字符串中加入下面語句(代碼構(gòu)造方法中去掉注釋):
connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";

再次執(zhí)行如下:

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

同樣的數(shù)據(jù)量,這次執(zhí)行只花費(fèi)了10秒 ,處理效率大大提高.

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MySQL查看目前運(yùn)行狀況的兩種方法

    MySQL查看目前運(yùn)行狀況的兩種方法

    這篇文章給大家簡單的介紹一下MySQL查看目前運(yùn)行狀況命令,希望文章對大家會帶來一些幫助
    2014-02-02
  • MySQL數(shù)據(jù)歸檔小工具mysql_archiver詳解

    MySQL數(shù)據(jù)歸檔小工具mysql_archiver詳解

    這篇文章主要介紹了MySQL數(shù)據(jù)歸檔小工具mysql_archiver詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • MySQL數(shù)據(jù)類型和常用字段屬性總結(jié)

    MySQL數(shù)據(jù)類型和常用字段屬性總結(jié)

    這篇文章主要介紹了MySQL數(shù)據(jù)類型和常用字段屬性總結(jié),本文總結(jié)了日期和時(shí)間數(shù)據(jù)類型、數(shù)值數(shù)據(jù)類型、字符串?dāng)?shù)據(jù)類型等,需要的朋友可以參考下
    2014-09-09
  • 記一次Mysql不走日期字段索引的原因小結(jié)

    記一次Mysql不走日期字段索引的原因小結(jié)

    本文主要介紹了記一次Mysql不走日期字段索引的原因,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Canal監(jiān)聽MySQL的實(shí)現(xiàn)步驟

    Canal監(jiān)聽MySQL的實(shí)現(xiàn)步驟

    本文主要介紹了Canal監(jiān)聽MySQL的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • MySQL查看和修改最大連接數(shù)的方法步驟

    MySQL查看和修改最大連接數(shù)的方法步驟

    使用MySQL 數(shù)據(jù)庫的站點(diǎn),當(dāng)訪問連接數(shù)過多時(shí),就會出現(xiàn) "Too many connections" 的錯(cuò)誤,所以我們需要設(shè)置MySQL查看和修改最大連接數(shù),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • MySQL多表之間字段的匹配實(shí)現(xiàn)代碼

    MySQL多表之間字段的匹配實(shí)現(xiàn)代碼

    本文主要介紹下MySQL多表之間字段的匹配;如tag表中name匹配info中的name,接下來詳細(xì)介紹下,感興趣的你可以參考下哈
    2013-03-03
  • 部署MySQL延遲從庫的好處小結(jié)

    部署MySQL延遲從庫的好處小結(jié)

    這篇文章主要給大家介紹了部署MySQL延遲從庫的一些好處,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • mysql緩沖和緩存設(shè)置詳解

    mysql緩沖和緩存設(shè)置詳解

    本文主要給大家講解的是mysql優(yōu)化過程中比較重要的2個(gè)參數(shù)緩沖和緩存的設(shè)置,希望大家能夠喜歡
    2016-12-12
  • mysql 表索引的一些要點(diǎn)

    mysql 表索引的一些要點(diǎn)

    這篇文章主要介紹了mysql 表索引的一些注意事項(xiàng),mysql默認(rèn)優(yōu)化不如sqlserver,所以需要優(yōu)化,而一些成熟的cms為了利益,也不會幫大家數(shù)據(jù)庫優(yōu)化的很好,需要自己另外設(shè)置
    2013-11-11

最新評論