views:

1312

answers:

19

Is there such a thing as too many stored procedures?

I know there is not a limit to the number you can have but is this any performance or architectural reason not to create hundreds, thousands??

A: 

i think as long as you name them uspXXX and not spXXX the lookup by name will be direct, so no downside - though you might wonder about generalizing the procs if you have thousands...

Steven A. Lowe
+2  A: 

Do you mean besides the fact that you have to maintain all of them when you make a change to the database? That's the big reason for me. It's better to have fewer ones that can handle many scenarios than hundreds that can only handle one.

Kevin
Totally agree - maintenance of thousands of SPs becomes unwieldy when schema changes happen.
stephbu
I second this thought, I would hate to main thousands of store procedures.
David Basarab
Isn't there the risk of ending up with knots of business logic in your stored procedures or hard to maintain "spaghetti" code if you have a few multi-purpose sprocs though? Targeted stored procedures that each perform one task only are far easier to maintain..
Rob
Absolutely Rob! There is definately a balance that's needed. Some tasks need to be in their own procedure. I would rather return an extra field on a select statement than create two procedures.
Kevin
I have turned down ERP work for that specific reason; thousands of stored procedures and triggers that would take me forever to learn. They're often even pretty necessary, but I still don't want to parse all that as a new developer to the project!
Grank
+2  A: 

To me the biggest limitation to that hundreds or thousands store procedure is maintainability. Even though that is not a direct performance hit, it should be a consideration. That is an architectural stand point, you have to plan not just for the initial development of the application, but future changes and maintenance.

That being said you should design/create as many as your application requires. Although with Hibernate, NHibernate, .NET LINQ I would try to keep as much store procedures logic in the code, and only put it in the database when speed is a factor.

David Basarab
+2  A: 

I would just mention that with all such things maintenance becomes an issue. Who knows what they all are and what purpose they serve? What happens years down the way when they are just artifacts of a legacy system that no one can recall what they are for but are scared to mess with?

I think the main thing is the question of not is it possible, but should it be done. Thats just one thought anyway.

Arthur Thomas
A: 

As others have said, it really comes down to the management aspect. After a while, it turns into finding the proverbial needle in the haystack. That's even more so when there's poor naming standards in place...

Kevin Fairchild
+2  A: 

My question is why aren't you using some sort of middleware platform if you're talking about hundreds or thousands of stored procedures?

Call me old fashioned but I thought the database should just hold the data and the program(s) should be the one making the business logic decisions.

MattC
Thats not exactly true. There are many things that can be offloaded to the database that are actually better done there. Some obvious stuff might be higher level constraint checks (something that must be true no matter how many apps access the tables).
Arthur Thomas
I wish i could upvote comments :)
Constantin
A: 

No, not that I know of. However, you should have a good naming convention for them. For example, starting them with usp_ is something that a lot of poeple like to do (fastr than starting with sp_).

Maybe your reporting sprocs should be usp_reporting_, and your bussiness objects should be usp_bussiness_, etc. As long s you can manage them, there shouldn't be a problem with having a lot of them.

If they get too big you might want to split the database up into multiple databases. This might make more logical sense and can help with database size and such.

Charles Graham
+1  A: 

If you have a lot of stored procedures, you'll find you are tying yourself to one database - some of them may not easily be transferred.

Tying yourself to one database isn't good design.

Moreover, if you have business logic on the database and in a business layer then maintenance becomes a problem.

Joe R
+2  A: 

in Oracle database you can use Packages to group related procedures together.

a few of those and your namespace would free up.

ShoeLace
+2  A: 

Hell yes, you can have too many. I worked on a system a couple of year ago that had something like 10,000 procs. It was insane. The people who wrote the system didn't really know how to program but they did know how to right badly structured procs, so they put almost all of the application logic in the procs. Some of the procs ran for thousands of line. Managing the mess was a nightmare.

