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%更新)吞吐量测试对比: