|
|
Comments and Discussions
|
|
 |

|
Great article, but maybe you could have covered a bit the limits of compiled queries (waht you cannot do, what won't work, what doesn't make sense, etc.)
|
|
|
|

|
hi Shivprasad,
You have infect provided nice guidline on imporving performance using Linq to SQL.
Here is a question please. In the way you have mentioned one will need to write a seprate funtion for each Linq to DB call where input parameters are different. Can there be a generic mehtoed that can be used for all Linq calls and also returns complied Linq ?
thanks
|
|
|
|

|
Hello
Thanks for the article. I've come across couple of similar performanc comparison but curious why CompiledQuery faster? What's the theory behind this?
dev
|
|
|
|

|
I like the way you explained the complex logic in simple terms. Very good work! Keep it up.
Just one more thing I would like to share to improve more performance of web application is using Cache with LINQ Complied query. LINQ complied query provides faster results and preserve it is Cache to reuse it for later usage. This is more useful in the case where data is to be used frequently and changes in such data are few or rare. We have used this combination in Job Portal and it provides results in seconds even on large number of rows.
Regards,
Virat Kothari
|
|
|
|
|

|
Excellent article and it is too useful
|
|
|
|

|
good one
For more help : pranayamr.blogspot.com
|
|
|
|

|
What happened if i used Stored procedures with Linq !? is this gone boost its performance maybe similiar to the compiled query performance ?
what i can do if i cant use the compiled query option how i can boost the performance of linq just drop it and use another technique!?
please any help to answer these questions ....
Regards
Mohammad Masoud
|
|
|
|

|
| Yes stored procedure also boost performance. LINQ generates dynamic queries which is not so effiecient. Controlling through SP's will increase performance. Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|

|
Thats not true, linqtosql isn't dropped by m$, linqtosql is the best solution, want prove ? StackOverflow is using l2s and it is huge portal with 24/7 time massive read/write to db - all in l2s...
|
|
|
|

|
FACT: No high-end database solution in the world is ever going to accept LINQ-powered solutions.
When you google LINQ you find millions of blogs and other articles about how good LINQ is. In reality, its use is forbidden. The reason is simple; servers cannot make good optimization decisions when given client-built ad hoc-queries. ALWAYS use stored procedures!
However, for hobby projects, LINQ is fine.
|
|
|
|

|
FACT: You don't know what you're talking about
|
|
|
|

|
Well, he isn't completely wrong: with Linq, you lack the level of fine tuning that you have if you send the query string directly. But then you may loose in readability.
What I'd like with LINQToSQL is to have a way to see the query that will be sent (this is probably already possible, I'll have to check). This way, I can build my prototype of the client quickly using LINQ, and then fine tune performance later (using a customized query string based on the initial one), if need be.
|
|
|
|

|
a late reply for your old comment... maybe you changed your mind in between? You didn't think how useful LINQ is just with objects - yes database queries are sometimes problematic - but how many damn loops I was writing before LINQ...
|
|
|
|

|
And yet linq to sql will always be slower then native query calls. Some other things I noticed about linq2sql:
- no full text index search
- no way to pass query hints
- linq 2 sp is useless as you lose all the dynamics
I love using linq/extension methods on objects (it really did cut the coding time), but with sql usage, i'll stick to plain old querys.
|
|
|
|

|
Stored procedures are a technology with significant limitations. There has been no big improvement in years and years. There is a complete disconnect between the script, the database, and the business logic.
LINQ is about building expressions that work against multiple types of data and data providers. Which is something stored procedures were never designed to do.
Before you reply with some out of context argument, think about the limitations of a relational database and the advantages that LINQ2SQL brings.
|
|
|
|

|
Linq project is dropped by microsoft so is Linq to Sql and articles based on that are relevant.
modified on Friday, August 21, 2009 4:49 AM
|
|
|
|

|
This is a news for me....Any pointers and links for the same
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|
|

|
They are not killing LINQ they are simply done with it. It is code complete.. Read the articles you link too, rather than jumping into sensationalism.
|
|
|
|

|
this lame flame bait should be good for other gossip sites.
|
|
|
|

|
Hi,
nice article
See also this article: LINQ compiled query[^] Although the article is in german language the images at the end show some interesting facts (the show a profiler output).
Kind regards,
Günther
|
|
|
|

