To use Stored Procedures or not.

Published Wed, Jun 14 2006 21:14 | William

A while ago, Frans wrote   Stored Procedures are Bad, M'Kay .  None other than Adam Machanic  chimed in, arguing essentially the opposite    .The always insightful Anatoly Lubarsky gives his take on it and well, my head is spinning.  I was Mr. Stored Procedure until Frans schooled me on it.  I still like them and I still use them, but not nearly as much as I used to and basically, Frans cured me of my superstitions with respect to stored procedures (and yes, many people out there use them because of superstition more than through reasoned analysis - although I'm not saying that's the case for everyone).

The only hole I could never reconcile with Frans' argument was with security.  Injection attacks can be addressed through parameterization so the old cannard about injection and dynamic sql isn't a neccsary problem. However even with role based security, it has always seemed to me that you can grant users permissions to procs (through roles ideally) and nothing else. This means that other than execute existing procs, they can't do anything.  AFAIK, this isn't possible with dynamic sql statements. to that end, I've thought that stored procs were ultimately more secure.  HOWEVER, how many people that use procs actually restrict stuff like this correctly?  I've seen a lot more that don't than that do.  Many people act like sticking it in a proc makes it perfectly secure and that sure as hell isn't the case.

The main reason I've liked them is b/c of flexibility.  I can change a lot of things behind the scenes without needed to recompile (more particularly, redeploy).  Granted tables don't change so frequently this is a big deal, but being able to change the user interface and all people to access 'new' functionality, driven totally by stored procs and db entries has always been pretty cool to me.  I buy the encapsulation argument from Adam.    I really like Anatoly 's argument about transactions since I have always been uncomfortable firing transactions client side.

The performance issue isn't one that I've looked into enough to have an opinion on. I've heard advocates from both sides claim that one is faster or that there's no difference.  I've tried testing it a few times and couldn't get conclusive results (and because I was lazy, I just figured I'd take someone else's word for it). Problem is I've heard it too many ways to be sure. This would be a great test to write - and will give me something to do tonight.

Interestingly enough, the Points of view here are all from people that are among the brightest cats out there.  If you've read or followed Adam, Frans or Anatoly, then you no doubt respect them b/c they are all bright as hell.  And none of them run their mouths about stuff that they are unsure of.  And Frans lives in a country with a gay necrophiliac duck (no, I'm not making fun of the Netherlands, I just love talking about the gay penguins in the Berlin zoo or the Dutch duck whenever i can).  Plus, it's a proven fact that you need a Ph.D to pronounce the city where Frans lives so you have to be smart just to live there [It took me about 20 attempts before I could pronounce his town correctly] Seriously though, all three of them are heavy hitters and raise some good points. And the most important thing about this debate is that it's happening. Doing things for superstitious reasons is silly and something we've all (at least I know I have) have been guilty of.  I'd really like to see how this discussion goes b/c they are two different perspectives for sure, although they are touching upon different areas - (unlike Frans first post where he just blew holes in Rob's argument [no disrespect to Rob on that one - it's just that Frans really did a thorough job addressing each point and countering it]).

So what do you think?  Did they miss anything?  Maybe Bill , Sahil, or the ADO.NET Guys will chime in ;-)

Comments

# Anatoly Lubarsky said on June 14, 2006 4:59 PM:

Thanks for the nice words, Bill.

Just another point, which is not so obvious.
Heavy systems 24x7. It is far easier to deploy using stored procedures, since you have to run something (?) to change the data which was inserted/updated/deleted during deployment to fit the new functionality.
Since the deployment is supposed to change only the old data.

# William said on June 14, 2006 7:30 PM:

Great point Anatoly.  That's actually something I do quite a bit, often making the change while I'm on the phone with the customer which has been huge. When people can hit refresh on a browser and see their request in place, it's huge.  This is in large part related to the nature of web apps,but the point is still very relevant.  I'll definitely add it to the list.

# RTA said on June 16, 2006 8:55 PM:

I tend to think of SPs as one would think of a separation of concerns. SPs have evolved a great deal but I prefer SPs for some of the following reasons:

 1) Consolidation of concerns. Let someone better at tuning SPs, i.e. a DBA than myself. I'm sorry I can't be an expert on everything.
 2) Separation of concerns. I prefer to separate issues that are mutable. It is easier to allow SPs to be converted from SQL to Oracle, to MySQL, to Sybase. I can use a provider model, strategy, etc. to address the DLL.
 3) Distribution of work. I would much prefer to tell the DB guys what I want. Yes, I'll tell of the data I want, come up with a schema, normalize tables, if needed; however, DB guys can work on that while I deal with my service.
 4) Performance. It depends on which Relational DB you are talking about. Oracle, Syabse, MySQL, etc. I don't think you can be narrow-minded and say SQL Server caches the execution plan without fully looking at the other RDBs. It all depends on your focus.

So there hasn't been enough to convince me that when I look at the world that I say SPs are BAD. Myoptic concerns lead to big explosions. IMHO

# William said on June 16, 2006 11:43 PM:

Great points on the different DB's RTA - much appreciated.

# Sahil Malik said on July 5, 2006 3:08 PM:

This topic is getting older than aunt martha's poochie. :/

Search

This Blog

Tags

Community

Archives

News

My other sites

Cool Stuff

Book Stuff

Security

ORM

Data Access

Funny Stuff

Compact Framework Stuff

Web Casts

My KnowledgeBase Articles

My MVP Profile

Design Patterns

Performance

Debugging

Remoting

My Fellow Authors

My Books

LINQ

Misc

Speech

Syndication

Email Notifications