Busy recently! Catch up with a lot of things!
In Hainan, more than 2 million of data needs to be inserted and deleted in batches. The first method written by a colleague needs to be run for more than 30 hours, after optimization, it can be reduced to 17 hours, which has been optimized a lot, but the customer still cannot accept the speed and then optimized, that is, to split a 2.3 million-row file into N 0.1 million-row files, and then use its program to read the files. It is still very slow, that is, it will start very quickly, but it will get slower and slower later. During the Spring Festival, he went home for the New Year and handed the optimization to me. Think about it and challenge it!
Before optimization, I read the code he wrote. He used the statement object and checked it online. 1. Used the statement object.
2. Pre-compile preparedstatement
3. Use preparedstatement + Batch Processing
In comparison, my colleagues use the statement object. The advantages and disadvantages are as follows:
Usage scope: when similar SQL statements (with the same structure and different values) are executed, the number of times is relatively small.
Advantage: simple syntax
Disadvantage: Hard coding is inefficient and the security is poor.
Principle: Hard coding. Similar SQL statements are compiled each time.
2. Pre-compile preparedstatement
Scope of use: when similar SQL statements are executed for a large number of times (such as user login, frequent table operations...), but the specific values are different, known as dynamic SQL
Advantage: the statement is compiled only once, reducing the number of compilations. Improved security (blocking SQL injection)
Disadvantage: slow execution of non-similar SQL statements.
Principle: similar SQL statements are compiled only once, reducing the number of compilations
Glossary:
SQL Injection: Select * from user where username = "Zhang San" and Password = "123" or 1 = 1;
The red part of the preceding statement uses SQL injection to make this statement return a record, thus reducing security.
3. Use preparedstatement + Batch Processing
Scope of use: Update multiple records in the database table at a time
Advantage: reduces the number of interactions with the SQL engine and improves efficiency again. Similar statements are compiled only once, reducing the number of compilations. Improved security (blocking SQL injection)
Disadvantages:
Principle:
Batch Processing: reduces the number of interactions with the SQL engine and passes Multiple SQL statements to the SQL engine at a time.
Glossary:
PL/SQL engine: the engine that executes PL/SQL code in Oracle. standard SQL statements are found during execution and handed over to the SQL engine for processing.
SQL engine: the engine that executes standard SQL.
After reading the comparison, it is obvious that the third method is fast, so I changed the program and used the third method to operate data in batches. The result speed is quite fast! It took 20 minutes to read and insert millions of rows of data into the database!
Example:
Public void exec3 (connection conn ){
Try {
Conn. setautocommit (false );
Long begintime = system. currenttimemillis ();
Preparedstatement PST = conn. preparestatement ("insert into T1 (ID) values (?) ");
For (INT I = 1; I <= 10000; I ++ ){
Pst. setint (1, I );
Pst. addbatch (); // Add to batch processing for packaging
If (I % 1000 = 0) {// you can set different sizes, such as 50,100,500,100 0.
Pst.exe cutebatch ();
Conn. Commit ();
Pst. clearbatch ();
}
}
Pst.exe cutebatch ();
Long endtime = system. currenttimemillis ();
System. Out. println ("Pst + batch:" + (endtime-begintime) + "millisecond ");
Pst. Close ();
Conn. Close ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
After the optimization, I sent it to Hainan for testing. As a result, it took him 30 minutes to finish reading the data. I was very happy. At least I optimized so much!
Later, when I sent a formal system to the other side, the results were not as fast as before! It took a few hours to get it done. It was strange that it would take 20 minutes to complete after running several times on the computer, but how long has it been there? Finally, let's think about it. It may be a problem with dblink. Of course, it's just speculation that the collective reason will have to wait until the end of the year to explore it!
Top!
This article is from the "Johnny's 0" blog, please be sure to keep this source http://johnny84.blog.51cto.com/2855387/767001