|
If you are just performing a "Select" operation, and you are not going to modify and update back into database, make sure you turn off object tracking.
DataContextObject.ObjectTrackingEnabled = false;
by default it is set to true.
This may also help in tuning performance.
-(|[ NightMare |])-
[Without a strive for perfection I would terribly be bored]
|
|
|
|

|
Compiling LINQ queries works great if the shape of the query is static - but if the query is dynamically composed (ie, add this predicate / join only when this input parameter has a value) then compilation is not an option.
It's ironic, because the queries that would actually benefit from compilation are most likely to use some form of composition as well, because it generally yields better execution plans for SQL Server. It's a trade-off - I'd rather have good SQL execution plans that return the data as quickly as possible.
|
|
|
|

|
MR_SAM_PIPER wrote: Compiling LINQ queries works great if the shape of the query is static
Agreed, LINQ queries can not be dynamically created on fly. So the dynamic option is completely ruled out.
MR_SAM_PIPER wrote: It's a trade-off - I'd rather have good SQL execution plans that return the data as quickly as possible.
Even SQL query plans are not created if the SQL is built on fly. I mean of you write something like this in stored proc's , i doubt query plans will be created. When i run the show plan it shows me "0" i.e no query plan
exec("select * from cust where custcode='" + select top ....)
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|

|
That's not the case - LINQ to SQL uses the procedure sp_executesql to execute parameterized queries against the database, which enables maximum reuse of query plans on the database server end. That is definitely a good thing in my book.
Query plans are ALWAYS created - the database engine needs it to run the statement. What you want is reuse of plans wherever possible - it's analogous to compiling the LINQ query, but there's much more benefit from reusing the plan at the database end because it's much closer to the actual data.
If you fire up the profiler and execute a trace that captures the output of a LINQ to SQL query with one or more parameters, you will see what I'm talking about.
|
|
|
|

|
MR_SAM_PIPER wrote: If you fire up the profiler and execute a trace that captures the output of a LINQ to SQL query with one or more parameters, you will see what I'm talking about.
Thanks for sharing.Yes i did that and it was indeed a great experience. sp_executesql where executed and the plans where reused. I was able to see the plan reuse flag in sql server. In my previous note i was referring to 'exec' statements which do not use the plan. Did not knew about sp_executesql SP.
It was a great experience to see the behind scene execution of LINQ to SQL.
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|
|

|
That was a great input thanks
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|

|
Hi Shiv,
This is Gurung , i hope you remember me , we met at Microsoft Grava presentation. Congrats for becoming MVP on ASP/ASP.NET , you deserved it.You never told me you are a Microsoft MVP. I was googling on internet with your name and landed here.
All your articles are good. God bless you mere dost.
|
|
|
|

|
Hey Gurung,
How's things friend, Nice to see you posting on code project. If you are a MVP, there is nothing to talk about it as such.
I was bit disappointed with Grava for only supporting WPF. It's a world a of internet and if a technology is not supporting cross browser solutions there is something seriously wrong in the vision.
By the way if you any more such domain oriented Microsoft technology meets coming up , let me know. I will glad to plug in.
This time after meet we will have a evening chill beer....All from me.
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|

|
Making LINQ quicker is done using PLINQ or placing the LINQ in the Server.
It is also against the The Code Project policy to advertise in your article:
<a href="http://www.questpond.com">http:
|
|
|
|

|
TheArchitectualizer wrote: Making LINQ quicker is done using PLINQ or placing the LINQ in the Server.
Thanks you voted me 2 , i am happy with that.PLINQ allows a developer to take advantage of parallel hardware and this article was not targetted to that aspect. There are 1000 other ways by which you can increase LINQ performance this article was not targetted to that. My main emphasis was on how LINQ query plan compilation can be cached for better performance and comparing the same with non-cached.
TheArchitectualizer wrote: placing the LINQ in the Server.
Are you talking write TSQL with .NET and LINQ and pushing to SQL Server as a stored procedure.
TheArchitectualizer wrote: It is also against the The Code Project policy to advertise in your article:
Nothing written in the article submission guidelines as such. I will consider it.
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
modified on Sunday, July 19, 2009 1:38 AM
|
|
|
|

