简体   繁体   中英

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.

I have one table that is "offer" which contains 3500 record. 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. 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.

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. 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").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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