views:

205

answers:

4

I've inherited a web application which strictly uses stored procedures to do its job. I like the approach of making it impossible for frontend developers to break the database, but I've been tired of writing calls to SPs with plain SQLs and wanted to have something saner. While I've been looking for a decent ORM (for Perl in this case, but that isn't relevant for the question) with support for stored procedures, I've realized that ORM could be a direct contradiction to SPs.

My thinking is that SPs are, like the name already tells us, procedures, i.e. representatives of procedural Pascal-style of programming, in fact, that one web application looks exactly like Pascal on the SQL-Server side -- many functions, no real namespacing. Contrasting to that, we are trying to do most of our programming OOP-style (or functional, which is a yet another topic), so actually procedural SPs are not really a good fit for clean object hierarchies. At the same time, relational logic can be converted to objects cleanly (via an ORM), but not the procedural, which is probably why most ORMs don't support SPs very well (but I'm not an expert in that field). In some sense, SPs are ORMs.

So the two questions are:

  1. Am I right assuming we are better off using plain tables when running an ORM?
  2. Are there any "object-oriented stored procedures" on the market, building up from relational model? Clearly, there are object-oriented databases, but I'm interested in "ORM on the server side".
+7  A: 

"Am I right assuming we are better off using plain tables when running an ORM?"

Yes.

The RDBMS should be focused on persistent storage, and nothing more.

If you do this, you will find that you can -- easily -- build an access layer in your OO language. All the "front-end" developers must use the access layer and cannot break the database.

"object-oriented stored procedures?"

Oracle has some OO-like features of PL/SQL.

Don't waste any time on it. Focus on a clean separation between the persistence (in the RDBMS) and the application processing (not in the RDBMS).

Many, many people will send you hate mail saying things like "the vendor put all those features in there, that means you should use them" and "what's wrong with stored procedures?" and "a good DBA is better than a room full of front-end developers" etc. etc.

I don't know why folks claim stored procedures are "better", yet many system eventually reaches the wall where the stored procedures and triggers got so burdensome that it had to be rewritten.

I've never seen anyone complain that they had too much application software outside the database.

Continue to follow your thoughts here -- use ORM -- avoid stored procedures.

S.Lott
Agreed. Building an ORM with stored procedures is madness.
Paul McMillan
You're right about triggers; that's an easy way to grow complexity out of control. But stored procedures can work very well as a layer that keeps the database consistent, and enforces logging and security.
Andomar
@Andomar: An access layer will keep the database consistent, and enforce logging and security. Stored procedures are not easy or particularly good at these things. They seem handy to the DBA's. In the long run, however, they're brittle and hard to maintain. I've charged clients a lot of money to hang around while they *fail* (repeatedly) to simply find the source for critical stored procedure.
S.Lott
I think that it is important to note that SPs and ORM work well together when you have a product that you are releasing under multiple RDBMS backends. Syntax can vary (especially when trying to get performance boosts) and SPs for those specific instances can keep code maintainable by leaving it up to the server itself. Outside of this edge case, I'd agree with the author.
Kevin Peno
@Kevin Peno: In all the stored procedure cases I'm aware of, the stored procedure call syntax is different. I can't actually see how you can claim that SP syntax is somehow less variable than other SQL syntax. ORM makes the SQL invisible, eliminating any need for SP's for the purpose of avoiding SQL syntax issues. Please post some examples.
S.Lott
-1 Because this is by no means a closed issue, and there is significant disagreement and a lack of consensus about it. There are pros and cons on both sides, and so it's good to read as much as you can on both and form your own opinion.
Charles Bretana
@Charles Bretana: Since I've had to bail out a number of customers with fatally complex up stored procedures and triggers, and never had to bail out a customer with any ORM problems, I'm not sure that the stored procedures have any possible advantages. Since I've never seen anything but problems with stored procedures, I'm not sure how it's possible to claim there's any tradeoff. Your experience may be different, but mine is entirely negative. But it leads to a lot of billable hours fixing the problems.
S.Lott
I'm not surprised at your attitude. You are clearly an evangelist. And, typically, you're surprised that anyone else is not equally on-board. But there are numerous sources if you care to look around.. Not everyone is as enamored with this technology as it's evangelists are. Generally, the evangelists are those who are not equally competent on both sides of the code/Database divide. Not sure if that describes you or not, but that's been my experience.
Charles Bretana
@Charles Bretana: I'm not surprised that you have a different opinion. I'm trying to present facts. I used to be a full-time Oracle DBA (for years. I was a travelling performance problem-solver). I've also been an Ingres and a DB2 DBA. I think I have a balanced experience. I think that an ounce of prevention is better than paying me large numbers of dollars to cure the problems created by stored procedures.
S.Lott
As I said, that's not been my experience. Although I've seen plenty of what you are talking about, I seen the opposite as well. When Designed by competent developers I have found systems using Stored procs to be more maintainable, manageable, and extensible. Again, I'm not questioning your experience, just pointing out that a broad range of opinion exists on this topic, available to anyone who looks. In fact, I've also seen wher strict adherance to ORM by evangelists created a system with performance so bad the project failed. Not necessarily the fault of ORM, but again, experience can lie.
Charles Bretana
@S Lott, to followup, if you truly had a balanced experience, you would have a more balanced opinion. There is a place for ORM tools, They have their strengths, AND weaknesses. But because, like ALL tools, they have both strengths and weaknesses they have their place, where they can provide value, and there are places and scenarios where tjhey are NOT the best choice. A "balanced" experience would recognize both and not be blind (or unwilling to acknowledge) one side of the issue.
Charles Bretana
@Charles Bretana: I am providing the voice of experience. Not balance. These are facts. ORM tools are less brittle, easier to maintain and -- in the long run -- less expensive than stored procedures. That's as balanced as I can be, given the facts. The facts seem to be pretty clear to me. I've had to write plans to rescue companies from horrifying stored procedure nightmares. Those are the facts on which I'm basing my recommendation.
S.Lott
Well, I guess that, in a nutshell, is as good a definition of evangelism as I could ask for.
Charles Bretana
@Charles Bretana: I don't understand why it's so important to you to deprecating facts. My point is to present facts. The facts point in one -- and only one -- direction. I don't know why this is so bad. Nor do I know why you have no facts of your own. If you want to deprecate the facts, that's kind of silly. If you have some other facts, that would be less silly.
S.Lott
@s Lott, I don;t think I've EVER experienced an issue where the facts point in one and only one direction. In every issue related to anything real, (math being the exception as it it has nothing to do with reality) therer have ALWAYS been pros and cons, facts, which point in both directions. Exercising rational judgment means being able to acknowledge, evaluate, measure and weight BOTH sets of facts, in order to determine what direction to take. Evalgelists, otoh, are only capable of seeing one set of facts, either ignoring, or rationalizing any that do not support their position.
Charles Bretana
@Charles Bretana: Amazing. The earth "could" be flat? The facts aren't overwhelming? Gravity "could" be linear since it appears that way close to the surface of the earth? The facts aren't overwhelming? The earth "could" be the center of the solar system? The facts aren't overwhelming? Simply an amazing point of view. I'll have to see if it ever matches the reality I've observed.
S.Lott
+2  A: 

Most ORM tools (that I've used. I'm in the .NET world) provide a mechanism for using stored procedures. Because ORM tools (again, the ones I've used) like to select all columns by default so they're all loaded into object graphs, you generally have to write your SPROCs to select all columns that are part of your object graph. This is the only major oddity I've run into when using an ORM package.

There are usually ways to optimize your SPROC calls in ORM tools so they don't need to select all columns, however that's usually more advanced.

I'd say it's safe to do, but generally you'll only want/need to do it when you need to optimize something that would be slow through normal ORM methods.

Dan Herbert
A: 

In .NET, LINQ Data Class will generate a strongly typed class for a procedure. You call the procedure like:

foreach (var customer in db.GetCustomers())
    Console.WriteLine(customer.firstName);

Where GetCustomers() is a stored procedure in the database. But you can't update the returned customer and submit the changes to the database. An ORM can only do that with plain tables.

My experience is opposite to S.Lot's, a stored procedure layer is keeping my database consistent, clean and fast. I guess it depends on the size and complexity of the application, and how well you know SQL.

Andomar
+1  A: 

This issue does not have a clear cut black and white answer. There are numerous conflicting arguments on both sides, and I, (imho), do not as yet see a clear consensus emerging. For an opposing view, with rational arguments pro-con, see ORM - Vietnam of CS, or another ORM link.

Charles Bretana