wpf 如何优化 linq 查询的速度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13858731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to optimize linq query for speed?
提问by sony
I have a linq query which fetches all records from the customer table to an observable collection as below:
我有一个 linq 查询,它将客户表中的所有记录提取到一个可观察的集合中,如下所示:
customerList = new ObservableCollection<customer>(dbContext.customers);
dgRecords1.ItemsSource = customerList;
the list is bound to a datagrid. The customer table contains many fields nearly hundred. But I display only a few fields on the datagrid. My question is
该列表绑定到一个数据网格。客户表包含近百个字段。但是我在数据网格上只显示了几个字段。我的问题是
whether bringing of only selected fields from the database using linq query increase the speed the customer screen?
使用 linq 查询仅从数据库中提取选定的字段是否会提高客户筛选的速度?
I need to filter and sometimes delete records from this list.
我需要过滤并有时从该列表中删除记录。
Which is the best way to select few fields into observable collection, Can someone give some sample linq queries?
哪个是将几个字段选择到可观察集合中的最佳方法,有人可以提供一些示例 linq 查询吗?
回答by Marc Gravell
Tips to optimize speed:
优化速度的提示:
- reducing columns reduces bandwidth required
- reducing rows, but introducing paging, reduced bandwidth by much more(usually)
- turning off change-tracking and identity-management (for example
ObjectTrackingEnabledin LINQ-to-SQL) will reduce overheads post-processing the data - using a pre-compiled query can sometimes help reduce pre-processing overheads
- 减少列减少了所需的带宽
- 减少行数,但引入分页,减少了更多的带宽(通常)
- 关闭更改跟踪和身份管理(例如
ObjectTrackingEnabled在 LINQ-to-SQL 中)将减少数据后处理的开销 - 使用预编译查询有时有助于减少预处理开销
... but frankly, when we had this problem, we solved it "once and for all" by writing "dapper", and going old-school:
……但坦率地说,当我们遇到这个问题时,我们通过编写“dapper”来“一劳永逸”地解决了它,然后走老路:
var list = connection.Query<CustomerViewModel>(
"select {some specific cols} from Customers").ToList();
where CustomerViewModelis a simple POCO type not related to LINQ etc that just has the required values, for example:
哪里CustomerViewModel是一个与 LINQ 等无关的简单 POCO 类型,它只具有所需的值,例如:
class CustomerViewModel {
public int Id {get;set;}
public string Name {get;set;}
// ...
}
This cuts out allunnecessary overheads, and is ideal when you just want to display data; additionally, the parameterization and materialization layers are very optimised (with strategy-caching, for optimum performance).
这减少了所有不必要的开销,当您只想显示数据时是理想的选择;此外,参数化和物化层非常优化(使用策略缓存,以获得最佳性能)。
回答by Habib
If you are only selecting required columns instead of all columns, you will have the improvement in the performance. You may use StopWatchto actually calculate the difference. It is always better to only select the required columns from the database, not all of them.
如果您只选择所需的列而不是所有列,则性能会有所提高。您可以StopWatch用来实际计算差异。最好只从数据库中选择所需的列,而不是全部。
One more thing that you may consider is implementing pagination using Skipand Take. You may see: LINQ TO SQL GridView (Enhanced Gridview)
您可以考虑的另一件事是使用Skip和实现分页Take。你可能会看到:LINQ TO SQL GridView (Enhanced Gridview)

