Reprinted from: http://gooderlee.iteye.com/blog/1160949
Use jdbc to insert 100,000 records into the database, and use three methods: statement, PreparedStatement, and PreparedStatement+batch to test:
- public void exec(Connection conn){
- try {
- //Starting time
- Long beginTime = System.currentTimeMillis();
- //Set manual submission
- conn.setAutoCommit(false);
- Statement st = conn.createStatement();
- for(int i=0;i<100000;i++){
- String sql="insert into t1(id) values ("+i+")";
- st.executeUpdate(sql);
- }
- //End Time
- Long endTime = System.currentTimeMillis();
- System.out.println( "st:"+(endTime-beginTime)/ 1000+ "seconds"); //Calculate time
- st.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace ();
- }
- }
//2. Use PreparedStatement object
- public void exec2(Connection conn){
- try {
- Long beginTime = System.currentTimeMillis();
- conn.setAutoCommit( false); //Manual commit
- PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
- for(int i=0;i<100000;i++){
- pst.setInt(1, i);
- pst.execute();
- }
- conn.commit();
- Long endTime = System.currentTimeMillis();
- System.out.println( "pst:"+(endTime-beginTime)/ 1000+ "seconds"); //Calculate time
- pst.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace ();
- }
- }
//3. Use PreparedStatement + batch processing
- public void exec3(Connection conn){
- try {
- conn.setAutoCommit(false);
- Long beginTime = System.currentTimeMillis();
- //Construct the preprocessing statement
- PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
- //10,000 cycles
- for(int i=1;i<=100000;i++){
- pst.setInt(1, i);
- pst.addBatch();
- // every 1000 commits
- if(i% 1000== 0){ //You can set different sizes; such as 50, 100, 500, 1000, etc.
- pst.executeBatch();
- conn.commit();
- pst.clearBatch();
- }
- }
- Long endTime = System.currentTimeMillis();
- System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒");
- pst.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace ();
- }
- }
Tested in Oracle 10g, the results:
1. Using statement takes 142 seconds;
2. Using PreparedStatement takes 56 seconds;
3. Using PreparedStatement + batch processing:
a. 50 inserts once, it takes 5 seconds;
b. It takes 2 seconds to insert 100 records once;
c. It takes 1 second to insert more than 1000 records once;
from the above, it can be concluded that when using jdbc to insert data in large batches, the third method (PreparedStatement + batch processing) is obviously used. ) for better performance.
-------------------------------------------------------------------------------------------------------------------------------
- When inserting a large amount of data in the ordinary way, the processing speed is quite slow.
- */
- PreparedStatement ps = null;
- //loop 10000 times
- for(int i = 0; i < 100000; i++) {
- ps = con.prepareStatement(sql);
- ps.executeUpdate();
- }
- Method 2: Cache the data in the object by means of addBatch(), and submit it by executing the executeBatch(); method at the end, so the speed will be much faster!
- */
- PreparedStatement ps = con.prepareStatement(sql);
- for(int i = 0; i < 100000; i++) {
- ps.setString(1, "1");
- ps.setString(2, "2");
- ps.addBatch();
- }
- ps.executeBatch();