欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

JDBC批量Insert深度優化(沒事務)

系統 1959 0
JDBC批量Insert深度優化(沒事務)
?
最近在做一個數據同步分發工具,高并發,高效率,異步非實時是主要特點。
為此,選擇的方案是JDBC、只有兩種操作,插入和更新。
?
對于更新,只能逐條分批就可以了,優化空間不大。
對于插入,則可以做批量的優化,優化的策略只能是具體問題具體分析,以測試結論為主要依據了。
?
環境:
MySQL 5.1
RedHat Linux AS 5
JavaSE 1.5
DbConnectionBroker 微型數據庫連接池
?
測試的方案:
執行10萬次Insert語句,使用不同方式。
?
A組:靜態SQL,自動提交,沒事務控制(MyISAM引擎)
1、逐條執行10萬次
2、分批執行將10萬分成m批,每批n條,分多種分批方案來執行。
?
B組:預編譯模式SQL,自動提交,沒事務控制(MyISAM引擎)
1、逐條執行10萬次
2、分批執行將10萬分成m批,每批n條,分多種分批方案來執行。
-------------------------------------------------------------------------------------------
C組:靜態SQL,不自動提交,有事務控制(InnoDB引擎)
1、逐條執行10萬次
2、分批執行將10萬分成m批,每批n條,分多種分批方案來執行。
?
D組:預編譯模式SQL,不自動提交,有事務控制(InnoDB引擎)
1、逐條執行10萬次
2、分批執行將10萬分成m批,每批n條,分多種分批方案來執行。
?
本次主要測試A、B組,并得出測試結果。
?
SQL代碼
DROP TABLE IF EXISTS tuser;

