The Myth of Stored Procedures Preference

When looking to the Stored Proscedures debate, there is always those three factors you should measure by.

Productivity

- Span code base over multiple staging environments is a pain and harder to maintain.

- Versioning sp code is way harder than versioning application code.

- Minor change to the design require changing in both the SPs and the DAL code.

- Todays IDEs are more advanced than most of the RDBMS offers, implementing on IDEs is obviously preferable.

- Switching between two seprate world to implement single method is always pain.

- It's impossible to cover every single scenario and write SP for it, which will lead to write these SP as you go, huge consistency problem.

- There is no way to only update single param in the Update method using SPs as there isn't optional parameters, on every update you need to supply full param collection.

- SPs are not portable if you want to develop application that run over multiple DBMS you will be writing SPs for each DBMS, standard SQL is portable.

Security

- Big myth over here, using SPs not guarante best security practice and certinly dosn't mean that your application is SQL Injection proof you can write code like this

string s = "EXEC sp_GetCustomerByEmail '" + txtEmailAddress.Text + "'";

and you will be using SP and still open to all kind of SQL Injection.

- Another myth regarding security is that if you are using Ad-hoc queries you *most likely* grand permissions for CRUD operations for your application user on the database, no you are not, that's why Views are invented.

Performance

often when SPs vs. Ad-hoc queries debate intoduced the performance card played, SPs advocates says SPs are pre-compiled which is not let met quate like Frans did from SQL Server Books Online

SQL Server 2000 and SQL Server version 7.0 incorporate a number of changes to statement processing that extend many of the performance benefits of stored procedures to all SQL statements. SQL Server 2000 and SQL Server 7.0 do not save a partially compiled plan for stored procedures when they are created. A stored procedure is compiled at execution time, like any other Transact-SQL statement. SQL Server 2000 and SQL Server 7.0 retain execution plans for all SQL statements in the procedure cache, not just stored procedure execution plans.

So with no pre-compilation and caching for both SPs and SQL statments there is no advantage for SPs here, in some other databases the SPs compiled into C or C++ but this isn't the case in SQL Server 7.0/2000.

I have introuduced my view on the SP vs. Dynamic SQL i don't see any benfit of SPs over the huge amount of productivity, performance that you will gain with dynamic SQL, the only benfit in peformance you will get it when using Managed SPs (SQL Server 2005) but for 0.7/2000 SPs isn't the right choice for most of the scenarios.

Read more (diverse views):

- Frans Bouma's Stored procedures are bad, m'kay?

- Jeff Atwood's Stored Procs vs. Ad-hoc , Give me parametrized SQL, or give me death

- Eric Wise's The Pragmatic Adhoc SQL vs Stored Procedures Discussion

- Rob Howard's Don't use stored procedures yet? Must be suffering from NIHS (Not Invented Here Syndrome)

- Jeremy D. Miller's Why I do not use Stored Procedures

Published Sun, Jan 6 2008 12:16 AM by Adel Khalil

Comments

# The Myth of Stored Procedures Preference

Saturday, January 05, 2008 11:34 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# Pages tagged "Diverse"

Sunday, January 06, 2008 2:46 AM by Pages tagged "Diverse"

Pingback from  Pages tagged "Diverse"

# re: The Myth of Stored Procedures Preference

Sunday, January 06, 2008 5:38 AM by tony petruzzi

the only benefit I've ever seen for using stored procedures instead of dynamic sql is when more than one database server is involved. Using stored procedures and views can over come the burden of trying to mash together two separate databases within your application.

Within the stored procedure or view you can access all the linked servers that you want while still working from within a single database. This means that your application only needs to connect and use a single datasource.

Ever better is you are not limited in using linked servers, stored procedures and views to connect to other MS SQL databases, you can also connect to other database engine such as Oracle.

In the coldfusion project I'm working on right now I'm using linked servers and views to connect and mash database between MS SQL and Oracle. The view calls both database to mash data together. I also use views to insert data into the Oracle database, this could be accomplish using stored procedures, but I like using views instead.

# re: The Myth of Stored Procedures Preference

Sunday, January 06, 2008 11:46 AM by hsTed

This is a very poorly written and constructed article.

# re: The Myth of Stored Procedures Preference

Sunday, January 06, 2008 4:04 PM by Timestamp

