C# 如何加速将大型csv文件导入mysql数据库?
在这一刻,我有一个大的csv文件,它有11 MB,并包含了大量的数据插入到。。。excel文件的结尾。因此,10列中大约有100万行。现在我想写一个c代码,它可以更快地导入这个文件 我做了什么? 首先,我编写了从csv文件导入所有数据的代码:C# 如何加速将大型csv文件导入mysql数据库?,c#,mysql,.net,csv,import,C#,Mysql,.net,Csv,Import,在这一刻,我有一个大的csv文件,它有11 MB,并包含了大量的数据插入到。。。excel文件的结尾。因此,10列中大约有100万行。现在我想写一个c代码,它可以更快地导入这个文件 我做了什么? 首先,我编写了从csv文件导入所有数据的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syst
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
using System.IO;
using System.Configuration;
using MySql.Data.MySqlClient;
namespace ControlDataBase
{
public partial class Import_data_mysql : Form
{
public Import_data_mysql()
{
InitializeComponent();
}
New_Tables frm2 = (New_Tables)Application.OpenForms["New_Tables"];
private DataTable ImportFile()
{
DataTable imported_data = new DataTable();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open csv file";
ofd.DefaultExt = "*.csv";
ofd.Filter = "Documents (*.csv)|*.csv";
ofd.ShowDialog();
FileInfo fi = new FileInfo(ofd.FileName);
string FileName1 = ofd.FileName;
string excel = fi.FullName;
using(StreamReader sr = new StreamReader(excel))
{
string header = sr.ReadLine();
if (string.IsNullOrEmpty(header))
{
MessageBox.Show("Not found or loaded not correct file.");
return null;
}
string[] header_columns = header.Split(';');
foreach(string header_column in header_columns)
{
imported_data.Columns.Add(header_column);
}
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (string.IsNullOrEmpty(line)) continue;
string[] fields = line.Split(';');
DataRow imported_row = imported_data.NewRow();
for (int i = 0; i < fields.Count(); i++)
{
imported_row[i] = fields[i];
}
imported_data.Rows.Add(imported_row);
}
}
return imported_data;
}
但当我插入11MB的大文件时,它会导入很多时间,大约需要10分钟。在编译这段代码的一半时间里,进程内存大约有。。。5GB
现在我想知道如何加快从大型csv文件导入数据的速度<代码>MysqlBulkLoader就足够了吗?也许应该用另一种方式重写导入代码?有什么想法吗?谢谢你的帮助
我试过什么?
我试着在x64模式下运行,并在App.config中添加
。但是它还没有帮助。我认为ImportFile函数很好,不应该是个问题。
额外的时间是在SQLQuery和foreach上。您应该使用一些更新批处理函数,而不是一个查询一行
INSERT INTO tbl_name
(a,b,c)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);
1.从文件中生成导入的_数据
2.从导入的_数据构建Sql查询
3.运行查询
也许你可以同时做1和2。祝你好运
INSERT INTO tbl_name
(a,b,c)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);