CREATE TABLE tuser (
????id bigint (20) NOT NULL AUTO_INCREMENT,
???? name varchar (12) DEFAULT NULL ,
????remark varchar (24) DEFAULT NULL ,
????createtime datetime DEFAULT NULL ,
????updatetime datetime DEFAULT NULL ,
???? PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
?
?
下面是A、B組的測試代碼:
package testbatch;

import java.io.IOException;
import java.sql.*;

/**
* JDBC批量Insert優化(上)
*
* @author leizhimin 2009-7-29 10:03:10
*/

public class TestBatch {
???????? public static DbConnectionBroker myBroker = null ;

???????? static {
???????????????? try {
????????????????????????myBroker = new DbConnectionBroker( "com.mysql.jdbc.Driver" ,
???????????????????????????????????????? "jdbc:mysql: //192.168.104.163:3306/testdb",
???????????????????????????????????????? "vcom" , "vcom" , 2, 4,
???????????????????????????????????????? "c:\\testdb.log" , 0.01);
????????????????} catch (IOException e) {
????????????????????????e.printStackTrace();
????????????????}
????????}

???????? /**
???????? * 初始化測試環境
???????? *
???????? * @throws SQLException 異常時拋出
???????? */

???????? public static void init() throws SQLException {
????????????????Connection conn = myBroker.getConnection();
????????????????Statement stmt = conn.createStatement();
????????????????stmt.addBatch( "DROP TABLE IF EXISTS tuser" );
????????????????stmt.addBatch( "CREATE TABLE tuser (\n" +
???????????????????????????????? "????id bigint(20) NOT NULL AUTO_INCREMENT,\n" +
???????????????????????????????? "????name varchar(12) DEFAULT NULL,\n" +
???????????????????????????????? "????remark varchar(24) DEFAULT NULL,\n" +
???????????????????????????????? "????createtime datetime DEFAULT NULL,\n" +
???????????????????????????????? "????updatetime datetime DEFAULT NULL,\n" +
???????????????????????????????? "????PRIMARY KEY (id)\n" +
???????????????????????????????? ") ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8" );
????????????????stmt.executeBatch();
????????????????myBroker.freeConnection(conn);
????????}

???????? /**
???????? * 100000條靜態SQL插入
???????? *
???????? * @throws Exception 異常時拋出
???????? */

???????? public static void testInsert() throws Exception {
????????????????init();???????? //初始化環境
????????????????Long start = System.currentTimeMillis();
???????????????? for ( int i = 0; i < 100000; i++) {
????????????????????????String sql = "\n" +
???????????????????????????????????????? "insert into testdb.tuser \n" +
???????????????????????????????????????? "\t(name, \n" +
???????????????????????????????????????? "\tremark, \n" +
???????????????????????????????????????? "\tcreatetime, \n" +
???????????????????????????????????????? "\tupdatetime\n" +
???????????????????????????????????????? "\t)\n" +
???????????????????????????????????????? "\tvalues\n" +
???????????????????????????????????????? "\t('" + RandomToolkit.generateString(12) + "', \n" +
???????????????????????????????????????? "\t'" + RandomToolkit.generateString(24) + "', \n" +
???????????????????????????????????????? "\tnow(), \n" +
???????????????????????????????????????? "\tnow()\n" +
???????????????????????????????????????? ")" ;
????????????????????????Connection conn = myBroker.getConnection();
????????????????????????Statement stmt = conn.createStatement();
????????????????????????stmt.execute(sql);
????????????????????????myBroker.freeConnection(conn);
????????????????}
????????????????Long end = System.currentTimeMillis();
????????????????System.out.println( "單條執行100000條Insert操作,共耗時:" + (end - start) / 1000f + "秒!" );
????????}

???????? /**
???????? * 批處理執行靜態SQL測試
???????? *
???????? * @param m 批次
???????? * @param n 每批數量
???????? * @throws Exception 異常時拋出
???????? */

???????? public static void testInsertBatch( int m, int n) throws Exception {
????????????????init();???????????? //初始化環境
????????????????Long start = System.currentTimeMillis();
???????????????? for ( int i = 0; i < m; i++) {
???????????????????????? //從池中獲取連接
????????????????????????Connection conn = myBroker.getConnection();
????????????????????????Statement stmt = conn.createStatement();
???????????????????????? for ( int k = 0; k < n; k++) {
????????????????????????????????String sql = "\n" +
???????????????????????????????????????????????? "insert into testdb.tuser \n" +
???????????????????????????????????????????????? "\t(name, \n" +
???????????????????????????????????????????????? "\tremark, \n" +
???????????????????????????????????????????????? "\tcreatetime, \n" +
???????????????????????????????????????????????? "\tupdatetime\n" +
???????????????????????????????????????????????? "\t)\n" +
???????????????????????????????????????????????? "\tvalues\n" +
???????????????????????????????????????????????? "\t('" + RandomToolkit.generateString(12) + "', \n" +
???????????????????????????????????????????????? "\t'" + RandomToolkit.generateString(24) + "', \n" +
???????????????????????????????????????????????? "\tnow(), \n" +
???????????????????????????????????????????????? "\tnow()\n" +
???????????????????????????????????????????????? ")" ;
???????????????????????????????? //加入批處理
????????????????????????????????stmt.addBatch(sql);
????????????????????????}
????????????????????????stmt.executeBatch();???? //執行批處理
//????????????????????????stmt.clearBatch();????????//清理批處理
????????????????????????stmt.close();
????????????????????????myBroker.freeConnection(conn); //連接歸池
????????????????}
????????????????Long end = System.currentTimeMillis();
????????????????System.out.println( "批量執行" + m + "*" + n + "=" + m * n + "條Insert操作,共耗時:" + (end - start) / 1000f + "秒!" );
????????}

???????? /**
???????? * 100000條預定義SQL插入
???????? *
???????? * @throws Exception 異常時拋出
???????? */

???????? public static void testInsert2() throws Exception {???? //單條執行100000條Insert操作,共耗時:40.422秒!
????????????????init();???????? //初始化環境
????????????????Long start = System.currentTimeMillis();
????????????????String sql = "" +
???????????????????????????????? "insert into testdb.tuser\n" +
???????????????????????????????? "????(name, remark, createtime, updatetime)\n" +
???????????????????????????????? "values\n" +
???????????????????????????????? "????(?, ?, ?, ?)" ;
???????????????? for ( int i = 0; i < 100000; i++) {
????????????????????????Connection conn = myBroker.getConnection();
????????????????????????PreparedStatement pstmt = conn.prepareStatement(sql);
????????????????????????pstmt.setString(1, RandomToolkit.generateString(12));
????????????????????????pstmt.setString(2, RandomToolkit.generateString(24));
????????????????????????pstmt.setDate(3, new Date(System.currentTimeMillis()));
????????????????????????pstmt.setDate(4, new Date(System.currentTimeMillis()));
????????????????????????pstmt.executeUpdate();
????????????????????????pstmt.close();
????????????????????????myBroker.freeConnection(conn);
????????????????}
????????????????Long end = System.currentTimeMillis();
????????????????System.out.println( "單條執行100000條Insert操作,共耗時:" + (end - start) / 1000f + "秒!" );
????????}

???????? /**
???????? * 批處理執行預處理SQL測試
???????? *
???????? * @param m 批次
???????? * @param n 每批數量
???????? * @throws Exception 異常時拋出
???????? */

???????? public static void testInsertBatch2( int m, int n) throws Exception {
????????????????init();???????????? //初始化環境
????????????????Long start = System.currentTimeMillis();
????????????????String sql = "" +
???????????????????????????????? "insert into testdb.tuser\n" +
???????????????????????????????? "????(name, remark, createtime, updatetime)\n" +
???????????????????????????????? "values\n" +
???????????????????????????????? "????(?, ?, ?, ?)" ;
???????????????? for ( int i = 0; i < m; i++) {
???????????????????????? //從池中獲取連接
????????????????????????Connection conn = myBroker.getConnection();
????????????????????????PreparedStatement pstmt = conn.prepareStatement(sql);
???????????????????????? for ( int k = 0; k < n; k++) {
????????????????????????????????pstmt.setString(1, RandomToolkit.generateString(12));
????????????????????????????????pstmt.setString(2, RandomToolkit.generateString(24));
????????????????????????????????pstmt.setDate(3, new Date(System.currentTimeMillis()));
????????????????????????????????pstmt.setDate(4, new Date(System.currentTimeMillis()));
???????????????????????????????? //加入批處理
????????????????????????????????pstmt.addBatch();
????????????????????????}
????????????????????????pstmt.executeBatch();???? //執行批處理
//????????????????????????pstmt.clearBatch();????????//清理批處理
????????????????????????pstmt.close();
????????????????????????myBroker.freeConnection(conn); //連接歸池
????????????????}
????????????????Long end = System.currentTimeMillis();
????????????????System.out.println( "批量執行" + m + "*" + n + "=" + m * n + "條Insert操作,共耗時:" + (end - start) / 1000f + "秒!" );
????????}

???????? public static void main(String[] args) throws Exception {
????????????????init();
????????????????Long start = System.currentTimeMillis();
????????????????System.out.println( "--------A組測試----------" );
????????????????testInsert();
????????????????testInsertBatch(100, 1000);
????????????????testInsertBatch(250, 400);
????????????????testInsertBatch(400, 250);
????????????????testInsertBatch(500, 200);
????????????????testInsertBatch(1000, 100);
????????????????testInsertBatch(2000, 50);
????????????????testInsertBatch(2500, 40);
????????????????testInsertBatch(5000, 20);
????????????????Long end1 = System.currentTimeMillis();
????????????????System.out.println( "B組測試過程結束,全部測試耗時:" + (end1 - start) / 1000f + "秒!" );

????????????????System.out.println( "--------B組測試----------" );
????????????????testInsert2();
????????????????testInsertBatch2(100, 1000);
????????????????testInsertBatch2(250, 400);
????????????????testInsertBatch2(400, 250);
????????????????testInsertBatch2(500, 200);
????????????????testInsertBatch2(1000, 100);
????????????????testInsertBatch2(2000, 50);
????????????????testInsertBatch2(2500, 40);
????????????????testInsertBatch2(5000, 20);

????????????????Long end2 = System.currentTimeMillis();
????????????????System.out.println( "B組測試過程結束,全部測試耗時:" + (end2 - end1) / 1000f + "秒!" );
????????}
}
?
運行結果:
--------A組測試----------
單條執行100000條Insert操作,共耗時:36.766秒!
批量執行100*1000=100000條Insert操作,共耗時:33.625秒!
批量執行250*400=100000條Insert操作,共耗時:35.063秒!
批量執行400*250=100000條Insert操作,共耗時:35.296秒!
批量執行500*200=100000條Insert操作,共耗時:37.016秒!
批量執行1000*100=100000條Insert操作,共耗時:35.953秒!
批量執行2000*50=100000條Insert操作,共耗時:36.265秒!
批量執行2500*40=100000條Insert操作,共耗時:36.625秒!
批量執行5000*20=100000條Insert操作,共耗時:37.234秒!
B組測試過程結束,全部測試耗時:323.906秒!
--------B組測試----------
單條執行100000條Insert操作,共耗時:44.188秒!
批量執行100*1000=100000條Insert操作,共耗時:34.235秒!
批量執行250*400=100000條Insert操作,共耗時:34.328秒!
批量執行400*250=100000條Insert操作,共耗時:34.032秒!
批量執行500*200=100000條Insert操作,共耗時:33.625秒!
批量執行1000*100=100000條Insert操作,共耗時:34.125秒!
批量執行2000*50=100000條Insert操作,共耗時:33.797秒!
批量執行2500*40=100000條Insert操作,共耗時:35.359秒!
批量執行5000*20=100000條Insert操作,共耗時:36.218秒!
B組測試過程結束,全部測試耗時:320.0秒!
?
?
上面的測試結果似乎在意料之外,看來是用連接池技術,各種執行方式性能差異不是很大。細心觀察測試結果,可以得出一些結論。
結論:
?
以下結論以測試的環境和組別為前提:
?
數據庫連接池控制下,單線程,自動提交,沒事務控制(MyISAM引擎)
?
1、預定義SQL并沒有想象中的那么高效,因為兩組測試結果相差3秒,最短執行時間均為33.625秒!單項測試結果各有勝出。
?
2、批量執行時,分批的大小對效率影響也很大,靜態SQL以200-1000條分批執行為宜。預處理SQL以50-400條為宜。
?
3、通過結果看,預處理SQL效率稍稍勝出,因此批量執行時候優先選擇預定義SQL,預定義SQL還有個好處就是消耗的內存較少。靜態SQL串會占用大量的內存資源,容易導致內存溢出的問題。
?
4、在批處理執行的時候,每批執行完成后,最好顯式的調用pstmt.close()或stmt.close()方法,以便盡快釋放執行過的SQL語句,提高內存利用率。
?
5、談到優化方式,上面的批處理就是很好的優化策略。
?
6、數據庫連接池可以大大提高效率,如果沒有連接池管理,效率會大大降低,但不管怎么連接,上面的優化策略都是有效的。
?

本文出自 “ 熔 巖 ” 博客,轉載請與作者聯系!

JDBC批量Insert深度優化(沒事務)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日本国产成人精品视频 | 欧美性xx| 成人影院av| 久久久激情视频 | 91最新在线视频 | 在线观看亚洲网站 | 亚洲精品不卡久久久久久 | 新白娘子传奇50集免费赵雅芝版 | 高清一区二区 | 亚洲天堂免费在线 | 92精品国产自产在线观看48页 | 青娱乐精品视频在线观看 | 先锋av资源在线 | 麻豆传媒视频入口 | 亚洲精品久久视频 | 色综合激情 | 欧美精品在线免费观看 | 成人5252色 | 91精品久久久久久久久网影视 | 精品久久精品 | 欧美成人一级 | 日韩高清一区 | 欧美精品99久久久久久人 | 免费精品久久久久久中文字幕 | 日本高清天码一区在线播放 | 无码日本亚洲一区久久精品 | 欧美午夜艳片欧美精品 | 99久久久无码国产精品 | 777久久婷婷成人综合色 | 欧美精品成人免费视频 | 波多野中文字幕s | 精品久久久影院 | 欧美一区视频在线 | 一级毛片视频免费 | 乳罩双性受给攻喂奶高h | 久久久久久91香蕉国产 | 日本三级黄色片网站 | 亚洲精品一区二区三区精华液 | 亚洲国产欧美91 | 九九香蕉视频 | 久草视频福利在线观看 |