java mysql批量insert多少条效率最高
1个回答
展开全部
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();
}
MyISAM:246.6秒、InnoDB:360.2秒
方法2: 使用事务,不自动commit
Java code
复制代码
代码如下:
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秒
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();
}
MyISAM:246.6秒、InnoDB:360.2秒
方法2: 使用事务,不自动commit
Java code
复制代码
代码如下:
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秒
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询