Home > Database > Mysql Tutorial > How Can Bulk Copying of DataTables Be Achieved in MySQL While Maintaining Performance?

How Can Bulk Copying of DataTables Be Achieved in MySQL While Maintaining Performance?

DDD
Release: 2024-10-24 06:14:02
Original
931 people have browsed it

How Can Bulk Copying of DataTables Be Achieved in MySQL While Maintaining Performance?

Using MySQLBulkLoader to Bulk Copy a DataTable

Question

In transitioning from Microsoft SQL Server to MySQL, how can bulk copying of DataTables be achieved without sacrificing performance?

Answer

While it may seem intuitive to avoid writing to a CSV file before inserting data, performance benchmarks indicate otherwise. Using the MySqlBulkLoader class to bulk-load data from a CSV file significantly outperforms direct inserts with a MySqlDataAdapter.

Implementation:

<code class="csharp">string tempCsvFileSpec = @"C:\Users\Gord\Desktop\dump.csv";

using (StreamWriter writer = new StreamWriter(tempCsvFileSpec))
{
    Rfc4180Writer.WriteDataTable(rawData, writer, false);
}

var msbl = new MySqlBulkLoader(conn);
msbl.TableName = "testtable";
msbl.FileName = tempCsvFileSpec;
msbl.FieldTerminator = ",";
msbl.FieldQuotationCharacter = '"';
msbl.Load();

System.IO.File.Delete(tempCsvFileSpec);</code>
Copy after login

This code dumps the DataTable into a temporary CSV file, bulk-loads the data, and then deletes the file. Benchmarking shows that this approach takes only 5-6 seconds, including the time for file I/O operations. In contrast, direct insertions with MySqlDataAdapter take approximately 30 seconds.

The above is the detailed content of How Can Bulk Copying of DataTables Be Achieved in MySQL While Maintaining Performance?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template