【发布时间】:2017-03-02 20:50:39
【问题描述】:
我指的是an article,它专注于加速 LINQ to SQL 查询。它提到的技术之一是“使用编译查询”并解释了如何使用它。
我想看到编译查询的性能改进,因此我尝试了作者提供的相同示例。我使用 Northwind Db 作为数据上下文。我尝试了正常执行和编译查询执行,并在 LINQ PAD 上检查了它们。
首先我尝试不使用使用 CompileQuery 执行查询。耗时 2.065 秒。
var oo = from o in Orders
where o.OrderDetails.Any (p => p.UnitPrice > 100)
select o;
oo.Dump ("Order items with unit price more than $100");
var oo1 = from o in Orders
where o.OrderDetails.Any (p => p.UnitPrice > 10)
select o;
oo1.Dump ("Order items with unit price more than $10");
其次,查询with 使用CompileQuery。耗时 2.100 秒。
var oo = CompiledQuery.Compile ((TypedDataContext dc, decimal unitPrice) =>
from o in Orders
where o.OrderDetails.Any (p => p.UnitPrice > unitPrice)
select o
);
oo (this, 100).Dump ("Order items with unit price more than $100");
oo (this, 10).Dump ("Order items with unit price more than $10");
多次重新执行它们表明两种方法所花费的时间几乎相似。
这里我们只看到每个方法的两个查询执行。我尝试为每个查询进行 10 个查询。但他们都完成了大约 7 秒。
预编译查询真的能提高性能吗?还是我弄错了它的使用条款?
感谢您的时间和考虑。
编辑: 阅读接受的答案后,读者可能还想查看this article,它很好地解释了编译查询如何提高性能。
【问题讨论】:
标签: c# performance linq-to-sql