Let’s look at the possibilities:
- reduce network traffic
- improve database performance
- create scheduled tasks
- decrease risks
- create routines for processing
In my conception, the main motivation would be number 5. The main idea of Stored Procedures is to encapsulate behavior together with the database, when for any reason, it is not desirable that these are modeled in the application(s) client(s). This is the main reason (but not the only one) that motivates the use of Stored Procedures.
Reasons 3 and 4 are something derived from 5. Reasons 1 and 2, although valid, are optimizations that should only be created as special cases to solve specific problems.
A real example of the use of Stored Procedure would be to create any CRUD procedure, mainly involve multiple tables and place it directly in the database. The advantage of doing this is that access to data is encapsulated and customer complexity is reduced. In the case of item 3, you can even eliminate the need for a client program.
The downside is that changes in requirements and business rules require changes in the database, which tend to be more costly and difficult than changes in customer applications.
Another advantage is that if there are multiple client programs, with the use of Stored Procedures, you centralize the business rules all in one place. However, this advantage is considered outdated today. You can use a web service (SOAP, REST or other) that holds exclusive access to the database and all other client applications would only access it through this web service. In this way, the web service would be responsible for centralizing the business rules, and no longer the database.
Cases 1 and 2 can also be advantageous in those circumstances where you make a SELECT
giant with multiple tables and the client application uses this to group, total or do some other processing. You can reduce network traffic between the database server and the client application, and thereby improve performance if these operations are performed directly by the DBMS through Stored Procedures.
Reduce network traffic? How to call the SP (small text) instead of passing a query (larger text)? The difference in general is negligible. 2) It depends on the DBMS but in general the only thing and save a little CPU pq the SP is already compiled (tb can pioerar with the sniffing paramet, etc again many considerations. 3???? 4) SQL Inject? 5) To? Pq?
– jean
An exmeplo is given by the devmedia himself "When we have several applications written in different languages, or run on different platforms, but perform the same function (in BD)." But I think it would be more useful to study each of the above-mentioned topics to examine each one and then come up with more accurate doubts
– jean
Things to study p/vc: Parameter Sniffing, SQL Inject Attack, Pq remove business rules from the bank layer to the BL layer
– jean
@jean consider the question too broad? or little elaborate? or outside the scope? could give some suggestion so that I can improve it?
– Jonathas B. C.
I found questions that do not add much. You should go deeper into the pq of the use (or not) of Sps. I gave up to two quick examples but there are several considerations and for smaller ones that are too extensive for one question. It would be nice to study and come with more punctual doubts (of course it is only a suggestion)
– jean
@jean I appreciate the suggestion, I will follow it, really wanted to be able to read about some experience in the practice of someone in the community, I imagined that this practical experience, would be well seen by all, but by the down votes I was wrong =(
– Jonathas B. C.
Do not be discouraged the community is great. It is that you gave the impression that you did not research well before asking and there are people who donwvote for it. In the OR it’s the same
– jean