The paragraph about SQL injection is nonsense.

Executing a stored procedure is much like calling a simple SQL statement; it can be done in insecure ways (dumb string concatenation like in the example) or in secure ways (e.g. JDBC parameters), and both styles of SQL are equally exposed to SQL injection.

# Stored Procedures vs. Dynamic SQL - The never ending debate? « blog.jemm.net

Pingback from  Stored Procedures vs. Dynamic SQL - The never ending debate? « blog.jemm.net

# Stored Procedures vs. Dynamic SQL - The never ending debate? « blog.jemm.net

Pingback from  Stored Procedures vs. Dynamic SQL - The never ending debate? « blog.jemm.net

# re: The Myth of Stored Procedures Preference

Sunday, January 06, 2008 7:51 PM by Bart Czernicki

In general if your write crappy code/have poor indexes/didn't set up SQL Server correctly it won't matter if you chose SPs over sql.  This is a real secondary thing.

I do think though that stored procedures help out a lot as units of deployment/organization as fixes/releases come into play.  Plus they abstract your code slightly as do views do.

# re: The Myth of Stored Procedures Preference

Sunday, January 06, 2008 9:45 PM by Sean

SP's do take longer to write, but it pays off in the long run if you end up with a big application -- it'll be easier to maintain.

If a SP isn't portable between various DBMS's, then your ad-hoc SQL probably isn't portable either.

SP's aren't a guarantee for perfect security, but they're still much better than ad-hoc SQL.  You have to give a user account much higher SQL permissions than you would if you were just using stored procedures.

Writing code like "EXEC sp_GetCustomerByEmail '" + txtEmailAddress.Text + "'"; isn't nearly as secure as using an SqlParameter -- the ideal method to call SP's is to call your SP and pass all its parameters as an arraylist.  There's much, much less potential for injection.

Also, with SP's, you don't have to recompile your ASP.NET application, you have better layer abstraction, and the best benefit -- you have a lot less transferred data.  Instead of transferring big, bulky ad-hoc SQL, you can transfer a stored procedure name and its parameters to the SQL server.

# re: The Myth of Stored Procedures Preference

Sunday, January 06, 2008 10:07 PM by Andrey Shchekin

>This is a very poorly written and constructed article.

I think it is rushed a bit, but all points it touches are good.

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 1:12 AM by Adel Khalil

@Andrey Shchekin

I may haven't spent the time required for this to turn out great article but i wanted to get my view on this ASAP.

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 4:28 AM by Horis Dinglebery

This article makes the author appear ignorant.

If you were to blindly follow the authors advice you could end up in a world of hurt.

For example - the execution plan for a stored procedure is created and cached stored once. The execution plan for an ad-hoc query is stored for each variant of the query. So if the code has a query to get user details:

select * from user where email = 'bill@microsoft.com'

The execution plan is generated and cached every variant of the query (i.e every time a user logs in) Imaging if gmail used ad hoc queries in this manner.

I am not saying that the use of ad-hoc queries is bad, there are work arounds for this issue such as parameterised queries. I am saying that this article is poorly written and should be taken with a grain of salt.

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 9:47 AM by Duncan

Agree with the points in the article for CRUD style operations.  

For intensive "batch" processing tasks such as collecting payments over 1 million customers, stored procedures significantly out perform dynamic SQL due to the latency between the application and the database.  Caching of query plans isn't the issue in this situation.

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 12:59 PM by Stefan

While I do see your points there are a few things I see differently:

The biggest reason for SP:s in my point of view is ensuring data consistency. Limiting the surface area to the data.

In larger projects where many people/even multiple teams are involved it's simply stupid to put the responsibility for query writing to the

developers. Sooner or later they will forget that CREATED column should not be changed when UPDATING etc. The biggest asset lies in the data (not the app), and data should be protected for consistency by all possible means.

You say: "Minor change to the design require changing in both the SPs and the DAL code."   You see it as something negative, I see it as something positive.

I would rather have compilation error, or even app crashes than inconsistent data because developer (me maybe) forgot to change a SQL statement.

Kind of type safety...

With a good code generator (generating sp calls from meta data) it really doesn't have to be that much of a burden. Although I do really agree on the "Versioning sp code" point.    

Last I fully agree with Sean, "Not having to recompile your ASP.NET application" - saved me a couple of times,