Too many (and I can't draw you a specific line in the sand) is probably an indicator of poor design. Besides, as others have pointed out, there are better ways to achieve a granular database interface without resorting to massive numbers of procs.

TheMadHungarian
+2  A: 

I have just under 200 in a commercial SQL Server 2005 product of mine, which is likely to increase by about another 10-20 in the next few days for some new reports.

Where possible I write 'subroutine' sprocs, so that anytime I find myself putting 3 or 4 identical statements together in more than a couple of sprocs, it's time to turn those few statements into a subroutine, if you see what I mean.

I don't tend to use the sprocs to perform all the business logic as such, I just prefer to have sprocs doing anything that could be seen as 'transactional' - so where as my client code (in Delphi but whatever) might do the odd insert or update itself, as soon as something requires a couple of things to be updated or inserted 'at once', it's time for a sproc.

I have a simple, crude naming convention to assist in the readability (and in maintenance!)

The product's code name is 'Rachel', so we have

RP_whatever   - these are general sprocs that update/insert data, 
              - or return small result sets

RXP_whatever  - these are subroutine sprocs that server as 'functions' 
              - or workers to the RP_ type procs

REP_whatever  - these are sprocs that simply act as glorified views almost
              - they don't alter data, they return potentially complex result sets
              - for reporting purposes, etc

XX_whatever   - these are internal test/development/maintenance sprocs 
              - that the client application (or the local dba) would not normally use

the naming is arbitrary, it's just to help distinguish from the sp_ prefix that SQL Server uses.

I guess if I found I had 400-500 sprocs in the database I might become concerned, but a few hundred isn't a problem at all as long as you have a system for identifying what kind of activity each sproc is responsible for. I'd rather chase down schema changes in a few hundred sprocs (where the SQL Server tools can help you find dependencies etc) than try to chase down schema changes in my high-level programming language.

robsoft
+7  A: 

Yes you can.

If you have more than zero, you have too many

Orion Edwards
So, hows Linq2SQL with the Geography datatype? SPs are pretty handy with it.
Meff
I am upvoting you twice for that. Well done.
Ryan
+1  A: 

Too much of any particular thing probably means you're doing it wrong. Too many config files, too many buttns on the screen ...

Can't speak for other RDBMSs but an Oracle application that uses PL/SQL should be logically bundling procedures and functions into packages to prevent cascading invalidations and manage code better.

David Aldridge
A: 

Yep, you can have toooooo many stored procedures.

Naming conventions can really help with this. For instance, if you have a naming convention where you always have the table name and InsUpd or Get, it's pretty easy to find the right stored procedure when you need it. If you don't have some kind of standard naming convention it would be really easy to come up with two (or more) procedures that do almost exactly the same thing.

It would be easy to find a couple of the following without having a standardized naming convention... usp_GetCustomersOrder, CustomerOrderGet_sp, GetOrdersByCustomer_sp, spOrderDetailFetch, GetCustOrderInfo, etc.

Another thing that starts happening when you have a lot of stored procedures is that you'll have some stored procedures that are never used and some that are just rarely used... If you don't have some way of tracking stored procedure usage you'll either end up with a lot of unused procedures... or worse, getting rid of one you think is never used and finding out afterwards that it's only used once a year or once a quarter. :(

Jeff

Jeff
A: 

You have to put all that code somewhere.
If you are going to have stored procedures at all (I personally do favour them, but many do not), then you might as well use them as much as you need to. At least all the database logic is in one place. The downside is that there isn't typically much structure to a bucket full of stored procs.

Your call! :)

AJ
But if you dynamically generate some of the code, you don't have as much to put!
MattMcKnight
A: 

Not while you have sane naming conventions, up-to-date documentation and enough unit tests to keep them organized.

Constantin
+1  A: 

I suppose the deeper question here is- where else should all of that code go? Views can be used to provide convenient access to data through standard SQL. More powerful application languages and libraries can be used to generate SQL. Stored procedures tend to obscure the nature of the data and introduce an unnecessary layer of abstraction, complexity, and maintenance to a system.

Given the power of object relational mapping tools to generate basic SQL, any system that is overly dependent on stored procedures is going to be less efficient to develop. With ORM, there is simply a lot less code to write.

MattMcKnight
A: 

My first big project was developed with an Object Relational Mapper that abstracted the database so we did everything object oriented and it was easier to mantain and fix bugs and it was especially easy to make changes since all the data access and business logic was C# code, however, when it came to do complex stuff the system felt slow so we had to work around those things or re-architect the project, especially when the ORM had to do complex joins in the database.

Im now working on a different company that has a motto of "our applications are just frontends of the database" and it has its advantages and disadvantages. We have really fast applications since all of the data operations are done on stored procedures, this is using mainly SQL Server 2005, but, when it comes to make a change, or a fix to the software its harder because you have to change both, the C# code and the SQL stored procedures so its like working twice, contrary to when I used an ORM, you dont have refactoring or strongly typed objects in sql, Management Studio and other tools help a lot but normally you spend more time going this way.

So I would say that deppending on the needs of the project, if you are not going to keep really complex business data than maybe you could even avoid using stored procedures at all and use an ORM that makes developer life's easier. If you are concerned about performance and need to save all of the resources than you should use stored procedures, of course, the quantity deppends uppon the architecture that you design, so for exmaple if you always have stored procedures for CRUD operations you would need one for inserts, one for update, one for deletion, one for selecting and one for listing since these are the most common operations, if you have 100 business objects, multiply these 4 by that and you get 400 stored procedures just to manage the most basic operations on your objects so, yes, they could be too many.

Gustavo Rubio
A: 

To me, using lots of stored procedures seems to lead you toward something equivalent to the PHP API. Thousands of global functions which may have nothing to do with eachother all grouped together. The only way to relate between them is to have some naming convention where you prefix each function with a module name, similar to the mysql_ functions in PHP. I think this is very hard to maintain, and very hard to to keep everything consistent. I think stored procedures work well for things that really need to take place on the server. A stored procedure for a simple select query, or even a select query with a join is probably going to far. Only use stored procedures where you actually require advanced logic to be handled on the database server.

Kibbee