@Test
public void test(){
long time1 = new Date().getTime();
int count = 0;
String sql = "insert into ip_table(ip_add,country,time) values(?,?,?)";
try {
conn.setAutoCommit(false); //如何没有设置此参数,则要435秒,设置之后只有11秒
pst = conn.prepareStatement(sql);
} catch (SQLException e1) {
e1.printStackTrace();
}
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
count ++;
String ip = "24.91."+i+"."+j;
System.out.println(count);
try {
pst.setString(1, ip);
pst.setString(2, "US");
pst.setString(3, DateUtil.getStringCurrentDateTime());
pst.addBatch();
if(count % 1000 == 0){ //如果1000改为5000或10000,时间则为10秒
pst.executeBatch();
conn.commit();
pst.clearBatch(); //此行可以不要,速度一样
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
try {
pst.executeBatch();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
long time2 = new Date().getTime();
System.out.println("用时:"+(time2-time1)/1000+" 秒");
public void test(){
long time1 = new Date().getTime();
int count = 0;
String sql = "insert into ip_table(ip_add,country,time) values(?,?,?)";
try {
conn.setAutoCommit(false); //如何没有设置此参数,则要435秒,设置之后只有11秒
pst = conn.prepareStatement(sql);
} catch (SQLException e1) {
e1.printStackTrace();
}
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
count ++;
String ip = "24.91."+i+"."+j;
System.out.println(count);
try {
pst.setString(1, ip);
pst.setString(2, "US");
pst.setString(3, DateUtil.getStringCurrentDateTime());
pst.addBatch();
if(count % 1000 == 0){ //如果1000改为5000或10000,时间则为10秒
pst.executeBatch();
conn.commit();
pst.clearBatch(); //此行可以不要,速度一样
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
try {
pst.executeBatch();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
long time2 = new Date().getTime();
System.out.println("用时:"+(time2-time1)/1000+" 秒");
}
批量插入优化技巧
410

被折叠的 条评论
为什么被折叠?



