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

来源:互联网 发布:淘宝退货率怎么计算 编辑:程序博客网 时间:2024/05/22 03:02

自己写的一个测试用例。记录在案。

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 sec4312 msUsing Time: 4 sec4251 msUsing Time: 4 sec4220 msUsing Time: 4 sec4235 msUsing Time: 4 sec4236 msTotal Time: 21 secTotal Time: 21271 msover....


 

 

为什么要批量提交?一次提交量过大,也占用太多带宽,效率上不是很理想,如果遇到10万级别以上数据,估计不宕机也得等你个半天了。

原创粉丝点击