您的位置:首页 > 编程语言 > Java开发

JAVA中的SQL批处理及最后消耗的总时间的测试用例

2012-08-15 17:47 399 查看
自己写的一个测试用例。记录在案。

package Junt;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import com.whaty.platform.database.oracle.dbpool;

/**
* @name:TestBatch.java
* @desc:
* @author:lizhuang
* @createTime:2012-8-10 下午08:01:10
*/
public class TestBatch {

/**
* @param args
*/
public static void main(String[] args) {
testTime();
}

public static void testTime() {
dbpool pool = new dbpool();
Connection conn = pool.getConn();
Statement ps = null;
try {
conn.setAutoCommit(false);
ps = conn.createStatement();
long start= System.currentTimeMillis();
for (int i = 0; i < 1; i++) {
long starttime = 0;
for (int j = 0; j < 1002; j++) {
String sql = "UPDATE pe_bzz_examscore t  SET t.test_score=(select s.AVG_test_score from stat_study_summary s "
+ "where s.student_id='ff8080813104436201311415f0bf1234' and s.batch_id='4028809929f2e6870129f2fd0c390009') " + "where t.student_id='ff8080813104436201311415f0bf1234';";
ps.addBatch(sql);
if (j == 1001) {
ps.executeBatch();
conn.commit();
break;}if (j % 200 == 0 && j != 0) {starttime = System.currentTimeMillis();ps.executeBatch();

conn.commit();
long endtime = System.currentTimeMillis();
long totTime = endtime - starttime;
System.out.println("Using Time: " + totTime / 1000 + " sec\t" + totTime + " ms");
}
}

}
ps.close();
conn.close();
long end= System.currentTimeMillis();
System.out.println("Total Time: " + (end-start) / 1000 + " sec");
System.out.println("Total Time: " + (end-start) + " ms");
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("over....");
}

}


 

 

 

控制台显示:

Using Time: 4 sec	4312 ms
Using Time: 4 sec	4251 ms
Using Time: 4 sec	4220 ms
Using Time: 4 sec	4235 ms
Using Time: 4 sec	4236 ms
Total Time: 21 sec
Total Time: 21271 ms
over....


 

 

为什么要批量提交?一次提交量过大,也占用太多带宽,效率上不是很理想,如果遇到10万级别以上数据,估计不宕机也得等你个半天了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: