什么时候应该使用CompiledQuery? [英] When should I use a CompiledQuery?

查看:267
本文介绍了什么时候应该使用CompiledQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表:

-- Tag

ID  | Name
-----------
1   | c#
2   | linq
3   | entity-framework

我有一个类,将有以下几种方法:

I have a class that will have the following methods:

IEnumerable<Tag> GetAll();
IEnumerable<Tag> GetByName();

我应该使用编译的查询在这种情况下?

Should I use a compiled query in this case?

static readonly Func<Entities, IEnumerable<Tag>> AllTags =
    CompiledQuery.Compile<Entities, IEnumerable<Tag>>
    (
        e => e.Tags
    );

然后我的 GetByName方法的方法是:

IEnumerable<Tag> GetByName(string name)
{
    using (var db = new Entities())
    {
        return AllTags(db).Where(t => t.Name.Contains(name)).ToList();
    }
}

生成一个 SELECT ID,名称FROM标签并执行其中,在code。或者我应该避免 CompiledQuery 在这种情况下?

Which generates a SELECT ID, Name FROM Tag and execute Where on the code. Or should I avoid CompiledQuery in this case?

基本上我想知道我什么时候应该使用编译查询。此外,在网站上他们为整个应用程序编译只有一次?

Basically I want to know when I should use compiled queries. Also, on a website they are compiled only once for the entire application?

推荐答案

您应该使用 CompiledQuery 当所有以下为真:

You should use a CompiledQuery when all of the following are true:


  • 查询将被执行一次以上,仅由参数值变化的。

  • 查询足够复杂,前pression的评价和看法的发电成本是显著(试行错误)

  • 您没有使用LINQ的功能如的IEnumerable&LT; T&GT;。载有(),不会与 CompiledQuery

  • 您已经简化了查询,这给了更大的性能优势,在可能的情况。

  • 您不打算进一步构成查询结果(例如,限制或项目),其中有反编译效果吧。

CompiledQuery 做的工作是执行查询的第一次。它给出了第一次执行没有任何好处。像任何性能调整,一般避免它,直到你确定你固定的实际性能热点。

CompiledQuery does its work the first time a query is executed. It gives no benefit for the first execution. Like any performance tuning, generally avoid it until you're sure you're fixing an actual performance hotspot.

2012更新: EF 5会自动完成(见<一个href=\"http://blogs.msdn.com/b/stuartleeks/archive/2012/06/12/entity-framework-5-controlling-automatic-query-compilation.aspx\">Entity框架5:控制自动查询编译)。所以,加上您没有使用EF 5上述列表中。

2012 Update: EF 5 will do this automatically (see "Entity Framework 5: Controlling automatic query compilation") . So add "You're not using EF 5" to the above list.

这篇关于什么时候应该使用CompiledQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