使用JDBC在MySQL数据库中快速批量插入数据
2016-10-27 14:28
726 查看
http://www.2cto.com/database/201403/284326.html
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢?
在JDBC编程接口中Statement
有两个方法特别值得注意:
void
addBatch() throws SQLException
Adds
a set of parameters to this PreparedStatement object's batch of commands
[]
executeBatch() throws SQLException
Submits
a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according
to the order in which they were added to the batch.
通过使用addBatch()和executeBatch()这一对方法可以实现批量处理数据。
不过值得注意的是,首先需要在数据库链接中设置手动提交,connection.setAutoCommit(false),然后在执行Statement之后执行connection.commit()。
一共10W+,执行时间一共花费 47 秒.
这个效率仍然不高,似乎没有达到想要的效果,需要进一步改进。在MySQL JDBC连接字符串中还可以加入参数, rewriteBatchedStatements=true,mysql默认关闭了batch处理,通过此参数进行打开,这个参数可以重写向数据库提交的SQL语句,具体参见:http://www.cnblogs.com/chenjianjx/archive/2012/08/14/2637914.htmluseServerPrepStmts=false,如果不开启(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement进行本地SQL拼装,最后送到db上就是已经替换了?后的最终SQL.
在此稍加改进,连接字符串中加入下面语句(代码构造方法中去掉注释): connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";
再次执行如下:
?
同样的数据量,这次执行只花费了10秒 ,处理效率大大提高.
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢?
在JDBC编程接口中Statement
有两个方法特别值得注意:
void
addBatch() throws SQLException
Adds
a set of parameters to this PreparedStatement object's batch of commands
[]
executeBatch() throws SQLException
Submits
a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according
to the order in which they were added to the batch.
通过使用addBatch()和executeBatch()这一对方法可以实现批量处理数据。
不过值得注意的是,首先需要在数据库链接中设置手动提交,connection.setAutoCommit(false),然后在执行Statement之后执行connection.commit()。
一共10W+,执行时间一共花费 47 秒.
这个效率仍然不高,似乎没有达到想要的效果,需要进一步改进。在MySQL JDBC连接字符串中还可以加入参数, rewriteBatchedStatements=true,mysql默认关闭了batch处理,通过此参数进行打开,这个参数可以重写向数据库提交的SQL语句,具体参见:http://www.cnblogs.com/chenjianjx/archive/2012/08/14/2637914.htmluseServerPrepStmts=false,如果不开启(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement进行本地SQL拼装,最后送到db上就是已经替换了?后的最终SQL.
在此稍加改进,连接字符串中加入下面语句(代码构造方法中去掉注释): connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";
再次执行如下:
?
相关文章推荐
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- jsp使用JDBC在MySQL数据库中快速批量插入数据(几万行数据)
- 使用JDBC在MySQL数据库中如何快速批量插入数据
- 使用JDBC在MySQL数据库中如何快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- 使用JDBC在MySQL数据库中快速批量插入数据
- java 使用jdbc向mysql数据库中插入1亿条数据
- 使用jdbc进行多数据的批量插入数据库(Oracle)
- JDBC批量插入数据优化,使用addBatch和executeBatch
- 使用JDBC向MySQL数据库批次插入10W条数据测试效率
- java 使用jdbc连接mysql数据库插入数据乱码