"you have better layer abstraction", and "lot less transferred data"

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 4:09 PM by Fady Anwar

Adel sorry but i must disagree with you

hard coded sql statements in the DAL is worst thing i could think of for the following reasons

1- when ur sql code is large and what mean by large is more than 1000 line it's not a good idea to write it using an VS IDE

2- who said there is no versioning tools for sql? VSTS now offer support for SQL projects

3- it's always a good practice to encapsulate the database functionality in stored procedures, so when ever u need to change ur database design u can do it and change ur stored procedures without rebuilding and deploying the project, and i'm not talking here about the projects that need 5 min for deployment, i'm talking about projects that take days and sometimes requires staff to travel abroad to deploy it

4- i don't think VS is better than the sql studio from the productivity aspect to write sql statements

unless u r talking about statements like select * from products

5- "It's impossible to cover every single scenario" who said so? the same way you can make ur ad-hoc sql statements dynamic you can make stored procedures dynamic too, stored procedures have parameters for a reason

6- "string s = "EXEC sp_GetCustomerByEmail '" + txtEmailAddress.Text + "'";" i've never seen a developer stupid enough to do that :D

simply because if u use stored procedures with security in mind and u know that hard coded sql statements is BAD and vulnerable to sql injection, the u will never take the effort to implement a sql stored procedure then execute it from a hard coded sql statement

7- you can't use transactions in hard coded sql statements the same way like stored procedures, u will have to handle these transactions ur self from ur DAL code, which is something i don't recommend

8- if you implemented your transaction in ur DAL layer which is again something i don't recommend, if ur web application failed for any reason u won't be able to roll back specially if that failure reason was that the database is gone offline, which is again if u r dealing with money that would be the worst dream from the business aspect

9- stored procedures still maintain a higher performance for a simple reason "less round trips"

if u hard coded sql statements in ur DAL this will means lots and lots of round trips between you sql server and your application server when ever a user clicks a button on ur asp.net pages

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 4:44 PM by Petey

Ted,

Why do you think it is a poorly written article? I think it touches on valid points and is in-line with my experience. SPs are teh suck.

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 6:56 PM by Tim

Your security argument is very weak. The security benefits of stored procedures lie in not constructing a SQL string, in passing parameters to a procedure. SQL injection attacks are possible because people don't check their inputs when building a SQL statement. And in your argument for a weakness in stored procedures you do exactly that: dynamically build a SQL string AND don't check the inputs.

Proving that a secure technique can be used insecurely doesn't prove that it's insecure.

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 7:57 PM by RyanOC

They have helped me out a lot. I work with people that are SQL experts but I wouldn't want them in the source code recompiling. They can update, create and help me with a lot of things this way.

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 8:13 PM by Ben Taylor

Ok...let's not say SPs are great just because we have been drinking the kulade too long.

Here is why you should use them:

1) You wish to isolate users from the physical structure of your database. You need performance that views will not provide in dynamic SQL.

2) You have complicated code that is best broken down using temp tables for performance.

3) You want the pre-cache query plan only stored procedures provide effeciently. Dynamic SQL will cache a plan; but, it is dropped from Cache earlier when RAM is needed than SP Cache. It also takes more to determine if a Cached plan is useable because the query has to be compared to determine if it is the same as an already executed query. For simple CRUD queries, who cares. But if you have a database that really does something...

Here is why you should not use them:

1) To protect from SQL Injection. There are methods to make SQL Calls that do not require an SP yet provide the same protection for SQL Injection as an SP.

2) You wish to have your data access layer be More intellegent. You require a data access layer to be intimately aware of your data structure.

3) True transporability is required, and you have a middle tier that is capable of working with multiple SQL syntax.

There are many more bullets that could be added to either side of the arguement. The answer is, it depends on your situation...AS ALWAYS. This arguement is like saying everything should be written using a Factory Pattern.

# re: The Myth of Stored Procedures Preference

Monday, January 07, 2008 8:43 PM by MR

These are some of the worst arguments against the use of stored procedures I've ever read. Good grief.

# Vinny Carpenter’s blog » Daily del.icio.us for January 7th

Pingback from  Vinny Carpenter’s blog » Daily del.icio.us for January 7th

# re: The Myth of Stored Procedures Preference

