mysql批量插入

使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢?
在JDBC编程接口中Statement 有两个方法特别值得注意:
void addBatch() throws SQLException
Adds a set of parameters to this PreparedStatement object's batch of commands.
int[] 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()。

插入1000条数据的几种方式对比

方法一:

conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASS); 
pstmt = conn.prepareStatement("insert into loadtest (id, data) values (?, ?)"); 
for (int i = 1; i <= COUNT; i++) {     
	pstmt.clearParameters();     
	pstmt.setInt(1, i);     
	pstmt.setString(2, DATA);     
	pstmt.execute(); 
	} 

InnoDB:360.2秒

方法二:使用事务,不自动commit

conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASS); 
conn.setAutoCommit(false); 
pstmt = conn.prepareStatement("insert into loadtest (id, data) values (?, ?)"); 
for (int i = 1; i <= COUNT; i++) {     
	pstmt.clearParameters();     
	pstmt.setInt(1, i);     
	pstmt.setString(2, DATA);     
	pstmt.execute();     
	if (i % COMMIT_SIZE == 0) {         
		conn.commit();     
		} 
		} 
conn.commit(); 

InnoDB:31.5秒

方法三:executeBatch

conn = DriverManager.getConnection(JDBC_URL + "??useServerPrepStmts=false&rewriteBatchedStatements=true",JDBC_USER, JDBC_PASS); 
conn.setAutoCommit(false); 
pstmt = conn.prepareStatement("insert into loadtest (id, data) values (?, ?)"); 
for (int i = 1; i <= COUNT; i += BATCH_SIZE) {     
	pstmt.clearBatch();     
	for (int j = 0; j < BATCH_SIZE; j++) {         
		pstmt.setInt(1, i + j);         
		pstmt.setString(2, DATA);         
		pstmt.addBatch();     
		}     
	pstmt.executeBatch();     
	if ((i + BATCH_SIZE - 1) % COMMIT_SIZE == 0) {         
		conn.commit();     
		} 
		} 
conn.commit(); 

InnoDB:5.2秒

上面的使用时必须
1)rewriteBatchedStatements=true
2)useServerPrepStmts=false

rewriteBatchedStatements=true,mysql默认关闭了batch处理,通过此参数进行打开,这个参数可以重写向数据库提交的SQL语句,具体参见:http://www.cnblogs.com/chenjianjx/archive/2012/08/14/2637914.html
useServerPrepStmts=false,如果不开启(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement进行本地SQL拼装,最后送到db上就是已经替换了?后的最终SQL.

方法四:先load再commit

 conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASS); 
 conn.setAutoCommit(false); 
 pstmt = conn.prepareStatement("load data local infile '' " + "into table loadtest fields terminated by ','"); 
 StringBuilder sb = new StringBuilder(); 
 for (int i = 1; i <= COUNT; i++) {     
	sb.append(i + "," + DATA + "\n");     
	if (i % COMMIT_SIZE == 0) {         
		InputStream is = new ByteArrayInputStream(
			sb.toString().getBytes());         
			((com.mysql.jdbc.Statement) pstmt).setLocalInfileInputStream(is);         
			pstmt.execute();         
			conn.commit();         
			sb.setLength(0);     
			} 
			} 
InputStream is = new ByteArrayInputStream(sb.toString().getBytes()); 
((com.mysql.jdbc.Statement) pstmt).setLocalInfileInputStream(is); 
pstmt.execute(); 
conn.commit(); 

InnoDB:4.6秒

转载于:https://my.oschina.net/u/2000675/blog/868180

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值