r/FullStack • • 19d ago

Question Why implement procedural programming inside an RDBMS (MariaDB) instead of handling all business logic in backend application code?

So I have just seen the procedural programming language in the MariaDB RDBMS. So my question is, why in a relational database management system would you need SQL to also have the power of a programming language? Why in the layer of the database server would you need that? Can that be achieved with your backend server logic programming language, whether it be JavaScript, PHP, Python, Rust, Go, etc.? Why is there a programming language, why SQL is a programming language more so than just for querying and interacting with a database or databases? How does that fit into the full picture of full stack web development?

Edit: Thank you everyone for the thoughtful responses, technical insights, and feedback. All of this input is extremely helpful much appreciated!

17 Upvotes

41 comments sorted by

View all comments

1

u/virgilreality 17d ago

There are functional and strategic reasons for putting code into a Stored Procedure (SP). The ones I can think of just off the top of my head:

  • Standardized and centralized data retrieval and updating.
  • Consistent meaning behind data retrieval. Subtle, but necessary.
  • Combining logically related functionality on a server that is optimized for it.
  • The SP

Please understand that nobody wants to have code running on a DB server. As a matter of fact, procedural code here is usually an indicator that you are approaching the problem wrong (but not always). As technology advances, there may emerge other ways to accomplish this...but sometimes you have to process things ReBAR (Row By Agonizing Row). The best choice is to try to think algebraically, where you are looking to join tables (or results) with minimal constraints and as little sorting as possible.

There are practical human reasons as well. I inherited a system with an SP that typically took between one and five minutes to execute. This was only acceptable because it was only run monthly. It was very, very complex in its intent, concept, and execution. Looking at the three-page, scrollable SQL was painful enough on its own, but it also hid details about the business reasons for data selection (or failure to). Answering questions about "why was/wasn't X included in the results" required an hour of picking apart the query to run its parts individually to identify the errant data and prove that it functioned as designed.

I rewrote the SP with the intent of improving traceability. I separated the massive SQL into smaller, sequentially important pieces that essentially did just ONE business function ("get the records for this time frame") and dropped that result into a temporary table. The next query did just ONE thing, but joined with the results of the previous query as a constraint, and dropped it into another temporary table. Twenty-plus segments like that later, I had my results, and I had temp tables that could be examined for the results of those individual constraints. The sequence and purpose was easily understandable, and I wrote queries to investigate those temp tables to see where (Item X) was included or excluded.

(Note: the temp tables got emptied at the beginning of the SP, so they only held data from the most recent run.)

Additionally...it consistently ran in under three seconds. I had broken it down into a cascading series of result sets that joined algebraically, which the DBMS is optimized for.

There's probably a better way to do this now, but that was my best result ever.