Tuesday, January 08, 2008 1:34 PM by Adel Khalil

@tony petruzzi

if you used SPs you will ran into problem of SQL compatability, that's why SPs aren't portable, if you have choosen to work with Dynamic SQL, it will work on both env without any problems, but views can solve this as well.

# re: The Myth of Stored Procedures Preference

Tuesday, January 08, 2008 1:37 PM by Adel Khalil

@Timestamp

Exactly, both using SPs or Dynamic SQL, doesn't protect you from SQL Injection out of the box you have to use SQL parameters for that, the myth here is that SPs advocates  always says that SPs are SQL Injection proof and Dynamic SQL isn't here I'm showing that it doesn't relate to either technique but it's all about the developer

# re: The Myth of Stored Procedures Preference

Tuesday, January 08, 2008 2:10 PM by Adel Khalil

@Horis Dinglebery

i think you misunderstood what i was saying, i only promote the use of Parametrized-Queries,

The execution plan is cached both SPs and Parametrized-Queries, and guess what even queries with no parameter is parametrized and cached (SQL Server 2000).

but any way there is ton of reasons why you should only use Parametrized-Queries.

# re: The Myth of Stored Procedures Preference

Tuesday, January 08, 2008 2:12 PM by Adel Khalil

@Duncan

 it doesn't out-perform this much review Eric Wise's experiment, but to isolate specific code as in your example is a good idea, but bear in mind that using SPs mostly for abstraction isn't the way to go.

# re: The Myth of Stored Procedures Preference

Tuesday, January 08, 2008 2:29 PM by Adel Khalil

@Fady Anwar

>>> 5- "It's impossible to cover every single scenario" who said so? the same way you can make ur ad-hoc sql statements dynamic you can make stored procedures dynamic too, stored procedures have parameters for a reason

no you can't really think of the following scenario, what if you want to create update procedures for 10 tables each contain 10 columns for example, think of the combination for each column for the expression and the filter parts of the SQL Statment, if you need to prepare a SPs to update every single filed you will have 100 SPs not to mention there is still possibilities that you need difference fields on the WHERE clauses, good luck with that.

>>>>6- "string s = "EXEC sp_GetCustomerByEmail '" + txtEmailAddress.Text + "'";" i've never seen a developer stupid enough to do that :D

no there are, check www.asp.net forums

>>>>>7- you can't use transactions in hard coded sql statements the same way like stored procedures, u will have to handle these transactions ur self from ur DAL code, which is something i don't recommend

i don't see why i can't use transaction from Dynamic SQL.

# re: The Myth of Stored Procedures Preference

Tuesday, January 08, 2008 2:33 PM by Adel Khalil

@Tim,

providing the SP executing example is to show that i can use SPs and STILL BE OPEN TO SQL injection.

# re: The Myth of Stored Procedures Preference

Wednesday, January 09, 2008 6:12 PM by Ellery Familia

This article is full of holes... the most obvious ones is the attack on SP security. If you implement SPs correctly, then this would not be a problem:

string s = "EXEC sp_GetCustomerByEmail '" + txtEmailAddress.Text + "'";

Even if txtEmailAddress.Text = "'; DROP DATABASE myDB", since the user executing that stored procedure only has rights to executing stored procedures and not random SQL... CERTAINLY not DROP rights, the Sql injection problem you mention does not exists, unless an incompetent developer creates it.

In answer to your "performance" attack, Yes.. SQL Server caches execution plans for random SQL, but not to the level that it does for SPs. SQL Server stores several execution plans for SPs and selects the best plan based on parameters, all tables and joins involved, as well as other costs calculations such as I/O and pages involved.

If you're using random SQL, you SQL statements must match perfectly in order for an execution plan to be reused.

# re: The Myth of Stored Procedures Preference

Friday, January 11, 2008 1:23 PM by James Gregory

People seem to be ignorant of, or simply forget, that using dynamic sql does note equate to injecting values. Dynamic SQL can be, and should be, used with parameters just as you do stored procedures.

If you use parameterised sql then your cached plans are just as efficient as stored procedures.

# re: The Myth of Stored Procedures Preference

Sunday, January 20, 2008 10:36 PM by Adel Khalil

More good news that even if you haven't used params ( which you should never ) SQL Server will cach the execution plan.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 4 and 6 and type the answer here: