【问题标题】:Is SQL View faster than Table while using Linq?使用 Linq 时 SQL View 是否比 Table 快?
【发布时间】:2012-10-26 15:27:48
【问题描述】:

我的 WPF 应用程序有一个用于选择客户的查找屏幕。客户表包含近 10,000 条记录。使用我的 Linq 查询加载和过滤记录时它非常慢(我没有对记录进行任何排序)。有没有办法提高速度?听说过使用索引视图。有人可以提供一些想法吗?

lstCustomerData = dbContext.customers.Where(c => c.Status == "Activated").ToList();
            dgCustomers.ItemsSource = lstCustomerData;

过滤:

 string searchKey = TxtCustName.Text.Trim();

            var list = (from c in lstCustomerData
                        where (c.LastName == null ? "" : c.LastName.ToUpper()).Contains(searchKey.ToUpper())
                        select c).ToList();
            if (list != null)
                dgCustomers.ItemsSource = list;

【问题讨论】:

标签: c# sql-server wpf linq view


【解决方案1】:

取决于什么是慢的。 SQL 查询慢吗? UI渲染慢吗?您是在内存中排序/筛选还是返回数据库?

您应该分析您的应用以准确找出最慢的部分,然后首先解决它。

如果您添加的 Linq 查询速度较慢,那么向数据库中的 Status 列添加索引可能会有所帮助。

您可能会通过更改 Where 子句获得一些改进:

var list = (from c in lstCustomerData
            where (c.LastName != null && c.LastName.ToUpper()).Contains(searchKey.ToUpper())
            select c).ToList();
if (list != null)
    dgCustomers.ItemsSource = list; 

因为它不必比较空字符串。但是,如果您的 NULL 记录很少,那么这可能无济于事。

但是,在这种情况下,所有过滤都在内存中完成,因此在数据库中使用索引视图将无济于事,除非您将过滤推送回源存储库。

【讨论】:

  • 我正在更新用户界面。我在我的问题中添加了过滤代码
猜你喜欢
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
相关资源
最近更新 更多