|
Shivprasad koirala wrote: Thanks you voted me 2 , i am happy with that.PLINQ allows a developer to take advantage of parallel hardware and this article was not targetted to that aspect. There are 1000 other ways by which you can increase LINQ performance this article was not targetted to that. My main emphasis was on how LINQ query plan compilation can be cached for better performance and comparing the same with non-cached.
Sorry! I will change it to 4.
Shivprasad koirala wrote: Are you talking write TSQL with .NET and LINQ and pushing to SQL Server as a stored procedure.
No, you can actually run .NET inside the Server.
Using .Net inside SQL Server[^]
Hmm, but LINQ to SQL is not supported in the CLR on the Server.
LINQ in CLR Integration[^]
'We will see how the code you write can be simpler than TSQL and perform better than TSQL, whilst not impacting on the stability of your system. We will also look at scenarios that aren’t possible using TSQL and how we can solve easily them using CLR objects.'
Shivprasad koirala wrote: Nothing written in the article submission guidelines as such. I will consider it.
Hmm, sorry. Read it again. Also try clicking on the 'Report Flag' at the top of your article. You will see a list box item with such information.
I do like your articles. Sometimes I think you get a little too opinionated and draw your own conclusions which are misleading. Sorry, I think people deserve the truth. Not implying you are lying or anything. I mean to say: you deserve the truth from me. But I am probibly being hypricotical by stating that.
modified on Sunday, July 19, 2009 3:04 AM
|
|
|
|

|
TheArchitectualizer wrote: Sometimes I think you get a little too opinionated and draw your own conclusions which are misleading. Sorry, I think people deserve the truth. Not implying you are lying or anything. I mean to say: you deserve the truth from me.
I agree , sometimes i get too hyper and conclude things. Agreed , i will try to improve on the same in future. Thanks for your inputs to improve me.
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|

|
Several of the conclusions you drew from running the code are misleading. For starters you cannot simply ignore the first execution. This startup cost is associated with the actual compiling of the query, and is a real performance hit that needs to be accounted for. If you are planning on executing a sql statement many times then it averages away, but if you are simply calling it once (say to read a configuration table), then this performance loss is realized.
You also seem to have concluded that 5 ms less is 5 times, this may be a language barrier thing, but it is not 5 TIMES faster, you data averages out to around 3-4 times faster, but even that is misleading as the performance bottleneck changes based on the query, and you're measuring the duration of the query, not the duration of the overhead imposed by LINQ. A long running query would likely see less relative gain.
You have provided some valuable tips on possible ways to improve performance, but I do not think you have the proper metrics to be drawing the conclusions you have, and it's especially unscientific to call out "ignore this data". Rather I think you should leave it as an exercise to the reader to determine if compiled queries would help them, and on what order of magnitude.
|
|
|
|

|
GuinnessKMF wrote: Several of the conclusions you drew from running the code are misleading. For starters you cannot simply ignore the first execution. This startup cost is associated with the actual compiling of the query, and is a real performance hit that needs to be accounted for. If you are planning on executing a sql statement many times then it averages away, but if you are simply calling it once (say to read a configuration table), then this performance loss is realized.
Thanks for your great feedback. Every word you wrote is probably right about my conclusion. My conclusion of neglecting the first reading was based on what i saw when i ran the comparison application practically. For the first run if you run the LINQ compiled query first or the non compiled the higher one is taken. Must be to do something with my environment. So wanted to neglect it.
GuinnessKMF wrote: You also seem to have concluded that 5 ms less is 5 times, this may be a language barrier thing, but it is not 5 TIMES faster, you data averages out to around 3-4 times faster, but even that is misleading as the performance bottleneck changes based on the query, and you're measuring the duration of the query, not the duration of the overhead imposed by LINQ. A long running query would likely see less relative gain.
The average difference for every subsequent reading was 5 times , so i concluded 5 x times. Yes for long running queries this can be faster. But i concluded from my environment data and my hardware.
Yes i agree my conclusions can be wrong and every one should do their testing. But fro 100% sure LINQ compiled queries are worth.
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|

