I went to the interview some time ago and asked how to efficiently insert 0.1 million records into the database. I have not handled class 20284 before. I have not read the relevant materials and I have not answered the questions, I checked some information today and summarized three methods: the test database is mysql !!! Method 1: publicstatic asked how to efficiently insert 0.1 million records to the database for an interview some time ago. I have not handled similar issues before, and I have not read the relevant materials. I did not answer the results, I checked some information today and summarized three methods:
The test database is mysql !!!
Method 1:
Public static void insert () {// start time Long begin = new Date (). getTime (); // SQL prefix String prefix = "insert into tb_big_data (count, create_time, random) VALUES"; try {// Save the SQL suffix StringBuffer suffix = new StringBuffer (); // Set the conn for non-automatic commit of transactions. setAutoCommit (false); // Statement st = conn. createStatement (); // pst is better than st in PreparedStatement pst = conn. prepareStatement (""); // Outer Loop, total number of transactions committed for (int I = 1; I <= 100; I ++) {// step for the first submission (int j = 1; j <= 10000; j ++) {// Construct the SQL suffix. append ("(" + j * I + ", SYSDATE ()," + I * j * Math. random () + "),");} // Construct the complete sqlString SQL = prefix + suffix. substring (0, suffix. length ()-1); // add and execute sqlpst. addBatch (SQL); // execute the operation pst.exe cuteBatch (); // submit the transaction conn. commit (); // clear the last added data suffix = new StringBuffer ();} // first-class connection pst. close (); conn. close ();} catch (SQLException e) {e. printStackTrace ();} // end time Long end = new Date (). getTime (); // time consumed System. out. println ("cast:" + (end-begin)/1000 + "ms ");}
Output Time: cast: 23 MS
This method is currently the most efficient method!
Method 2:
public static void insertRelease() {Long begin = new Date().getTime();String sql = "INSERT INTO tb_big_data (count, create_time, random) VALUES (?, SYSDATE(), ?)";try {conn.setAutoCommit(false);PreparedStatement pst = conn.prepareStatement(sql);for (int i = 1; i <= 100; i++) {for (int k = 1; k <= 10000; k++) {pst.setLong(1, k * i);pst.setLong(2, k * i);pst.addBatch();}pst.executeBatch();conn.commit();}pst.close();conn.close();} catch (SQLException e) {e.printStackTrace();}Long end = new Date().getTime();System.out.println("cast : " + (end - begin) / 1000 + " ms");}
Note: There will be no comments. similar to the above, there will be an analysis below!
Console output: cast: 111 MS
The execution time is five times that of the preceding method!
Method 3:
Public static void insertBigData (SpringBatchHandler sbh) {Long begin = new Date (). getTime (); JdbcTemplate jdbcTemplate = sbh. getJdbcTemplate (); final int count = 10000; String SQL = "INSERT INTO tb_big_data (count, create_time, random) VALUES (?, SYSDATE (),?) "; JdbcTemplate. batchUpdate (SQL, new BatchPreparedStatementSetter () {// set parameters for prepared statement. This method will be called throughout the process public void setValues (PreparedStatement pst, int I) throws SQLException {pst. setLong (1, I); pst. setInt (2, I) ;}// returns the number of updated result sets public int getBatchSize () {return count ;}}); Long end = new Date (). getTime (); System. out. println ("cast:" + (end-begin)/1000 + "ms ");}
This method uses spring batchUpdate for execution. due to efficiency problems, there are only 10 thousand data records!
Execution time: cast: 387 MS
Summary: Method 1 and method 2 are very similar. The only difference is that method 1 uses "insert into tb (...) values (...), (...)...;,
The second method is "insert into tb (...) values (...); insert into tb (...) values (...);... "way, if it wasn't for testing, I don't know the difference between the two is so big!
Of course, this is only the current test, and the specific execution time and step size also have a lot to do! If you change the step size to 100, it may take several minutes to test the method...
Methods are highly praised on the three networks. However, efficiency is also seen. it takes 6 minutes to record 10 thousand records, but the efficiency is not ideal! In addition, Method 3 requires configuring the spring applicationContext environment for application!
However, Method 3 is highly available in ssh/spring-mvc!
I started to study the big data issue just now. the above is only the result of a real test. it is not necessarily a fact. if you have good suggestions, please correct me. thank you!
Learn from each other to make progress faster!
The source code will be sent later. you can download and test it directly!