[dba-VB] LINQ Compiled Query

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Sun Jan 9 17:08:03 CST 2011


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-43
48-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
 
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: 9 ?????? 2011 ?. 18:00
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] LINQ Compiled Query

Hi Shamil

Yes, I noticed that comment and think it is valid. 
However, if you have a crucial query in, say, a loop it could be worth the
efforts.

/gustav


>>> shamil at smsconsulting.spb.ru 09-01-2011 15:20 >>>
Hi Gustav --

No, I didn't. 

One of comments says: "I feel this compiled query approach is cumbersome.
Image your application has 100 linq queries, you have to create 100 helper
classes, and another 100 static functions. I wish Microsoft provides an
easily way, maybe some syntax sugar, or some more magics in DataContent, to
make programmer life easier. "

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 ?. 17:12
To: dba-vb at databaseadvisors.com
Subject: [dba-VB] LINQ Compiled Query

Hi all

Has anyone tested this technique out:
http://www.blog.ingenuitynow.net/15+Minutes+On+LINQ+Compiled+Queries.aspx 

It looks quite simple to implement.

/gustav






More information about the dba-VB mailing list