<p>One of my co-workers claims that even though the execution path is cached, there is no way parameterized SQL generated from an ORM is as quick as a stored procedure. Any help with this stubborn developer?</p>

[{'answer_id': 50352, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 5, 'selected': True, 'text': '<p>I would start by reading this article:</p>\n\n<p><a href="http://decipherinfosys.wordpress.com/2007/03/27/using-stored-procedures-vs-dynamic-sql-generated-by-orm/" rel="nofollow noreferrer">http://decipherinfosys.wordpress.com/2007/03/27/using-stored-procedures-vs-dynamic-sql-generated-by-orm/</a></p>\n\n<p>Here is a speed test between the two:</p>\n\n<p><a href="http://www.blackwasp.co.uk/SpeedTestSqlSproc.aspx" rel="nofollow noreferrer">http://www.blackwasp.co.uk/SpeedTestSqlSproc.aspx</a></p>\n'}, {'answer_id': 50355, 'author': 'rjzii', 'author_id': 1185, 'author_profile': 'https://Stackoverflow.com/users/1185', 'pm_score': 3, 'selected': False, 'text': '<p>For most people, the best way to convince them is to "show them the proof." In this case, I would create a couple basic test cases to retrieve the same set of data, and then time how long it takes using stored procedures versus NHibernate. Once you have the results, hand it over to them and most skeptical people should yield to the evidence.</p>\n'}, {'answer_id': 50356, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 3, 'selected': False, 'text': '<p>Round 1 - You can start a profiler trace and compare the execution times. </p>\n'}, {'answer_id': 50358, 'author': 'Boris Terzic', 'author_id': 1996, 'author_profile': 'https://Stackoverflow.com/users/1996', 'pm_score': 1, 'selected': False, 'text': '<p>Measure it, but in a non-micro-benchmark, i.e. something that represents real operations in your system. Even if there would be a tiny performance benefit for a stored procedure it will be insignificant against the other costs your code is incurring: actually retrieving data, converting it, displaying it, etc. Not to mention that using stored procedures amounts to spreading your logic out over your app <strong>and</strong> your database with no significant version control, unit tests or refactoring support in the latter.</p>\n'}, {'answer_id': 50359, 'author': 'matt b', 'author_id': 4249, 'author_profile': 'https://Stackoverflow.com/users/4249', 'pm_score': 1, 'selected': False, 'text': '<p>Benchmark it yourself. Write a testbed class that executes a sampled stored procedure a few hundred times, and run the NHibernate code the same amount of times. Compare the average and median execution time of each method. </p>\n'}, {'answer_id': 50360, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 1, 'selected': False, 'text': '<p>It is just as fast if the query is the same each time.  Sql Server 2005 caches query plans at the level of each statement in a batch, regardless of where the SQL comes from.</p>\n\n<p>The long-term difference might be that stored procedures are many, many times easier for a DBA to manage and tune, whereas hundreds of different queries that have to be gleaned from profiler traces are a nightmare.</p>\n'}, {'answer_id': 50361, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 1, 'selected': False, 'text': "<p>I've had this argument many times over.<br>\nAlmost always I end up grabbing a really good dba, and running a proc and a piece of code with the profiler running, and get the dba to show that the results are so close its negligible.</p>\n"}, {'answer_id': 50363, 'author': 'Johannes Passing', 'author_id': 4372, 'author_profile': 'https://Stackoverflow.com/users/4372', 'pm_score': 0, 'selected': False, 'text': "<p>Measure it. </p>\n\n<p>Really, any discussion on this topic is probably futile until you've measured it.</p>\n"}, {'answer_id': 50369, 'author': 'l0st3d', 'author_id': 5100, 'author_profile': 'https://Stackoverflow.com/users/5100', 'pm_score': 0, 'selected': False, 'text': '<p>He may be correct for the specific use case he is thinking of.  A stored procedure will probably execute faster for some complex set of SQL, that can be arbitrarily tuned.  However, something you get from things like hibernate is caching.  This may prove much faster for the lifetime of your actual application.</p>\n'}, {'answer_id': 50378, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': "<p>The problem here is that you've accepted the burden of proof.  You're unlikely to change someone's mind like that.  Like it or not, people--even programmers-- are just too emotional to be easily swayed by logic.  You need to put the burden of proof back on him- get him to convince you otherwise- and that will force him to do the research and discover the answer for himself.</p>\n\n<p>A better argument to use stored procedures is security.  If you use <em>only</em> stored procedures, with <em>no</em> dynamic sql, you can disable SELECT, INSERT, UPDATE, DELETE, ALTER, and CREATE permissions for the application database user.  This will protect you against most 2nd order SQL Injection, whereas parameterized queries are only effective against first order injection.</p>\n"}, {'answer_id': 50383, 'author': 'NotMe', 'author_id': 2424, 'author_profile': 'https://Stackoverflow.com/users/2424', 'pm_score': 3, 'selected': False, 'text': "<p>I would only add a couple things to Rob's answer: </p>\n\n<p>First, Make sure the amount of data involved in the test cases is similiar to production values.  In other words if your queries are normally against tables with hundreds of thousands or rows, then create such a test environment.  </p>\n\n<p>Second, make everything else equal except for the use of an nHibernate generated query and a s'proc call.  Hopefully you can execute the test by simply swapping out a provider.</p>\n\n<p>Finally, realize that there is usually a lot more at stake than just stored procedures vs. ORM.  With that in mind the test should look at all of the factors: execution time, memory consumption, scalability, debugging ability, etc.</p>\n"}, {'answer_id': 50971, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 0, 'selected': False, 'text': "<p>The additional layer of abstraction will cause it to be slower than a pure call to a sproc. Just by the fact that you have additional allocations on the managed heap, and additional pushes and pops off the callstack, the truth of the matter is that it is more efficient to call a sproc over having an ORM build the query, regardless how good the ORM is.</p>\n\n<p>How slow, if its even measurable, is debatable. This is also helped by the fact that most ORM's have a caching mechanism to avoid doing the query at all.</p>\n"}, {'answer_id': 50987, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 0, 'selected': False, 'text': "<p>Even if the stored procedure is 10% faster (it probably isn't), you may want to ask yourself how much it really matters. What really matters in the end, is how easy it is to write and maintain code for your system.  If you are coding a web app, and your pages all return in 0.25 seconds, then the extra time saved by using stored procedures is negligible.  However, there can be many added advantages of using an ORM like NHibernate, which would be extremely hard to duplicate using only stored procedures.</p>\n"}]
['https://Stackoverflow.com/questions/50346', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2701/']