|
Shivprasad koirala wrote: Thanks for your great feedback. Every word you wrote is probably right about my conclusion. My conclusion of neglecting the first reading was based on what i saw when i ran the comparison application practically. For the first run if you run the LINQ compiled query first or the non compiled the higher one is taken. Must be to do something with my environment. So wanted to neglect it.
This doesn't have anything to do with your enviroment, it's a real cost that will have to be paid. If you prepay it some way (as someone suggested in a background thread) that's good, but it cannot be entirely avoided. Even in the example you showed the total performance is worse even after 8 reads. Anyone consuming this article should understand that this isn't some magic instant improvement.
Shivprasad koirala wrote: The average difference for every subsequent reading was 5 times , so i concluded 5 x times.
5 milliseconds is not 5 TIMES faster. When talking how many "times" faster something is, you multiply/divide, not add/subtract. So even using just the subsequent runs, 52ms non-compiled, 13ms compiled. 52/13 = 3.69.. times faster.
Shivprasad koirala wrote: Yes for long running queries this can be faster. But i concluded from my environment data and my hardware.
Actually longer running queries can be faster or slower, it's a compiled execution plan, if the query runs longer because of passing tons of data, compiling will pay off less, if it takes longer because of complex query logic, then compiling helps more.
Shivprasad koirala wrote: Yes i agree my conclusions can be wrong and every one should do their testing. But fro 100% sure LINQ compiled queries are worth.
I don't disagree that this is valuable information that will help some people improve the performance of their application, but this can make things worse if not used properly, if someone isn't going to execute the query more than 25 times, this is now slower and more complex.
modified on Monday, July 20, 2009 9:55 AM
|
|
|
|

|
Agreed i should consider even the first time readings. Thanks for your detail input really helps to think better.
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|

|
You both make good points and the value of this article is that it shows using a static method can be useful in some cases. In general, I find using stored procedures with LINQ is kind of the swiss arm knive for performance. kensurferca
|
|
|
|

|
I agree, if you want the query to run faster use PLINQ. or place the LINQ code in the SQL Server.
~TheArch
modified on Sunday, July 19, 2009 3:08 AM
|
|
|
|

|
PLINQ allows a developer to take advantage of parallel hardware and this article was not targetted to that aspect. There are 1000 other ways by which you can increase LINQ performance this article was not targetted to that. My main emphasis was on how LINQ query plan compilation can be cached for better performance and comparing the same with non-cached.
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|

|
GuinnessKMF wrote: This startup cost is associated with the actual compiling of the query, and is a real performance hit that needs to be accounted for.
Okay, yes. But this can be easily avioded if you compile the query in a background thread before the query is executed.
|
|
|
|

|
Thanks for your inputs , yes i should have spwaned the same in a new thread
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|

|
Some functional similarities between Linq and Stored Procedure based data handling
1. We can execute insert/update/delete operation using both Linq and stored Procedure.
2. We can fetch data using both Linq and Stored Procedure.
3. We can populate Business object directly from linq but with stored procedure
we need one extra step to manually map fetched data and my custom business objects.
Actually Linq is used like OR mapper. But we cannot do directly with SP.
So many times I faced the similar question like why use linq instead of stored procedure for
database crud operation.
Actually I thought one of previous commenter need to clear about that.
|
|
|
|

|
Thats a decent comparison. But we can not compare both of them in terms of performance as both serve different purposes.
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|
|

|
I read you article. It's fine. I have following question: What about using of stored procedures? Stored procedures also will be precompiled after first run, in this case why we (people) should use LINQ instead of stored procedures? Is precompiled LINQ will be worked more faster than stored procedures? Did you investigate this?
|
|
|
|

|
LINQ is an additional layer on top of the data access. So LINQ will never be faster like e.g. a datareader. So stored procedures are faster than LINQ, too. Especially if you use the Entity Framework (with LINQ to Entities) instead of LINQ-to-SQL.
The great benefit of LINQ is that it is integrated into the programming language and you can write even complex statements in minutes. LINQ is not only used together with SQL statements. You can use different providers to query XML, objects etc. All with the same syntax.
|
|
|
|

|
Stored procedures and LINQ are different things. LINQ is meant to provide a uniform query language by which you can fill you business objects. Comparing LINQ with stored procedures is like comparing apples and oranges.
Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
Tips to improve your LINQ query performance.
| Type | Article |
| Licence | CPOL |
| First Posted | 15 Jul 2009 |
| Views | 108,617 |
| Downloads | 1,171 |
| Bookmarked | 134 times |
|
|