標籤:blog io ar os 使用 sp java for 資料
最近公司要求測試資料庫的效能,就上網查了一些批量插入資料的代碼,發現有好幾種不同的用法,插入同樣資料的耗時也有區別
別的先不說,先上一段代碼與君共用
方法一:
package com.bigdata;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;import java.sql.PreparedStatement;public class TestBigData {/** * @param args * @throws Exception * @throws IllegalAccessException * @throws InstantiationException */public static void main(String[] args) throws Exception {//--------------------------------串連資料庫----------------------String driver="com.mysql.jdbc.Driver";String url="jdbc:mysql://127.0.0.1:3306/test";String user="root";String password="root";//1、建立驅動Driver driverInstance = (Driver) Class.forName(driver).newInstance();//2、註冊驅動DriverManager.registerDriver(driverInstance);//3、擷取串連Connection conn = DriverManager.getConnection(url, user, password);//----------------------對資料庫進行操作-------------------//記錄開始時間Long begin=System.currentTimeMillis();//-----------插入資料----------//sql語句首碼String sqlPre="insert into tb_big_db (count,create_time,random) values ";StringBuffer sb = new StringBuffer();//設定事務為非自動認可conn.setAutoCommit(false);//使用PrepareStatement更好PreparedStatement pstate = conn.prepareStatement("");//--------------------------十萬條資料-------------//設定外迴圈,總提交事務的次數for(int i=0;i<100;i++){for(int j=0;j<10000;j++){//構建sql尾碼sb.append("("+j*i+",SYSDATE(),"+i*j*Math.random()+"),");}//構建完整的sqlString sql = sqlPre + sb.substring(0, sb.length()-1);//添加sqlpstate.addBatch(sql);//執行sqlpstate.executeBatch();//提交事務conn.commit();//清空StringBuffer上一次添加的sql語句sb = new StringBuffer();}//大迴圈完畢,關閉串連pstate.close();conn.close();//結束時間Long end = System.currentTimeMillis();System.out.println("10萬條資料,插入資料庫耗時:"+(end-begin)+"ms");}}
耗時:1952ms
說明:這速度,不堪入目啊,公司的資料庫,不管從硬體還是軟體來看,都不行呐。
方法二:
package com.bigdata;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;import java.sql.PreparedStatement;public class TestBigData2 {public static void main(String[] args) throws Exception{//--------------------------------串連資料庫----------------------String driver="com.mysql.jdbc.Driver";String url="jdbc:mysql://127.0.0.1:3306/test";String user="root";String password="root";//1、建立驅動Driver driverInstance = (Driver) Class.forName(driver).newInstance();//2、註冊驅動DriverManager.registerDriver(driverInstance);//3、擷取串連Connection conn = DriverManager.getConnection(url, user, password);//-----------------------------------操作資料庫-----------------//記錄開始時間Long begin = System.currentTimeMillis();//動態sql語句String sql = "insert into tb_big_db (count,create_time,random) values (?,SYSDATE(),?)";//設定事務為非自動認可conn.setAutoCommit(false);//先行編譯sqlPreparedStatement pstate = conn.prepareStatement(sql);//外迴圈10次for(int i=0;i<10;i++){//內迴圈10000次for(int j=0;i<1000;j++){pstate.setLong(1, i*j);pstate.setLong(2, i*j);//添加到批處理上pstate.addBatch();}//批處理pstate.executeBatch();//提交conn.commit();}//關閉pstate.close();conn.close();//結束時間Long end = System.currentTimeMillis();System.out.println("插入10萬條資料,耗時:"+(end-begin)+"ms");}}
耗時:未知,原因是我用這個方法測試的時候,居然給我報記憶體溢出異常,說明這個方法需要的記憶體大,耗時高,所以我和上面的方法一做了個對比。
由於2個方法都用來prepareStatement類,該類在執行sql語句之前會對其進行先行編譯,就是說先把sql準備好先,用到的時候就直接用就可以了
方法一:通過程式碼分析,可以明顯看到,方法一種的sql語句通過拼接成10條sql語句,故資料庫執行語句時,只要執行10條就好
方法二:可以看出,它每迴圈一次,就要addBach()一次,速度變得更加慢,有10000條就addBatch()一萬次,再批處理10次,所以速度變得慢是理所當然的
MySQL批量插入資料的幾種方法