I've just recently started using LINQ on a daily basis. I've read quite a lot about L2E queries should be compiled to improve performance using the following:
CompiledQuery.Compile(query);
Using LINQ-To-Entities 4.0 I ran a query 10 times uncompiled and then compiled and yielded the following results in seconds:
// Sample Query
from u in ctx.Users orderby u.Id, u.Username select u
Uncompiled Compiled
---------------------
0.295 0.2946174
0.024 0.0220462
0.008 0.0060126
0.013 0.0210441
0.007 0.010021
0.011 0.010021
0.008 0.0060126
0.009 0.0070147
0.008 0.0060126
As you can see there's not a real big difference in the times from my small test. There is slower time for the first call, and then both speed up (implying compilation/caching). Can anyone provide insight to this?
From what I can gather, it has about the same benefit of hard-coded SQL queries with embedded parameters to SQL queries using named parameters: The system can recognize that they're all the same and use the same query plan.
For instance, if you were to simply inline the expression above, it will probably work just as well as compiling it if you're always passing in the same ctx.Users
object. However, if you had several user repositories of the same type and were planning on using that order by
on all of them, it would be a good idea to compile the query once and use parameters to access it.
When researching this, I took a look at how a LINQ query is formed in IL: a new Func<>
delegate is created for just about every clause in your LINQ query every time you call it. For that reason alone, I imagine that compiling a query will be better for your system as far as memory thrashing goes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With