天天看點

MySQL批量Insert,HikariCP、線程池參數優化測試(續)MySQL批量Insert,HikariCP、線程池參數優化測試(續)

MySQL批量Insert,HikariCP、線程池參數優化測試(續)

前篇MySQL批量Insert,HikariCP、線程池參數優化測試講述了MySQL批量插入時優化參數的探索測試。深入研究優化參數時,發現前篇MySQL沒有真正開啟rewriteBatchedStatements參數,開啟該參數可大幅度提升寫入的性能。

rewriteBatchedStatements=true
           
MySQL預設關閉了batch處理,通過此參數進行打開,這個參數可以重寫向資料庫送出的SQL語句,具體參見:http://www.cnblogs.com/chenjianjx/archive/2012/08/14/2637914.html

配置

static{

        HikariConfig config = new HikariConfig();
      config.setJdbcUrl("jdbc:mysql://192.168.1.1:3306/test");
      config.setUsername("xxxx");
      config.setPassword("xxxx");
      config.addDataSourceProperty("cachePrepStmts", "true");
      config.addDataSourceProperty("prepStmtCacheSize", "250");
      config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

       config.addDataSourceProperty("rewriteBatchedStatements", "true");
      
      config.setMaximumPoolSize(30);
      config.setConnectionTimeout(6000);
      config.setValidationTimeout(3000);
      
      try {
        ds = new HikariDataSource(config);
        Properties prop = ds.getDataSourceProperties();
        System.out.println("rewriteBatchedStatements = " + prop.getProperty("rewriteBatchedStatements"));
      } catch (Exception e) {
    	  ds = null;
      }
    }
           

測試結果

HikariCP連接配接池30個連接配接、30個工作線程

--------C組測試----------
單條插入50000條記錄,耗時:86.3秒, 平均:579.38 TPS! 連接配接耗時:0.693
批量插入25批 * 2000條/批  (共50000條),耗時:2.13秒, 平均:23441.16 TPS! 連接配接耗時:0.0
批量插入50批 * 1000條/批  (共50000條),耗時:2.51秒, 平均:19928.26 TPS! 連接配接耗時:0.092
批量插入100批 * 500條/批  (共50000條),耗時:2.77秒, 平均:18024.51 TPS! 連接配接耗時:0.625
批量插入200批 * 250條/批  (共50000條),耗時:3.06秒, 平均:16350.56 TPS! 連接配接耗時:0.0
批量插入400批 * 125條/批  (共50000條),耗時:3.61秒, 平均:13861.93 TPS! 連接配接耗時:0.032
批量插入1000批 * 50條/批  (共50000條),耗時:5.46秒, 平均:9150.80 TPS! 連接配接耗時:0.0
批量插入2500批 * 20條/批  (共50000條),耗時:9.29秒, 平均:5380.97 TPS! 連接配接耗時:0.006
C組測試過程結束,全部測試耗時:117.158秒!
--------D組測試----------
單條插入50000條記錄,耗時:75.39秒, 平均:663.19 TPS! 連接配接耗時:0.092
批量插入25批 * 2000條/批  (共50000條),耗時:1.56秒, 平均:32030.75 TPS! 連接配接耗時:0.183
批量插入50批 * 1000條/批  (共50000條),耗時:1.69秒, 平均:29655.99 TPS! 連接配接耗時:0.107
批量插入100批 * 500條/批  (共50000條),耗時:1.87秒, 平均:26795.28 TPS! 連接配接耗時:0.02
批量插入200批 * 250條/批  (共50000條),耗時:2.73秒, 平均:18301.61 TPS! 連接配接耗時:0.001
批量插入400批 * 125條/批  (共50000條),耗時:2.86秒, 平均:17458.1 TPS! 連接配接耗時:0.002
批量插入1000批 * 50條/批  (共50000條),耗時:4.22秒, 平均:11842.73 TPS! 連接配接耗時:0.002
批量插入2500批 * 20條/批  (共50000條),耗時:6.80秒, 平均:7349.7 TPS! 連接配接耗時:0.006
D組測試過程結束,全部測試耗時:98.997秒!
           

小結

  • a) 相對于前篇MySQL批量Insert,HikariCP、線程池參數優化測試中的測試結果,開啟rewriteBatchedStatements參數,性能大約提升了5倍!
  • 批量update … where操作,相對于隻有insert into的測試結果,吞吐量最高
  • 批量insert into … on duplicate update操作
    • 如果on duplicate命中率為零,相對于隻有insert into的測試結果,吞吐量幾乎無影響
    • 如果on duplicate命中率為1/5,吞吐量相對于隻有insert into的測試結果,下降幅度明顯

以下是批量insert into、update、insert into … on duplicate update (零更新、20%更新)吞吐量測試對比:

MySQL批量Insert,HikariCP、線程池參數優化測試(續)MySQL批量Insert,HikariCP、線程池參數優化測試(續)