<p>LINQ simplifies database programming no doubt, but does it have a downside?  Inline SQL requires one to communicate with the database in a certain way that opens the database to injections.  Inline SQL must also be syntax-checked, have a plan built, and then executed, which takes precious cycles.  Stored procedures have also been a rock-solid standard in great database application programming.  Many programmers I know use a data layer that simplifies development, however, not to the extent LINQ does.  Is it time to give up on the SP's and go LINQ?  </p>

[{'answer_id': 72210, 'author': 'Varun Mahajan', 'author_id': 6613, 'author_profile': 'https://Stackoverflow.com/users/6613', 'pm_score': 1, 'selected': False, 'text': '<p>This is a performance run by Maximilian Beller. According to him, LINQ is much much slower. \n<a href="http://www.mbeller.de/2007/12/performance-comparison-between-linq.html" rel="nofollow noreferrer">Read his comprehensive study</a></p>\n'}, {'answer_id': 72303, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': "<p>It depends on what you're doing.  LINQ is going to be less efficient at the actual data/set manipulation than a real database.  But you'll save a lot in not having to connect to the database over a network.</p>\n\n<p>If your database is on the same machine or is formally 'well-connected', you're probably better off using it.</p>\n\n<p>But if you're getting back a large result set from a remote db that could mean significant transmission time, or if it's a really short query that won't justify the overhead, LINQ would likely be better.</p>\n"}, {'answer_id': 72308, 'author': 'Gareth Jenkins', 'author_id': 1521, 'author_profile': 'https://Stackoverflow.com/users/1521', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re interested in benchmarking, Rico Mariani has an <a href="http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx" rel="nofollow noreferrer">excellent 5-part study</a> that covers the qualitative and quantitative differences.</p>\n\n<p>He may be an MS guy, but he\'s known as a performance nut - his benchmarks are thorough and well thought out.</p>\n'}, {'answer_id': 72920, 'author': 'sirrocco', 'author_id': 5246, 'author_profile': 'https://Stackoverflow.com/users/5246', 'pm_score': 1, 'selected': False, 'text': "<p>Just think about changing a columns name - now change the (n)SPs and (x)Views.</p>\n\n<p>Do everything that is expensive on the database (like searches , sorting etc..) and you won't notice a problem.</p>\n\n<p>Also, if you want to display a large grid without paging ... then use a dataset - that one is faster.</p>\n\n<p>StackOverflow also uses linq2sql - do you see a problem :) ?</p>\n\n<p>Use an ORM - it's the way to go on most applications.</p>\n\n<p>PS: also, about micro benchmarks - like .. let's select 10.000 rows with an ORM - DON'T DO IT. That's not why you use an ORM. If you want to select 10.000 rows use ADO.</p>\n"}, {'answer_id': 73514, 'author': 'Randolpho', 'author_id': 12716, 'author_profile': 'https://Stackoverflow.com/users/12716', 'pm_score': 0, 'selected': False, 'text': "<p>Because of the structure of LINQ to SQL, there is no possible way it can be <em>faster</em> than using raw SQL, either your own well-formed queries or as a stored procedure. What LINQ buys you is not speed but type safety and organization; in short most of the benefits that ORMs generally grant you. </p>\n\n<p>LINQ to SQL is not about speed, it's about building a more maintainable software system. It's about all the stuff dedicated Software Engineers and Architects care about, stuff like loose coupling and layering</p>\n\n<p>That's not to say that you can't build some really unmaintainable code with LINQ -- nobody is keeping you from shooting yourself in the foot but you -- but done properly, LINQ can help tremendously. I'm not saying LINQ is a silver bullet, however. It has a host of issues that make it difficult to use in many enterprise situations -- which is why MS offers Entity Framework (ADO.NET 3.0). Of course, even that's not perfect given the recent EF Vote of No Confidence. </p>\n\n<p>Is LINQ to SQL or even EF better than raw SQL? I'd say a resounding Hells Yeah. Are there other solutions that might work better? Maybe. </p>\n"}, {'answer_id': 73805, 'author': 'Jeremiah Peschka', 'author_id': 11780, 'author_profile': 'https://Stackoverflow.com/users/11780', 'pm_score': 4, 'selected': True, 'text': '<p>LINQ to SQL actually presents some alarming performance problems in the database.  Basically, it creates multiple execution plans based on the length of the parameter you are using.  I posted about it a while back on my blog <a href="http://facility9.com/2008/08/28/linq-to-sql-may-cause-performance-problems/" rel="noreferrer">LINQ to SQL may cause performance problems</a>.  </p>\n\n<p>Now, is that to say that LINQ doesn\'t have a place?  Hardly. LINQ definitely has a place in the development toolkit, just like stored procedures. Ultimately, you want to use stored procedures when performance is absolutely necessary and use an ORM tool in any other situation.</p>\n\n<p>As far as inline SQL goes, there are ways to execute inline SQL so that the plan is only built once and is never recompiled.  Most ORMs should take care of this aspect of performance tuning as well and using these methods is usually the safest way to execute your SQL since it forces you to use parameterized queries.</p>\n\n<p>Like most database solutions, the right answer depends on the problem you\'re trying to solve.  If you favor development speed over database/application performance, then using LINQ or another DAL/ORM tool is the best way to go.  If you favor performance over ease of development, then using stored procedures and pure datasets is going to be your best bet.  LLBLGen even provides a LINQ to LLBLGen layer so you can use LINQ to query LLBLGen\'s objects and have LLBLGen actually handle building your queries and avoid some of the downfalls of LINQ.</p>\n'}, {'answer_id': 74955, 'author': 'James Curran', 'author_id': 12725, 'author_profile': 'https://Stackoverflow.com/users/12725', 'pm_score': 2, 'selected': False, 'text': '<p>Your basic premise is flawed..</p>\n\n<blockquote>\n  <blockquote>\n    <p>Inline SQL requires one to communicate with the database in a certain way that opens the database to injections.</p>\n  </blockquote>\n</blockquote>\n\n<p>No it doesn\'t.  Hard-coding user-inputted values into a SQL statement does, but  you could do that with store procedures as well.</p>\n\n<p>Parameterizing your queries guards against injection attacks, but inline SQL can be parameterizing just as easily as stored procedures.</p>\n\n<blockquote>\n  <blockquote>\n    <p>Inline SQL must also be syntax-checked, have a plan built, and then executed.</p>\n  </blockquote>\n</blockquote>\n\n<p>All Sql (SPs and inline) must be syntax-checked and have a plan built on their first call. Thereafter, the exact text of the request &amp; the execution plan are cached.  If another request with the exact same text (not counting parameters) is received, the cached execution plan is used.  </p>\n\n<p>So, if you hard-code values into inline SQL, the text won\'t match, and it will have to re-parse the query.  However, if you use parameters, the text of the query will match, and you will get a cache hit. In which case, it wouldn\'t matter if the query in inline SQL or a SP.</p>\n\n<p>In other words, the only problem with inline SQL is that it easy to do something that slow &amp; insecure.  But making inline SQL fast &amp; secure is no more work that using a SP.</p>\n\n<p>Which brings us to LINQ, which always using parameters, even if you hard-code the values into the LINQ statement, making "fast &amp; secure" inline SQL trivial.</p>\n\n<p>LINQ also have the advantage over SPs of having all your code in one spot, instead of scattered over two different machines.</p>\n'}]
['https://Stackoverflow.com/questions/72168', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1115144/']