JDBC:大数据量插入的三种方法比较
2015-11-02 13:39
429 查看
测试的会后使用Oracle,MySQL效果不是很明显
向Oracle 的 customers 数据表中插入 10 万条记录
测试如何插入, 用时最短.
@Test
public void testBatch(){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = null;
try {
connection = JDBCTools.getConnection();
JDBCTools.beginTx(connection);
sql = "INSERT INTO customers VALUES(?,?,?)";
preparedStatement = connection.prepareStatement(sql);
Date date = new Date(new java.util.Date().getTime());
long begin = System.currentTimeMillis();
for(int i = 0; i < 100000; i++){
preparedStatement.setInt(1, i + 1);
preparedStatement.setString(2, "name_" + i);
preparedStatement.setDate(3, date);
//"积攒" SQL
preparedStatement.addBatch();
//当 "积攒" 到一定程度, 就统一的执行一次. 并且清空先前 "积攒" 的 SQL
if((i + 1) % 300 == 0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
}
//若总条数不是批量数值的整数倍, 则还需要再额外的执行一次.
if(100000 % 300 != 0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin)); //569
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
} finally{
JDBCTools.releaseDB(null, preparedStatement, connection);
}
}
@Test
public void testBatchWithPreparedStatement(){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = null;
try {
connection = JDBCTools.getConnection();
JDBCTools.beginTx(connection);
sql = "INSERT INTO customers VALUES(?,?,?)";
preparedStatement = connection.prepareStatement(sql);
Date date = new Date(new java.util.Date().getTime());
long begin = System.currentTimeMillis();
for(int i = 0; i < 100000; i++){
preparedStatement.setInt(1, i + 1);
preparedStatement.setString(2, "name_" + i);
preparedStatement.setDate(3, date);
preparedStatement.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin)); //9819
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
} finally{
JDBCTools.releaseDB(null, preparedStatement, connection);
}
}
/**
* 1. 使用 Statement.
*/
@Test
public void testBatchWithStatement(){
Connection connection = null;
Statement statement = null;
String sql = null;
try {
connection = JDBCTools.getConnection();
JDBCTools.beginTx(connection);
statement = connection.createStatement();
long begin = System.currentTimeMillis();
for(int i = 0; i < 100000; i++){
sql = "INSERT INTO customers VALUES(" + (i + 1)
+ ", 'name_" + i + "', '29-6月 -13')";
statement.executeUpdate(sql);
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin)); //39567
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
} finally{
JDBCTools.releaseDB(null, statement, connection);
}
}
向Oracle 的 customers 数据表中插入 10 万条记录
测试如何插入, 用时最短.
@Test
public void testBatch(){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = null;
try {
connection = JDBCTools.getConnection();
JDBCTools.beginTx(connection);
sql = "INSERT INTO customers VALUES(?,?,?)";
preparedStatement = connection.prepareStatement(sql);
Date date = new Date(new java.util.Date().getTime());
long begin = System.currentTimeMillis();
for(int i = 0; i < 100000; i++){
preparedStatement.setInt(1, i + 1);
preparedStatement.setString(2, "name_" + i);
preparedStatement.setDate(3, date);
//"积攒" SQL
preparedStatement.addBatch();
//当 "积攒" 到一定程度, 就统一的执行一次. 并且清空先前 "积攒" 的 SQL
if((i + 1) % 300 == 0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
}
//若总条数不是批量数值的整数倍, 则还需要再额外的执行一次.
if(100000 % 300 != 0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin)); //569
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
} finally{
JDBCTools.releaseDB(null, preparedStatement, connection);
}
}
@Test
public void testBatchWithPreparedStatement(){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = null;
try {
connection = JDBCTools.getConnection();
JDBCTools.beginTx(connection);
sql = "INSERT INTO customers VALUES(?,?,?)";
preparedStatement = connection.prepareStatement(sql);
Date date = new Date(new java.util.Date().getTime());
long begin = System.currentTimeMillis();
for(int i = 0; i < 100000; i++){
preparedStatement.setInt(1, i + 1);
preparedStatement.setString(2, "name_" + i);
preparedStatement.setDate(3, date);
preparedStatement.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin)); //9819
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
} finally{
JDBCTools.releaseDB(null, preparedStatement, connection);
}
}
/**
* 1. 使用 Statement.
*/
@Test
public void testBatchWithStatement(){
Connection connection = null;
Statement statement = null;
String sql = null;
try {
connection = JDBCTools.getConnection();
JDBCTools.beginTx(connection);
statement = connection.createStatement();
long begin = System.currentTimeMillis();
for(int i = 0; i < 100000; i++){
sql = "INSERT INTO customers VALUES(" + (i + 1)
+ ", 'name_" + i + "', '29-6月 -13')";
statement.executeUpdate(sql);
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin)); //39567
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
} finally{
JDBCTools.releaseDB(null, statement, connection);
}
}
相关文章推荐
- Daily Scrum 11.2
- 关于人工智能的二十件事
- UVa 514 Rails(栈)
- AIX的用户和组管理
- Codeforces 585B Phillip and Trains(dp)
- AEAI ESB V3.5.4开源发布,应用集成平台
- HDFS命令行工具
- AEAI ESB V3.5.4开源发布,应用集成平台
- 【首届大数据社区研讨会】听技术大咖如何解读大数据
- mac终端打开出现问题:[forkpty: Resource temporarily unavailable]
- Spark读写和Lost Excutor错误的分析和解决过程
- Kafka错误4_ 远程链接Kafka时报错Failed to send messages after 3 tries
- LeetCode 011 Container With Most Water
- 大数据量数据库优化
- 8.8.2 EXPLAIN Output Format 优化输出格式
- kafka.common.ConsumerRebalanceFailedException异常解决
- [Rails Level 2] Ground up
- Daily Scrum (2015/11/1)
- 如何通过AIDL实现跨应用绑定Service
- Daily Scrumming* 2015.11.1(Day 13)