MySQL JDBC Statement.executeBatch实践(执行效率低)
现在很少使用原生jdbc去实现代码, 最近在测试MySQL批处理数据遇到一个问题: 执行Statement.executeBatch时效率极其低下(插入10000条数据), 断点调试时在此处停顿很长时间(908712ms)
数据版本:
select version();
// 5.7.17-11-V2.0R530D002-20190816-1203-log
数据库表:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(64) DEFAULT NULL,
`age` smallint(6) DEFAULT NULL,
`salary` decimal(10,2) DEFAULT NULL,
`max_size` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
测试代码如下:
public class BatchTest {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
batch("com.mysql.cj.jdbc.Driver",
"jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&allowMultiQueries=true",
"root", "123456");
}
private static void batch(String driver, String url, String username, String password) throws ClassNotFoundException, SQLExceptio