In csdn ask, http://bbs.csdn.net/topics/390357952? Page = 1 # post-393577221, which adopted the comments of all heroes. At last, when I inserted 1-5 million pieces of data on the machine, it seemed that I didn't feel the time used.
I used MySQL's batch data format,
For example
Insert into tables (lable1, lable2, lable3 ,...)
Values (num11, num12, num13 ,...),
(Num21, num22, num23 ,...),
....
(Numn1, numn2, numn3 ,..);
As described earlier, each collection point of each account generates several hundred pieces of data. I combine these hundreds of data into the preceding statement, I have generated a total of to data items for each of the collection points under more than 20 accounts. Therefore, a total of to data items are inserted each time.
Each insert statement is saved in a sqlstringlist.
I found some information on the Internet and used the transaction processing method. Originally, my transaction method was to put all SQL statements in one transaction, but some enthusiastic friends told me that "commit every time 1000 dbcommands are executed
(COMMIT) transaction, and then start the transaction again, this is better. Put too many commands in a transaction. Once the physical memory allocation limit is exceeded, yourProgramIt will become very slow and slow ."
So I modified it and restarted the transaction every 500 statements. C #CodeAs follows:
[CSHARP] View plaincopy
- /// <Summary>
-
- /// Execute multiple SQL statements to implement database transactions.
-
- /// </Summary> MySQL database
-
- /// <Param name = "sqlstringlist"> Multiple SQL statements </param>
- Public Static VoidExecutesqltran (list <String> Sqlstringlist)
-
- {
-
- Using(Mysqlconnection conn =NewMysqlconnection (mysqlhelper. connstr ))
-
- {
- Conn. open ();
-
- Mysqlcommand cmd =NewMysqlcommand ();
-
- Cmd. Connection = conn;
-
- Mysqltransaction Tx = conn. begintransaction ();
-
- Cmd. Transaction = TX;
- Try
-
- {
-
- For(IntN = 0; n <sqlstringlist. Count; n ++)
-
- {
- StringStrsql = sqlstringlist [N]. tostring ();
-
- If(Strsql. Trim (). length> 1)
-
- {
- Cmd. commandtext = strsql;
-
- Cmd. executenonquery ();
-
- }
-
- // Added later
- If(N> 0 & (N % 500 = 0 | n = sqlstringlist. Count-1 ))
-
- {
-
- TX. Commit ();
- Tx = conn. begintransaction ();
-
- }
-
- }
-
- // Tx. Commit (); // The original one-time submission
- }
-
- Catch(System. Data. sqlclient. sqlexception E)
-
- {
-
- TX. rollback ();
- Throw NewException (E. Message );
-
- }
-
- }
-
- }