Preliminary preparationOrder Test Form CREATE TABLE `trade` ( `id` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci', `trade_no` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci', UNIQUE INDEX `id` (`id`), INDEX `trade_no` (`trade_no`) ) COMMENT = 'Order' COLLATE='utf8_unicode_ci' ENGINE=InnoDB; Test environment
Implementation method:1. Insert a single dataThis is the most common way, importing data one by one through a loop. The obvious disadvantage of this method is that it requires connecting to the database every time. Implementation code: //Start time var startTime = DateTime.Now; using (var conn = new MySqlConnection(connsql)) { conn.Open(); //Insert 100,000 data for (var i = 0; i < 100000; i++) { //Insert var sql = string.Format("insert into trade(id,trade_no) values('{0}','{1}');", Guid.NewGuid().ToString(), "trade_" + (i + 1) ); var sqlComm = new MySqlCommand(); sqlComm.Connection = conn; sqlComm.CommandText = sql; sqlComm.ExecuteNonQuery(); sqlComm.Dispose(); } conn.Close(); } //Completion time var endTime = DateTime.Now; //Time consuming var spanTime = endTime - startTime; Console.WriteLine("Loop insertion method takes: " + spanTime.Minutes + "minutes" + spanTime.Seconds + "seconds" + spanTime.Milliseconds + "milliseconds"); 100,000 tests take: In the above example, we are importing 100,000 records in batches and need to connect to the database 100,000 times. We change the SQL statement to 1000 and concatenate them into 1, which can reduce the database connection. The code is modified as follows: //Start time var startTime = DateTime.Now; using (var conn = new MySqlConnection(connsql)) { conn.Open(); //Insert 100,000 data var sql = new StringBuilder(); for (var i = 0; i < 100000; i++) { //Insert sql.AppendFormat("insert into trade(id,trade_no) values('{0}','{1}');", Guid.NewGuid().ToString(), "trade_" + (i + 1) ); //Merge and insert if (i % 1000 == 999) { var sqlComm = new MySqlCommand(); sqlComm.Connection = conn; sqlComm.CommandText = sql.ToString(); sqlComm.ExecuteNonQuery(); sqlComm.Dispose(); sql.Clear(); } } conn.Close(); } //Completion time var endTime = DateTime.Now; //Time consuming var spanTime = endTime - startTime; Console.WriteLine("Loop insertion method takes: " + spanTime.Minutes + "minutes" + spanTime.Seconds + "seconds" + spanTime.Milliseconds + "milliseconds"); 100,000 tests take: After optimization, the database connection time that originally required 100,000 times only needs to be connected 100 times. Judging from the final operating results, since the database is on the same server and does not involve network transmission, the performance improvement is not obvious. 2. Merge data insertionMySQL also supports batch data import by merging data. Implementation code: //Start time var startTime = DateTime.Now; using (var conn = new MySqlConnection(connsql)) { conn.Open(); //Insert 100,000 data var sql = new StringBuilder(); for (var i = 0; i < 100000; i++) { if (i % 1000 == 0) { sql.Append("insert into trade(id,trade_no) values"); } // concatenation sql.AppendFormat("('{0}','{1}'),", Guid.NewGuid().ToString(), "trade_" + (i + 1)); //Insert 1000 records at a time if (i % 1000 == 999) { var sqlComm = new MySqlCommand(); sqlComm.Connection = conn; sqlComm.CommandText = sql.ToString().TrimEnd(','); sqlComm.ExecuteNonQuery(); sqlComm.Dispose(); sql.Clear(); } } conn.Close(); } //Completion time var endTime = DateTime.Now; //Time consuming var spanTime = endTime - startTime; Console.WriteLine("Merge data insertion method takes time: " + spanTime.Minutes + "minutes" + spanTime.Seconds + "seconds" + spanTime.Milliseconds + "milliseconds"); 100,000 tests take: Inserting operations in this way can significantly improve the insertion efficiency of the program. Although the first method can also reduce the number of database connections after optimization, the second method reduces the amount of logs after merging (MySQL binlog and innodb transaction logs), reduces the amount and frequency of log flushing, and thus improves efficiency. At the same time, it can also reduce the number of SQL statement parsing times and reduce network transmission IO. 3. MySqlBulkLoader insertMySQLBulkLoader is also called LOAD DATA INFILE. Its principle is to read data from a file. So we need to save our dataset to a file and then read it from the file. Implementation code: //Start time var startTime = DateTime.Now; using (var conn = new MySqlConnection(connsql)) { conn.Open(); var table = new DataTable(); table.Columns.Add("id", typeof(string)); table.Columns.Add("trade_no", typeof(string)); //Generate 100,000 data for (var i = 0; i < 100000; i++) { if (i % 500000 == 0) { table.Rows.Clear(); } //Record var row = table.NewRow(); row[0] = Guid.NewGuid().ToString(); row[1] = "trade_" + (i + 1); table.Rows.Add(row); //Insert 500,000 records in a batch if (i % 500000 != 499999 && i < (100000 - 1)) { continue; } Console.WriteLine("Start inserting: " + i); //Convert data to csv format var tradeCsv = DataTableToCsv(table); var tradeFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "trade.csv"; File.WriteAllText(tradeFilePath, tradeCsv); #region Save to database var bulkCopy = new MySqlBulkLoader(conn) { FieldTerminator = ",", FieldQuotationCharacter = '"', EscapeCharacter = '"', LineTerminator = "\r\n", FileName = tradeFilePath, NumberOfLinesToSkip = 0, TableName = "trade" }; bulkCopy.Columns.AddRange(table.Columns.Cast<DataColumn>().Select(colum => colum.ColumnName).ToList()); bulkCopy.Load(); #endregion } conn.Close(); } //Completion time var endTime = DateTime.Now; //Time consuming var spanTime = endTime - startTime; Console.WriteLine("MySqlBulk method takes: " + spanTime.Minutes + "minutes" + spanTime.Seconds + "seconds" + spanTime.Milliseconds + "milliseconds"); 100,000 tests take: Note: The MySQL database configuration needs to be enabled: Allow file import. The configuration is as follows:
Performance test comparisonFor the above three methods, 100,000, 200,000, 1 million, and 10 million data records were tested respectively, and the final performance is as follows: at lastAccording to the test data, as the amount of data increases, the MySqlBulkLoader method still performs well, while the performance of other methods decreases significantly. The MySqlBulkLoader method can fully meet our needs. The above is the detailed content of the steps for .Net Core to import tens of millions of data into Mysql. For more information about importing tens of millions of data into Mysql, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
>>: Web page WB.ExecWB control printing method call description and parameter introduction
Table of contents Preface Install the graphics dr...
Table of contents Single condition single data fi...
Table of contents 1. Import files using script ta...
Get the number of connections --- Get the maximum...
This article example shares the specific code of ...
A Docker container starts a single process when i...
Table of contents Saltstack deploys zabbix servic...
Table of contents 1 Introduction 2 Prerequisites ...
The installation tutorial of mysql 8.0.11 winx64 ...
mysql copy one table column to another table Some...
GitHub address, you can star it if you like it Pl...
Spring integration with springmvc The web.xml con...
Why should we read the log? For example, if the c...
This article uses examples to illustrate the prin...
1. Find mysqldump.exe in the MySQL installation p...