這些日子研究ECPerf的原碼。今天下午,看到數據的載入。他是采用executeBatch實現的。他是批量的執行SQL語句。于是我寫了個測試程序來測試executeBatch的性能。
我采用的是MySQL數據庫,插入100000條紀錄,分為三部分測試,一是直接插入,二是使用executeBatch,三是批量是用executeBatch,下面試我得源碼和測試結果。
Connection conn = null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "qwe123");
conn.setAutoCommit(false);
PReparedStatement ps = conn.prepareStatement("insert into tb_test values (?,?)");
String date = new java.util.Date().toString();
long l = System.currentTimeMillis();
for(int i=0;i<100000;i++){
ps.setString(1, "axman");
ps.setString(2, date);
ps.executeUpdate();
conn.commit();
}
ps.executeBatch();
System.out.println(System.currentTimeMillis()-l);
long l1 = System.currentTimeMillis();
for(int i=0;i<100000;i++){
ps.setString(1, "axman");
ps.setString(2, date);
ps.addBatch();
conn.commit();
}
ps.executeBatch();
System.out.println(System.currentTimeMillis()-l1);
int a[] = new int[100];
Date b[] = new Date[100];
int j=0;
long l2 = System.currentTimeMillis();
for(int i=0;i<100000;i++){
新聞熱點
疑難解答