r/FullStack • • 16d 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

6

u/traplords8n 16d ago

Calculations are way more efficient in SQL rather than php, node, etc.

2

u/Ronin-s_Spirit 16d ago

You sound disingenuous, it's only faster if it prevents multiple round trips between the program and the DB.

3

u/Available_Safe1818 16d ago

It didn't sound like a disingenuous response to me. They might just have no idea.

1

u/MilkEnvironmental106 15d ago

There is also the cost of transporting large amounts of data between a dB and the backend node. Queries on large data usually run much better on the dB where it has access to indexes and metadata and it avoids the network trip completely. It really just depends on whether it's something the query interface of your dB does well.

1

u/Ronin-s_Spirit 15d ago

I don't know what you mean, you still have to return the data to your main logic program unless you've made the strange decision of letting the DB send data straight to the user.

I'm assuming the DB lives on another server, because otherwise there are almost zero benefits to running code inside the DB.

1

u/MilkEnvironmental106 15d ago

I'm talking about large aggregation queries and sending the entire dataset and processing Vs aggregating on the server and sending the aggregated response.

1

u/BigFeeder 14d ago

No application that works beyond couple of hundred database rows aggregates data on server side

1

u/MilkEnvironmental106 14d ago

Okay, well applications that work on less than a couple hundred of rows are small fish in a very big pond...

1

u/jshine13371 15d ago

it's only faster if it prevents multiple round trips between the program and the DB.

Not true. But the person you replied to should've said "can" be faster, as it depends on a couple of factors:

  1. Set operations are designed to operate on data and therefore are typically physically more efficient than procedural operations.
  2. My first point can be emulated in procedural languages but the likelihood of the average developer always doing it as optimally as the potential that the database engine is designed for is less likely.

1

u/Ronin-s_Spirit 15d ago

define "set operations".

1

u/jshine13371 15d ago

Are you genuinely curious?

1

u/Ronin-s_Spirit 15d ago

I don't want to misunderstand.

1

u/jshine13371 14d ago edited 14d ago

A set-based operation is one designed to process a collection of data as opposed to a procedural operation that typically operates on a single instance of data (e.g. a scalar value or instance of a data object like a record).

Algorithmically speaking, it's based on set theory which is mathematically optimal for working with data and the logical data structures used to store that data. Physically speaking, it's implemented as an abstraction between the developer and the physical layer via the logical layer as way to remove imperfect human decision on how to physically process the data and instead let the database engine make that decision based on the aforementioned set theory algorithms.

In short, the biggest distinction between database layer code and application layer code is with databases you tell them what you want and the database engine figures out the most reasonably optimal way to physically serve you the results. In the application layer, the code says exactly how you want the physical processing to occur, which is often imperfect and doesn't consider the array of factors that affect the performance of physically processing data, mainly the statistics of that data. E.g. an inner loop between two collections isn't always the most performant way to compare those two collections, and other times a hash match isn't either. But the multitude of factors that determine which would be better for a given set of data are mostly accounted for in a database engine and are dynamically applied, for the same set of code, based on the specific data being operated on.

1

u/Ronin-s_Spirit 14d ago

I don't know how the fuck it would do that. You're just giving it instructions like "select column from table", this should be the same filtering every time that can be easily codified instead of using sql.

1

u/jshine13371 13d ago edited 13d ago

That's quite a response lol...

this should be the same filtering every time that can be easily codified instead of using sql.

Nope.

For the same exact use case / end goal, the way to find, filter, re-shape, join, and other operations on the data, will vary in performance depending on the data (and it's statistical qualities) itself, therefore requiring different physical operations as the data varies for that same end goal. In application code, you could write very complex code (basically an algorithm) to consider most of those factors every time you process data, or you can leverage a database engine that already has the algorithms built in and abstracts that part with SQL which allows the developer to tell the engine what it wants and let's the engine decide the best way to get it based on those algorithms.

E.g. to join two collections together, if at least one of the collections has a small number of rows, and especially if the larger collection is sorted, typically an inner loop (aka nested loop join) is the most efficient physical join operation. But if both collections are large and unsorted then hashing both collections and comparing the hashes (aka hash join) is typically more efficient.

Aside from size and sorting, cardinality of specific values being predicated on, and the type of predicate operation, among other factors that determine when parallelization and / or batch processing would be more efficient than serial and row processing come into play, which are also dynamically dependent not only on the data statistics but the availability of resources on the machine in the moment the code is executing. There is an uncountably large combination of outcomes based on these factors and more, which the average developer doesn't consider and would be unreasonable to have to think about every time you had to process data in any way.

That's why abstracting away that part to an engine that's algorithmically optimized to consider all of those factors and make those decisions even for the same piece of code, dynamically as the data varies, is ideal. Application layer code is like your daily driver car, it can do anything, jack of all trades. But database layer code is like a racecar. Not great for driving to work with or for saving money on gas, but best at its specific job it's designed for, data problems.

This is a great article that touches on a few of the physical operators that only one of the logical operators (JOINs) in SQL represents and abstracts away.

1

u/phil-99 11d ago

> Calculations are way more efficient in SQL rather than php, node, etc.

“Calculations” are not necessarily quicker in any system than any other.

Some operations on large data sets may be more efficiently handled in the DB than in an app that has to build a query execute a query get the results process the results and send the results back to the database.

But as always with a DBA answer: it depends.

And the details matter so much that making definitive statements like this shows a lack of expertise but an abundance of confidence.