简体   繁体   English

编译查询和普通查询对我实体框架似乎没有区别

[英]Compiled query and normal query, seems no difference for me entity framework

I stuck in the compiled query and normal query i have been working on it since 3 days but did not find clue why this happening please help me. 我停留在已编译查询和普通查询中,自3天以来一直在进行此操作,但是找不到线索为什么会发生这种情况,请帮帮我。

I have one table that is "offer" which contains 3500 record. 我有一张表是“要约”,其中包含3500条记录。 I have written two type of query one is compiled query and other is normal query. 我写了两种查询,一种是编译查询,另一种是普通查询。 see below code. 参见下面的代码。

Compiled Query. 编译查询。

public static class TestCompiled
{        
    private static readonly Func<MyEntity, DateTime, IEnumerable<Offer>> activeOfferCQ =
        CompiledQuery.Compile(
          (MyEntity context, DateTime currentTime) =>
              context.Offers.Where(o => (o.Organization == null || (o.Organization != null && o.Organization.IsActive == true))));

    public static IEnumerable<Offer> GetBesicActiveOffers(MyEntity DBContext, DateTime currentTime)
    {
        return activeOfferCQ.Invoke(DBContext, currentTime);
    }
}

This is my compiled query and I have created console application to compare these compiled and normal query below is code. 这是我的已编译查询,并且我已经创建了控制台应用程序以比较这些已编译查询和正常查询,以下是代码。

Implementation 履行

MyEntity context = new MyEntity ();

for (int i = 0; i < 5; i++)
{
    var temp = i + 1;
    sp.Start();
    List<Offer> objOffers = context.Offers.Where(x =>(x.Organization == null || (x.Organization != null && x.Organization.IsActive == true))).ToList();
    Console.WriteLine("SrN: " + (temp) + "  Q1= " + sp.Elapsed.Milliseconds + "    Record Count=" + objOffers.Count());
    sp.Stop();

    sp.Start();
    List<Offer> objList = TestCompiled.GetBesicActiveOffers(context, curtime).ToList();
    Console.WriteLine("SrN: " + (temp) + "  Q2= " + sp.Elapsed.Milliseconds + "    Record Count=" + objList.Count());
    sp.Stop();
    Console.WriteLine("______________________________________________________________");
    Console.WriteLine();
    //if (i == 0)
    Thread.Sleep(5000);
}

Result. 结果。

Check result: 检查结果: 在此处输入图片说明

As I found that normal query is running faster than compiled query as we know compiled query is faster than normal query, please help me what I am doing wrong. 当我发现普通查询的运行速度比编译查询快时,因为我们知道编译查询比普通查询快,请帮助我我做错了什么。 Does I am missing any thing in compiled query. 我是否在编译查询中缺少任何内容。

With such small times and variations, I don't think the compilation time of the query (which seems pretty simple) will make any significant difference. 由于时间和变化如此之小,我认为查询的编译时间(看起来很简单)不会产生任何重大变化。

The compiled query in EF avoids the cost of, as it name says, compiling the query. 顾名思义,EF中的已编译查询避免了编译查询的开销。 It doesn't really help the database query time or data transfer time. 它实际上并没有帮助数据库查询时间或数据传输时间。 You are basically not seeing any difference (or very small ones) since the compilation time (ie: the creation time of the SQL sentence) of your query is negligible. 由于查询的编译时间(即SQL语句的创建时间)可以忽略不计,因此您基本上看不到任何差异(或很小的差异)。

As a side note: you are using a DateTime parameter on your compiled query that is not used in the query itself... I don't think in this precise case it really affects the query compilation time, but for a performance test, this seems rather weird. 附带说明:您在已编译查询中使用的DateTime参数在查询本身中未使用...我不认为在这种精确的情况下它确实会影响查询编译时间,但是对于性能测试而言,似乎很奇怪。 You should try to measure performance on identical conditions. 您应该尝试在相同条件下衡量性能。

PS : There's no direct support for compiled queries when using DbContext , and EF, starting with version 5, already caches queries where it sees fit (what they call "autocompiled queries"). PS :使用DbContext和EF(从版本5开始)时,不直接支持已编译的查询,已经在其认为合适的地方缓存了查询(它们称为“自动编译的查询”)。

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

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