DataTable.Select和C#性能问题 [英] DataTable.Select and Performance Issue in C#

查看:765
本文介绍了DataTable.Select和C#性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进口从三个制表符分隔文件中的数据在数据表之后,我需要去掌握直通表的每一行,并找到两个子表中的所有行。对每一个的DataRow []数组我从子表发现,我不得不再次去直通每个单独的行和检查根据不同paramenters的值,并在年底我需要创建,这将是主人和两个小孩的合并最终记录表列。
现在我已经做到这一点,它的工作,但问题是它的性能。我使用DataTable.Select找到从子表我认为拍得很慢所有的子行。
请记住表的谁都没有主键重复的行是可以接受的。
目前,我在主表1200行,也不要蜘蛛在子表8000行,它需要做的是8分钟,总时间。



任何知道如何才能提高性能。提前
谢谢



该代码如下 * 的**的 * 的***

 的DataTable rawMasterdt = importMasterFile(); 
的DataTable rawDespdt = importDescriptionFile();

dsHelper =新DataSetHelper的();
的DataTable distinctdt =新的DataTable();
distinctdt = dsHelper.SelectDistinct(DistinctOffers,rawMasterdt,C1);

如果(distinctdt.Rows.Count大于0)
{
诠释计数= 0;
的foreach(DataRow的报价在distinctdt.Rows)
{
串EXP =C1 =+'+报价[0]的ToString()+'+;
的DataRow masterRow = rawMasterdt.Select(EXP)[0];

计数++;
txtBlock1.Text =导入发售+ count.ToString()+ + distinctdt.Rows.Count.ToString的();
如果(masterRow!= NULL)
{
产品展示新品推荐=新产品();

newProduct.Code = masterRow [C4]的ToString()。
newProduct.Name = masterRow [C5]的ToString()。
// -----
newProduct.Description = getProductDescription(报价[0]的ToString(),rawDespdt);
newProduct.Weight = getProductWeight(报价[0]的ToString(),rawDespdt);
newProduct.Price = getProductRetailPrice(报价[0]的ToString(),rawDespdt);
newProduct.UnitPrice = getProductUnitPrice(报价[0]的ToString(),rawDespdt);
// -------类似于上面更多的功能在这里

productList.Add(新品展示);
}
}
txtBlock1.Text =导入已完成;
公共字符串getProductDescription(串offercode,数据表DSP)
{
串EXP =((C =+'+ offercode +')+和(C6 =' C' ));
的DataRow [] = dRows dsp.Select(EXP);
字符串记述=;
如果(dRows.Length大于0)
{
的for(int i = 0; I< dRows.Length - 1;我++)
{
记述=记述++ dRows [I] [12];
}
}
返回记述;

}


解决方案

可以通过使用字典加快它了很多。例如:

 如果(distinctdt.Rows.Count大于0)
{
//构建C1值的索引来加速内部循环
&字典LT;字符串的DataRow>万事达卡=新词典<字符串的DataRow>();
的foreach(DataRow的行rawMasterdt.Rows)
万事达卡=行[行[C1]的ToString()];

诠释计数= 0;
的foreach(DataRow的报价在distinctdt.Rows)
{

然后到位的

 字符串EXP =C1 =+'+报价[0]的ToString()+'+ ; 
的DataRow masterRow = rawMasterdt.Select(EXP)[0];

您会做这个

 的DataRow masterRow; 
如果(masterIndex.ContainsKey(报价[0]的ToString())
masterRow =万事达卡[报价[0]的ToString();
,否则
masterRow = NULL;


I'm importing the data from three Tab delimited files in the DataTables and after that I need to go thru every row of master table and find all the rows in two child tables. Against each DataRow[] array I found from the child tables, I have to again go thru individually each row and check the values based upon different paramenters and at the end I need to create a final record which will be merger of master and two child table columns. Now I have done that and its working but the problem is its Performance. I'm using the DataTable.Select to find all child rows from child table which I believe making it very slow. Please remember the None of the table has any Primary key as the duplicate rows are acceptable. At the moment I have 1200 rows in master table and aroun 8000 rows in child table and the total time it takes to do that is 8 minutes.

Any idea how can I increase the Performance. Thanks in advance

The code is below *******

 DataTable rawMasterdt = importMasterFile();
 DataTable rawDespdt = importDescriptionFile();

        dsHelper = new DataSetHelper();
        DataTable distinctdt = new DataTable();
        distinctdt = dsHelper.SelectDistinct("DistinctOffers", rawMasterdt, "C1");

        if (distinctdt.Rows.Count > 0)
        {
            int count = 0;
                foreach (DataRow offer in distinctdt.Rows)
                {
                    string exp = "C1 = " + "'" + offer[0].ToString() + "'" + "";
                    DataRow masterRow = rawMasterdt.Select(exp)[0];

                    count++;
                    txtBlock1.Text = "Importing Offer " + count.ToString() + " of " + distinctdt.Rows.Count.ToString(); 
                    if (masterRow != null )
                        {
                            Product newProduct = new Product();

                            newProduct.Code = masterRow["C4"].ToString();
                            newProduct.Name = masterRow["C5"].ToString();
                          //  -----
                            newProduct.Description = getProductDescription(offer[0].ToString(), rawDespdt);
                            newProduct.Weight = getProductWeight(offer[0].ToString(), rawDespdt);
                            newProduct.Price = getProductRetailPrice(offer[0].ToString(), rawDespdt);
                            newProduct.UnitPrice = getProductUnitPrice(offer[0].ToString(), rawDespdt);
                          //  ------- more functions similar to above here

                            productList.Add(newProduct);
                        }
                }
                txtBlock1.Text = "Import Completed";
 public string getProductDescription(string offercode, DataTable dsp)
    {
        string exp = "((C1 = " + "'" + offercode + "')" + " AND ( C6 = 'c' ))";
        DataRow[] dRows = dsp.Select( exp);
        string descrip = "";
        if (dRows.Length > 0)
        { 
            for (int i = 0; i < dRows.Length - 1; i++)
            {
              descrip = descrip + " " + dRows[i]["C12"];
            }
        }
        return descrip;

    }

解决方案

You can speed it up a lot by using a dictionary. For example:

if (distinctdt.Rows.Count > 0)
{
    // build index of C1 values to speed inner loop
    Dictionary<string, DataRow> masterIndex = new Dictionary<string, DataRow>();
    foreach (DataRow row in rawMasterdt.Rows)
        masterIndex[row["C1"].ToString()] = row;

    int count = 0;
    foreach (DataRow offer in distinctdt.Rows)
    {

Then in place of

    string exp = "C1 = " + "'" + offer[0].ToString() + "'" + "";
    DataRow masterRow = rawMasterdt.Select(exp)[0];

You would do this

DataRow masterRow;
if (masterIndex.ContainsKey(offer[0].ToString())
    masterRow = masterIndex[offer[0].ToString()];
else
    masterRow = null;

这篇关于DataTable.Select和C#性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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