如何从asp.net一次性将批量数据插入到mysql表中

发布于 2024-09-03 16:06:46 字数 305 浏览 5 评论 0原文

我有一个要求,我需要使用asp.net/C#读取Excel表并将所有记录插入到mysql表中。Excel表由大约2000行和50列组成。目前,在读取Excel记录时,我正在使用prepare语句将记录一条一条地插入到mysql表中。但是由于数据量巨大,因此需要大约70秒的时间。

我还考虑过创建一个新的数据行,为每个单元格分配值,将生成的数据行添加到数据表中,最后调用 dataadapter.update(...)。但这似乎很复杂,因为我有大约 50 列,因此我必须为数据行分配 50 个值。

有人可以建议是否有替代方法来提高插入的性能吗?

谢谢

I have a requirement that I need to read an excel sheet using asp.net/C# and insert all the records into mysql table.The excel sheet consists of around 2000 rows and 50 columns. Currently,upon reading the excel records ,I am inserting the records one by one using a prepare statement into mysql table.But its taking around 70 secs to do so because of the huge data.

I've also thought of creating a new datarow, assigning values to each cell,adding the resulting datarow to datatable and finally calling dataadapter.update(...).But it seems to be complex because I got around 50 columns and hence I'll have to assign 50 values to the datarow.

Could someone please suggest if there is an alternate to improve the performance of the insertion?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裸钻 2024-09-10 16:06:46

MySQL LOAD DATA INFILE 类似于 MS SQL 的 BULK INSERT 这将为您带来最佳性能。

您可以在循环遍历 Excel 单元格时连接文本文件,但为了提高性能,请尝试一次性从 Excel 导出文件 - 我不确定您需要如何巧妙地处理导出文件(分隔符等)

编辑:

或者,如果您没有胃口/时间尝试这样的事情,至少将尽可能多的 VALUES 行传递到单个 INSERT 语句中:

INSERT INTO tbl_name ( a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9); (也来自 MySQL 站点。)

我不知道是否 5000高于理论极限——可能吗? -- 因此,只需为每个语句组装大约 100 个插入,然后执行它。

无论如何,每个命令一次插入的方法会让您付出代价。

MySQL LOAD DATA INFILE is akin to MS SQL's BULK INSERT and that is going to give you the best performance.

You can concatenate your text file while looping through the Excel cells, but for more performance gain, try exporting the file from Excel in one shot - I'm not sure how you'd need to finesse the export file (delimiters, etc.)

EDIT:

Or, if you don't have the stomach/time to try something like this, at least pass as many VALUES rows into a single INSERT statement as you can:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9); (also from the MySQL site.)

I don't know whether 5000 is above the theoretical limit -- probably? -- so just assemble maybe 100 inserts per statement and then execute it.

At any rate, the single-insert-per-command approach is what's costing you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文