自己写的一个测试用例。记录在案。
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 {
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();
break;
}
if (j % 200 == 0 && j != 0) {
starttime = System.currentTimeMillis();
ps.executeBatch();
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万级别以上数据,估计就宕机了。