在C#数据表中创建大数据性能非常慢。 [英] Creating large data in C# data table perfomance very slow.

查看:152
本文介绍了在C#数据表中创建大数据性能非常慢。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,



在我的Asp.Net c#应用程序中



我们正在创建数据表10K记录和120列,完成数据表的时间超过2小时。



以下是我正在使用的代码请帮帮我

Dear Friend,

In my Asp.Net c# application

we are creating datatable with 10K records and 120 columns, it is tacking more than 2 hours to complete the datatable.

THe following is the code i am using please help me out

foreach (DataColumn dc in policyLevelTable.Columns)
{
    for (int i = 1; i <= 10000; i++)
    {
        if (dc.ColumnName.ToLower() == "policyid")
        {
            DataRow newdr = policyLevelTable.NewRow();
            newdr[0] = i;
            // newdr[1] = 1;
            policyLevelTable.Rows.Add(newdr);
        }
        else
        {
            DataRow[] dr = policyLevelTable.Select("policyid in (" + i + ")");
            dr[0][dc.ColumnName] = RandomParameterType(new List<int>{1,2,3,4}, 0, false);
        }
    }
}





谢谢,

Ramu。



我的尝试:



foreach(policyLevelTable中的DataColumn dc) .Columns)

{

for(int i = 1; i< = 10000; i ++)

{

if(dc.ColumnName.ToLower()==policyid)

{

DataRow newdr = policyLevelTable.NewRow();

newdr [0] = i;

// newdr [1] = 1;

policyLevelTable.Rows.Add(newdr);



}

其他

{

DataRow [] dr = policyLevelTable.Select(policyid in(+我+));



dr [0] [dc.ColumnName] = RandomParameterType(new List< int> {1,2,3,4},0,false);





}

}

}



Thanks,
Ramu.

What I have tried:

foreach (DataColumn dc in policyLevelTable.Columns)
{
for (int i = 1; i <= 10000; i++)
{
if (dc.ColumnName.ToLower() == "policyid")
{
DataRow newdr = policyLevelTable.NewRow();
newdr[0] = i;
// newdr[1] = 1;
policyLevelTable.Rows.Add(newdr);

}
else
{
DataRow[] dr = policyLevelTable.Select("policyid in (" + i + ")");

dr[0][dc.ColumnName] = RandomParameterType(new List<int> { 1, 2, 3, 4 }, 0, false);


}
}
}

推荐答案

反之亦然:循环遍历行作为外部循环,然后填入其中的每一列。这样,您不需要(可能很耗时)。为每行中的每个单元格选择,您可以完全省略它,因为您将始终在同一行上工作。

此外,除非您每次都需要一个新的List,否则在所有循环外创建一个。

然后检查RenadomParamaterType并查看它正在做什么,以及它花了多少时间。
Do it the other way round: loop through the rows as the outer loop, then fill in each column inside it. That way, you don't need to do the (probably time consuming) .Select for each cell in each row, you can omit it entirely, since you will always be working on the same row.
In addition, unless you need a new List each time, create one outside all loops.
Then check RenadomParamaterType and see what it's doing, and how much of the time it's taking.


创建10k行永远不会很快。用户对10k行的期望是什么?如果他们每行花费3秒钟,则需要8个小时来处理所有行。显示大量数据是无用的,实现分页和搜索,以便您只显示减少的数据量,或仅显示相关的数据。
Creating 10k rows is never going to be quick. What do you expect the user to do with 10k rows anyway? If they spend 3 seconds per row it would take them 8 hours to process all of the rows. Showing that much data is useless, implement paging and searching so that you only show a reduced amount of data, or only the data that is relevant.


我不知道这是多长时间要采取,但代码较少,所以银色衬里...



I don't know how long this is gonna take, but there's less code, so silver linings...

Random random = new Random();
for (int i = 1; i &lt;= 10000; i++)
{
    DataRow newdr = policyLevelTable.NewRow();
    for (int j = 0; j < policyLevelTable.Columns.Count; i++)
    {
        newdr[j] = (j==0) ? j : random.Next(5);
    }
}





警告 - 这将消耗相当多的内存。如果在网站上完成此操作,您可能会遇到比简单速度问题更多的问题。



Caveat - this is going to consume quite a bit of memory. If this is done on a website, you will likely have more problems than simple speed issues.


这篇关于在C#数据表中创建大数据性能非常慢。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