LINQ - Compiled Query Does not work as concept

Asked By imran khan on 22-Jun-11 01:32 PM

Dear All,

I am using LINQ with Entities

I am executing  same query with different parameter in a for loop. So I decided to Prepare that query as compiled query and then call the compiled query. I did the same in the following manner but there is no difference in time when query execute first or then after … Please suggest where I gone wrong.

As query was executing in DAL layer. So I create my own DAL class Called ABC.cs  in DAL.

In that  First I create global variable for compiled as


public static  Func<datacontext , DateTime, string, IQueryable<tmpclass >> compiledQuery;



Then I assigned the compiled veriable in the constructor of DAL class


static ABC()

      {

       

        compiledQuery = System.Data.Objects.CompiledQuery.Compile<datacontext, DateTime, string, IQueryable< tmpclass >>(

               (ctx, orderDate, shemecode) => (from dsp1 in ctx.table1 where dsp1.NAVDATE <= orderDate && dsp1.SCHEMECODE == shemecode

orderby dsp1.NAVDATE  descending                               select new Quartile_tmp_dt  { newbasedt= dsp1.NAVDATE }));

      }

 

 

 

 

Then I am invoking  it as

 

 

 

  using (DataContext AWEntities = new Datacontext())

{

 AWEntities.Tables1.MergeOption =System.Data.Objects.MergeOption.NoTracking;

 AWEntities.Tables1.MergeOption = System.Data.Objects.MergeOption.NoTracking;

 

foreach (var item in result)

 {

DateTime frdt = Convert.ToDateTime(frmDate);

var res = compiledQuery.Invoke(AWEntities, frdt, item.SCHEMECODE);

}

}

pete rainbow replied to imran khan on 22-Jun-11 07:32 PM
is the query you are running getting a good query plan,  ie running against indexes etc

you might want to have a look in the dbms log and see what it's doing

if there is no good query plan then doing a compiled query will gain you little as most of the time will be spent in running the query

how does this compare to running the old style datareader?