提高c#中数据表的查询性能

本文关键字:查询 性能 数据表 提高 | 更新日期: 2023-09-27 18:04:49

代码试图从查找表中查找值。为了提高代码的性能,我决定给查找函数一个排序表,而不是原来的表。我听说创建数据视图有助于提高select的性能,但我不相信实现,因为我再次将数据视图转换为表。所以我不明白这有什么用。在性能上几乎没有区别,但是对于较大的数据库可能会有所不同。有人能给我一个建议,如果我这样做是对还是错,或者如何提高性能。

创建数据视图

        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#中数据表的查询性能

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

使用Views: View的好处完全取决于数据和查询条件。由于查询的目的是查找,视图根本没有给我带来任何好处,相反,对于更大的记录,它会花费更多的时间。