What are real and practical examples of using Stored Procedures?

2

While attending an interview for the developer position, I come across a question related to Stored Procedure SP ) and at first I have theoretical knowledge on the subject, but I have never come across a practical example in the use of SP and I have been unable to set a good example in this situation for lack of experience on the subject.

Given that conceptually SP serves for example, for:

  
  • Reduce network traffic
  •   
  • improve database performance
  •   
  • create scheduled tasks
  •   
  • reduce risk
  •   
  • create processing routines
  •   

    (source: devMedia )

    Someone with more experience, could share with a real, practical example and that can easily be used in a concise and objective answer on the subject?

        
    asked by anonymous 04.12.2017 / 19:31

    2 answers

    1

    Let's look at the possibilities:

      
  • Reduce network traffic
  •   
  • improve database performance
  •   
  • create scheduled tasks
  •   
  • reduce risk
  •   
  • create processing routines
  •   
    The main thrust of Stored Procedures is to encapsulate behavior along with the database when, for whatever reason, it is not desirable that these are modeled on the client application (s). This is the main (but not the only) reason for using Stored Procedures .

    Motives 3 and 4 are derived from 5. Motives 1 and 2, although valid, are optimizations that should only be created as special cases to solve specific problems.

    A real example of using Stored Procedure would be to create some CRUD procedure, especially if you wrap multiple tables and place it directly in the database. The advantage of doing this is that data access is encapsulated and client complexity is reduced. In case of item 3, you can even eliminate the need for a client program.

    The downside is that changes to requirements and business rules require changes to the database, which tend to be more costly and difficult than changes in client applications.

    Another advantage is that if you have multiple client programs, using Stored Procedures , you centralize the business rules all in one place. However, this advantage is considered to be surpassed today. You can use a web service (SOAP, REST or something else) that has 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, not the database anymore.

    Cases 1 and 2 can also be advantageous in those circumstances where you make a giant% s with multiple tables and the client application uses this to group, totalize, 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 using Stored Procedures . >     

    09.12.2017 / 20:32
    1

    Two examples:

  • Two applications a web service made in C # and a desktop application made in Delphi need to run a business rule implemented in the DB. For better maintenance and management it is better that both access the same SP instead of each one trying to implement a query. So qq change in this logic would be made by changing the SP instead of generating maintenance in two different systems. Note that the same result could be obtained if both applications could call a DLL that encapsulates these business rules (unless they run on different OS).

  • Performance. Some routines are actually much faster if implemented directly in the DB. Although I am in favor of keeping the business rules in the business rules layer has times that is inevitable. In this case the routine can be implemented as an SP, as the SP gets stored "compiled" in the DB there may be a performance gain if this routine is called many (thousands) times (per day / hour). Of course this depends on comparing to ad hoc queries, non-parameterized queries, etc. Even using SP there might be considerations about SQL Inject (security) and Parameter Sniffing (performance)

  • 04.12.2017 / 19:57