[dba-VB] LINQ Compiled Query

Gustav Brock Gustav at cactus.dk
Mon Jan 10 05:44:57 CST 2011


Hi Shamil

Good question, and I don't know, but I would guess that at least each thread will run at the same speed as if the query was not compiled.
In fact I have some trouble understanding what really is "complied" using this technique? Perhaps it is only some clever caching behind the scene that is taken place.

/gustav


>>> shamil at smsconsulting.spb.ru 10-01-2011 00:08 >>>
Hi Gustav --

<<<
Precompiling LINQ Queries by Julie Lerman
http://msdn.microsoft.com/en-gb/magazine/ee336024.aspx 
>>>
Yes, that's a very good article, thank you.

Is it interesting what will happen when two or more threads (on ASP.NET
app/service, WCF service, ....)  will try to use the same static compiled
LINQ query (with different parameters)?
Will the first thread block the others till query execution finishes?  
(I'm copying here refererred article's code sample)

static Func<ObjectContext, int, Customer> _custByID;

public static Customer GetCustomer( int ID)
    {
      //test for an existing instance of the compiled query
      if (_custByID == null)
      {
        _custByID = CompiledQuery.Compile<SalesEntities, int, Customer>
         ((ctx, id) => ctx.Customers.Where(c => c.CustomerID == id).Single());
      }
      return _custByID.Invoke(_context, ID);
    } 


Thank you.

--
Shamil
 
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: 9 ?????? 2011 ?. 20:30
To: dba-vb at databaseadvisors.com 
Subject: Re: [dba-VB] LINQ Compiled Query

Hi Shamil

I have no idea - and things may have evolved from 2008 to 2010.
Perhaps the only "trick" is to wrap the LINQ query in a static class?
But no ... I browsed a little and found this from a year ago:

EF 4.0 & compiled queries & performance
http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/0c07e1d6-7db6-4348-b106-e576d3153b70

which demonstrates a dramatic speed increase for repeated runs of a compiled query.
It also provides this link which seems to explain what is going on:

Precompiling LINQ Queries by Julie Lerman
http://msdn.microsoft.com/en-gb/magazine/ee336024.aspx 

/gustav

PS: All MSDN Magazines can be browsed from here:
http://msdn.microsoft.com/en-gb/magazine/ee310108.aspx 


>>> shamil at smsconsulting.spb.ru 09-01-2011 16:55 >>>
Hi Gustav --

<<<
However, if you have a crucial query in, say, a loop it could be worth the
efforts.
>>> 
Shouldn't LINQ be "smart enough" to not rebuild dynamic queries when that
used in a loop? (of course if LINQ expression is not forcibly destroyed (by
developer) at the end of every cycle iteration).

I'd guess even if they get forcibly destoryed LINQ coudl somehow keep
parsed&compiled LINQ expression cached...

It's a very smart technology... (nothing new - all the principles used in
(P)LINQ are known for years but that's the first time such a technology
released to the business applications developers?)

Thank you.

--
Shamil





More information about the dba-VB mailing list