Are there any guidelines on when you should and should not write a long complicated 2000+ line stored procedure?
In my specific case this stored procedure contains a lot of if/then, case, goto and branching statements. It works by constructing SQL queries depending on inputs and results of queries and uses the execute statement to run the constructed queries. It can execute several constructed queries in one call and uses results of those queries to construct other queries to run.
It is pretty messy and hard to understand. Debugging is tough, the only way to know what is going on is to step through a call to see what it's doing. There barely any exception handling or logging. Maintaining it is a pain. In fact, no one really knows what it does or how it was created and if we had to make modifications to it we would have to take a "cross your fingers and hope for the best" approach. But, I think it was done this way for performance reasons.
This procedure is used by many applications. The only other way I can think to do something like this is through a web service. It would probably be comparable in complexity, but a lot easier to understand. However, it would probably be multiple times slower as it would still have to make several calls to the database for 1 request.
So, my question(s) are, how do we decide when and when not to write long stored procedures?
Is there something I'm missing or do we just have to put up with our monstrous stored procedure?
Are there ways to structure and break down stored procedures into smaller components so they are easy to understand?
Will a stored procedure always be faster than anything else and the right choice when you need to make many calls to the database?
A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it.
You should never write such a monstrosity.... any function/procedure should be no more than two pages long - and that's taking a very liberal stand on this matter....
A stored procedure provides an important layer of security between the user interface and the database. It supports security through data access controls because end users may enter or change data, but do not write procedures.
Stored procedures promote bad development practices, in particular they require you to violate DRY (Don't Repeat Yourself), since you have to type out the list of fields in your database table half a dozen times or more at least. This is a massive pain if you need to add a single column to your database table.
Sounds like you need to change the way you write and maintain this stored procedure.
A stored procedure is created from a SQL query. Maybe you can write an application/script to generate the SQL to create/update your stored procedure.
Doing this would give you all the benefits of a re-usable stored procedure (the benefits you already have), but it would make maintenance of the stored procedure easier.
By having a 2000+ line stored procedure, you are basically locking yourself in as the only person who can do the job. This means you are stuck where you are and cannot move forward (no job promotion, no assignment to a new project, etc).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With