简体   繁体   English

使用Linq时,SQL View是否比Table更快?

[英]Is SQL View faster than Table while using Linq?

My WPF application has a lookup screen for selecting customers. 我的WPF应用程序有一个用于选择客户的查找屏幕。 The customer table contain nearly 10,000 records. 客户表包含近10,000条记录。 Its very slow when loading and filtering records using my Linq query(I am not doing any ordering of records). 使用Linq查询加载和过滤记录时,它的速度非常慢(我没有对记录进行任何排序)。 Is there a way to increase speed? 有办法提高速度吗? Heard about using indexed views. 听说过使用索引视图。 Can someone please give some ideas? 有人可以给点子吗?

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

filtering: 过滤:

 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;

Depends on what is slow. 取决于什么是慢的。 is the SQL Query slow? SQL查询速度慢吗? Is the UI rendering slow? UI渲染慢吗? Are you sorting/fintering in memory or going back to the DB? 您是在对内存进行排序/存储还是返回数据库?

You should profile your app to find out exactly what the slowest piece is, then tackle that first. 您应该分析您的应用程序,以找出最慢的部分,然后首先解决该问题。

If the Linq query you added is what is slow then adding an index to the Status column in your database may help. 如果您添加的Linq查询比较慢,那么向数据库的“ Status列中添加索引可能会有所帮助。

You might get some improvement by changing your Where clause: 通过更改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; 

since it doesn't have to compare an empty string. 因为它不必比较空字符串。 However if you have very few NULL records than this probably won't help much. 但是,如果您的NULL记录很少,那么这可能没有多大帮助。

In this case, however, all of the filtering is done in memory so using an indexed view in the DB won't help unless you push the filtering back to the source repository. 但是,在这种情况下,所有筛选都在内存中完成,因此除非您将筛选推回源存储库,否则在数据库中使用索引视图将无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM