自动编译 Linq 查询
我们发现 编译我们的 Linq 查询 比每次都必须编译要快得多,因此我们希望开始使用编译查询。 问题是它使代码更难阅读,因为查询的实际语法在其他文件中是关闭的,远离它的使用位置。
我突然想到,也许可以编写一个方法(或扩展方法),使用反射来确定传入的查询并自动缓存已编译的版本以供将来使用。
var foo = (from f in db.Foo where f.ix == bar select f).Cached();
Cached()
必须反映传入的查询对象并确定所选的表以及查询的参数类型。 显然,反射有点慢,因此最好使用缓存对象的名称(但您仍然必须在第一次编译查询时使用反射)。
var foo = (from f in db.Foo where f.ix == bar select f).Cached("Foo.ix");
有谁有这样做的经验,或者知道这是否可能?
更新:对于那些没有看过它的人,您可以使用以下代码将 LINQ 查询编译为 SQL:
public static class MyCompiledQueries
{
public static Func<DataContext, int, IQueryable<Foo>> getFoo =
CompiledQuery.Compile(
(DataContext db, int ixFoo) => (from f in db.Foo
where f.ix == ixFoo
select f)
);
}
我想做的是缓存这些Func<>
对象,我可以在第一次自动编译查询后调用这些对象。
We've found that compiling our Linq queries is much, much faster than them having to compile each time, so we would like to start using compiled queries. The problem is that it makes code harder to read, because the actual syntax of the query is off in some other file, away from where it's being used.
It occurred to me that it might be possible to write a method (or extension method) that uses reflection to determine what queries are being passed in and cache the compiled versions automatically for use in the future.
var foo = (from f in db.Foo where f.ix == bar select f).Cached();
Cached()
would have to reflect the query object passed in and determine the table(s) selected on and the parameter types for the query. Obviously, reflection is a bit slow, so it might be better to use names for the cache object (but you'd still have to use reflection the first time to compile the query).
var foo = (from f in db.Foo where f.ix == bar select f).Cached("Foo.ix");
Does anyone have any experience with doing this, or know if it's even possible?
UPDATE: For those who have not seen it, you can compile LINQ queries to SQL with the following code:
public static class MyCompiledQueries
{
public static Func<DataContext, int, IQueryable<Foo>> getFoo =
CompiledQuery.Compile(
(DataContext db, int ixFoo) => (from f in db.Foo
where f.ix == ixFoo
select f)
);
}
What I am trying to do is have a cache of these Func<>
objects that I can call into after automatically compiling the query the first time around.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于未来的后代:.NET Framework 4.5 将默认执行此操作(根据我刚刚观看的演示文稿中的幻灯片)。
For future posterity : .NET Framework 4.5 will do this by default (according to a slide in a presentation I just watched).
既然没人尝试,那我就试试吧。 也许我们都能以某种方式解决这个问题。 这是我的尝试。
我使用字典进行了设置,我也没有使用 DataContext,尽管我认为这是微不足道的。
现在这使我能够这样做,
期待对此进行一些讨论,以进一步发展这个想法。
Since nobody is attempting, I'll give it a shot. Maybe we can both work this out somehow. Here is my attempt at this.
I set this up using a dictionary, I am also not using DataContext although this is trivial i believe.
now this allows me to do this
looking forward to some discussion about this, to further develop this idea.
我不得不处理保存一个&gt; 使用 LinqToSql 开发的 15y/o 项目,CPU 消耗太大。
基准测试表明,使用编译查询对于复杂查询来说速度要快 7 倍,对于简单查询速度要快 2 倍(考虑到运行查询本身可以忽略不计,这里仅涉及编译查询的吞吐量)。
缓存不是由 .Net Framework 自动完成的(无论什么版本),这只发生在实体框架而不是 LINQ-TO-SQL 上,并且这些是不同的技术。
编译查询的使用很棘手,因此这里有两个重要的亮点:
考虑到这一点,我想出了这个缓存类。 使用其他评论中提出的静态方法有一些可维护性缺点 - 主要是可读性较差 - 而且迁移现有的庞大代码库更困难。
在非常紧密的循环中,使用被调用方的缓存键而不是查询本身可以提高 10% 的性能:
下面是代码。 出于安全考虑,缓存会阻止编码器在已编译的查询中返回 IQueryable。
I had to deal with saving a > 15y/o project that was developed using LinqToSql and was too CPU hungry.
Benchmarking showed that using compiled query is x7 faster for complex queries, and x2 for simple queries (considering that the running the query itself is negligible, here it's just about the throughput of compiling the query).
Caching is NOT done automatically by .Net Framework (no matter what version), this only happens for Entity Framework NOT for LINQ-TO-SQL, and these are different technologies.
Usage of compiled queries is tricky, so here are two important highlights:
Considering that, I came up with this cache class. Using the static approach as proposed in other comments has some maintainability drawbacks - it's mainly less readable -, plus it is harder to migrate an existing huge codebase.
On very tight loops, using a cache key from the callee instead of the query itself yielded +10% better performance:
And here is the code. The cache prevents the coder from returning an IQueryable in a compiled query, just for safety.
您无法在匿名 lambda 表达式上调用扩展方法,因此您需要使用 Cache 类。 为了正确缓存查询,您还需要将所有参数(包括 DataContext)“提升”为 lambda 表达式的参数。 这会导致非常冗长的用法,例如:
为了清理它,如果我们使其成为非静态的,我们可以在每个上下文的基础上实例化 QueryCache:
然后我们可以编写一个 Cache 方法,该方法将使我们能够编写以下内容:
查询中的任何参数也需要被提升:
这是我模拟的 QueryCache 实现:
这可以扩展以支持更多参数。 最棒的是,通过将参数值传递到 Cache 方法本身,您可以获得 lambda 表达式的隐式类型。
编辑:请注意,您不能将新运算符应用于已编译的查询。具体来说,您不能执行以下操作:
因此,如果您计划对查询进行分页,则需要在编译操作中执行此操作,而不是稍后执行。 这不仅是为了避免异常,而且也是为了与 Skip/Take 的整体要点保持一致(以避免从数据库返回所有行)。 这种模式可以工作:
另一种分页方法是返回一个 Func:
这种模式的用法如下:
You can't have extension methods invoked on anonymous lambda expressions, so you'll want to use a Cache class. In order to properly cache a query you'll also need to 'lift' any parameters (including your DataContext) into parameters for your lambda expression. This results in very verbose usage like:
In order to clean that up, we can instantiate a QueryCache on a per-context basis if we make it non-static:
Then we can write a Cache method that will enable us to write the following:
Any arguments in your query will also need to be lifted:
Here's the QueryCache implementation I mocked up:
This can be extended to support more arguments. The great bit is that by passing the parameter values into the Cache method itself, you get implicit typing for the lambda expression.
EDIT: Note that you cannot apply new operators to the compiled queries.. Specifically you cannot do something like this:
So if you plan on paging a query, you need to do it in the compile operation instead of doing it later. This is necessary not only to avoid an exception, but also in keeping with the whole point of Skip/Take (to avoid returning all rows from the database). This pattern would work:
Another approach to paging would be to return a
Func
:This pattern is used like: