【问题标题】:Increasing the query Performance on DataTable in C#在 C# 中提高 DataTable 的查询性能
【发布时间】:2016-08-31 11:03:31
【问题描述】:

代码正在尝试从查找表中查找值。为了提高代码的性能,我决定给查找函数一个排序表而不是原始表。正如我听说创建数据视图有助于提高选择的性能,但我不相信实现,因为我再次将数据视图转换为表。所以我不明白它是如何帮助的。性能几乎没有差异,但对于较大的数据库,它可能会有所不同。如果我做对或错或如何提高性能,有人可以向我提供建议。

创建数据视图

        List<DataView> dvTablesLookup = null;
        List<DataTable> dtTablesLookup = null;


        dvTablesLookup = datatablesLookup.Select(dt => new DataView(dt)).ToList();
        dvTablesLookup.ForEach(x => x.Sort = sortLookup);
        dtTablesLookup = dvTablesLookup.Select(dv => dv.ToTable()).ToList();

调用函数

result.AddRange(this.GetValueFromLookup(filter, lookupValueField.StringValue, dtTablesLookup[i]));

查找功能。该函数将查找表作为参数。

private object[] GetValueFromLookup(MultipleKeyConditionBuilder filter, string lookupValueField, DataTable datatableLookup)
    {
        object[] result;
        DataRow[] rows = datatableLookup.Select(filter.Condition);
        result = new object[rows.Count()];
        for (int i = 0; i < rows.Count(); i++)
        { 
            result[i] = rows[0][lookupValueField];
        }

        return result;
    }

【问题讨论】:

    标签: c# performance datatable ado.net dataview


    【解决方案1】:

    使用字典我意识到我的数据中有很多重复,并且重复执行相同的查询给了我相同的结果。所以我决定使用字典,它从几分钟到几秒钟显着提高了性能。

    使用视图:视图的好处完全取决于数据和查询条件。由于查询的目的是查找,因此视图根本没有让我受益,相反,对于更大的记录,它需要更多的时间。

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 2013-08-03
      • 2016-08-18
      • 1970-01-01
      • 2021-08-03
      • 2013-02-17
      • 2021-12-15
      • 2014-04-09
      相关资源
      最近更新 更多