C# 中大型数据集的最佳实践

发布于 2025-01-08 08:32:10 字数 977 浏览 0 评论 0原文

目前,我正在设计和实现一个软件,该软件必须通过主从架构对两个表实现 CRUD 操作。标题大约有 50 万行,详细信息大约有 100 万行。

将所有这些数据填充到数据集中是疯狂的,数据也可以更改,而且我对拥有数据库的本地副本不感兴趣。 我对该软件运行流畅感兴趣。尽管数据集可能不是最好的解决方案,但我应该使用它来与其他软件部分保持一致。

首先,我认为使用 TypedDataset 和一些方法,如 GetNext() 、 GetFirst() 、 GetByCod() 但我不确定是否是最好的解决方案...... 我正在做一个小测试,但工作得不太流畅。

我有兴趣了解其他开发人员如何做到这一点、最佳实践以及使用大数据进行操作的“最佳选择”是什么。

我正在使用 Visual Studio 2008 和 Sql Server 2005。

添加: 当您谈论使用 SqlDataReader 时,您指的是这样的东西?

using (SqlConnection con = new SqlConnection(CON)) {

            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM TABLE");
            cmd.Connection = con;
            SqlDataReader rd = cmd.ExecuteReader();
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = rd;
            bindingNavigator1.BindingSource = bindingSource;

            txtFCOD.DataBindings.Add("Text", bindingSource, "FIELD");
        }

Currently I'm working in desing and implement a software who has to implement CRUD operations over two tables with master-detail arquitecture. Header has about half million of rows and detail about million of rows.

Fill all this data in a dataset is crazy, also data can change and I'm not interested in have a local copy of database.
I'm interested in that software works fluently. Although dataset may be not the best solution, I should use this to be consistent with other software parts.

First I think to use a TypedDataset and some methos like GetNext() , GetFirst() , GetByCod() but I'm not sure if is the best solution....
I'm doing a little test and don't work very fluently.

I'm interested in know how other developers do this , Best practices and what's "the best choice" to do operations with large data.

I'm using Visual Studio 2008 and Sql Server 2005.

ADDED:
When you talk about of using SqlDataReader you're referring something like this ?

using (SqlConnection con = new SqlConnection(CON)) {

            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM TABLE");
            cmd.Connection = con;
            SqlDataReader rd = cmd.ExecuteReader();
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = rd;
            bindingNavigator1.BindingSource = bindingSource;

            txtFCOD.DataBindings.Add("Text", bindingSource, "FIELD");
        }

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

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

发布评论

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

评论(2

梦忆晨望 2025-01-15 08:32:10

我认为没有办法管理这么大的数据集。

您需要 DataReader,而不是 DataSet。

具有大量数据的数据库的本地副本是实现此类目标的有效方法(应用程序的快速响应),但是您会遇到同步(复制)、并发等问题。

最佳实践是仅从服务器获取该数据哪个用户真正需要。您必须使用服务器端处理,通过存储过程等。

我仍然不知道您想要操作哪些数据以及您的应用程序的目的是什么,但是大量数据还有另一个缺点在客户端 - 您的应用程序将需要大量内存和快速 CPU。也许您的计算机速度很快并且能够处理此问题,但请考虑一下当有人在具有 1GHz Atom CPU 的平板电脑上安装您的应用程序时会发生什么。那将是一场灾难。

I think there is no way to manage so big dataset.

You need DataReader, not DataSet.

Local copy of database with really big amount of data is effective way to reach something like this (fast response from your app), but you will have problems with synchronization (replication), concurrency etc..

Best practice is getting from server only that data which user really need. You have to use server-side processing, by stored procedures etc.

I still dont know what data you want to manipulate and what is the purpose of your app, but there is another disadvantage of big amounts of data on client side - your app will need a lot of ram and fast CPU. Maybe your computer is fast and capable of handling this, but consider what happens when somebody install your app on tablet with 1GHz Atom CPU. That will be disaster.

嘴硬脾气大 2025-01-15 08:32:10

很少有需要立即检索所有数据的情况。

您可以考虑以下措施:

  • 使用视图来提供特定的较小数据集。
  • 考虑使用 SQL Server 2005 中引入的 OVER() 功能进行分页
  • 不要对大量数据使用数据集。数据读取器很多
    在这种情况下效率更高。

我个人认为您应该避免将大量数据加载到内存中,除非您可以完全控制加载的数据量以及何时处理它。请记住,如果在服务器端处理数据,您正在使用其他进程可能需要的资源。

您应该始终尝试一次处理较小的块,并且最好是尽可能短的时间。这可以防止您的进程长时间占用任何资源。

There should rarely be a scenario where you need to retrieve all the data at once.

You could consider the following:

  • Use Views for serving specific smaller sets of data.
  • Consider using paging using the OVER() functionality introduced in SQL Server 2005
  • Do not use datasets for large amounts of data. DataReaders are much
    more efficient in this case.

Personally I think you should avoid loading large amounts of data into memory unless you have complete control over how much is being loaded and when it is being disposed. Remember, if handling data server-side, you are using the resources that other process may need.

You should always try and work with smaller chunks at a time, and preferably for as short a time as possible. This prevents your process from hogging any resources for long periods of time.

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