MySqlDataAdapter 或 MySqlDataReader 用于批量传输?

发布于 2024-08-31 11:11:05 字数 324 浏览 1 评论 0原文

我正在使用 .NET 的 MySql 连接器将数据从 MySql 服务器复制到 SQL Server 2008。

是否有人在使用以下其中一种方法时体验到比另一种方法更好的性能?

  • 调用 Fill to a DataTable,
  • DataAdapter 并以 500 个DataReader.Read 为一个循环

然后我使用 SqlBulkCopy 加载 500 个 DataTable 行,然后继续循环,直到 MySql 记录集完全传输。

我主要关心的是使用合理的内存量并在短时间内完成。

任何帮助将不胜感激!

I'm using the MySql connector for .NET to copy data from MySql servers to SQL Server 2008.

Has anyone experienced better performance using one of the following, versus the other?

  • DataAdapter and calling Fill to a DataTable in chunks of 500
  • DataReader.Read to a DataTable in a loop of 500

I am then using SqlBulkCopy to load the 500 DataTable rows, then continue looping until the MySql record set is completely transferred.

I am primarily concerned with using a reasonable amount of memory and completing in a short amount of time.

Any help would be appreciated!

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

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

发布评论

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

评论(2

潜移默化 2024-09-07 11:11:05

我没有使用过 SqlBulkCopy,但根据一般经验,DataReaders 通常会提供更好的性能。

可以在基础查询仍在返回记录时处理 DataReader(因此您不必等待查询完成才能开始处理数据)。 DataReader 将在数据可用时立即返回数据,并且我相信默认情况下只会将活动记录存储在内存中(而不是完整的结果集),从而减少内存使用。

DataAdapter 将完整的结果集加载到 DataTable/DataSet 中,由于信息在内存中的存储方式以及附加的关联状态(例如行状态等),该数据集将具有更高的开销。

如果我只读取数据,我将始终使用 DataReader 而不是 DataAdapter...如果我在任何一点上错了,请有人纠正我?

无论如何,SqlBulkCopy 似乎只迭代记录,并且不使用 DataTable 进行任何优化(根据 Reflector),所以我认为 DataReader 是你最好的选择。

I have not used SqlBulkCopy, but as a general rule of thumb, DataReaders typically offer better performance.

A DataReader can be processed while the underlying query is still returning records (so you don't have to wait for the query to finish before you can start processing data). A DataReader will return data as soon as it is available and I believe will only store the active record in memory by default (not the complete result set), thus reducing memory usage.

A DataAdapter loads the full result set in to a DataTable/DataSet that will have higher overhead due to how the information is stored in memory and the additional associated state (think rowstate, etc).

If I am only reading data, I will always use a DataReader over a DataAdapter... someone please correct me if I am wrong on any point?

Regardless, SqlBulkCopy appears to only iterate over the records and does not use the DataTable for any optimizations (according to Reflector), so I think DataReader is your best bet.

灵芸 2024-09-07 11:11:05

在处理大量数据时,我将 SqlBulkCopy 与 DataReader 结合使用。我发现该过程在速度和内存使用方面非常有效,因为在复制之前不会检索整个数据集。我建议将 BatchSize 属性设置为某个合理的值,例如您的情况为 500。

I've used SqlBulkCopy with DataReader when processing large amounts of data. I’ve found the process to be quite efficient in terms of speed and memory usage, since the entire data set is not retrieved before copying. I recommend setting the BatchSize property to some reasonable value, say 500 in your case.

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