What are the benefits of creating Stored Procedures in SQL and MySQL?

前端 未结 4 1391
遥遥无期
遥遥无期 2020-12-29 23:52

I have a theoretical question.

I can\'t see any difference between declaring a function within a PHP file and creating a stored procedure in a dat

4条回答
  •  天涯浪人
    2020-12-30 00:37

    As most of the guys already explained it, but still i would try to reiterate in my own way

    Stored Procedures :
    Logic resides in the database.
    Lets say some query which we need to execute, then we can do that either by :

    • Sending the query to DataBase server from client, where it will be parsed, compiled and then executed.
    • The other way is stationing the query at DataBase server and create an aliasing for the query, which client will use to send the request to database server and when recieved at server it will be executed.

      So we have :
      Client ----------------------------------------------------------> Server

      Conventional :
      Query created @Client ---------- then propagate to Server ----------Query : Reached server : Parse, Compiled , execute.

      Stored Procedures :
      Alias is Created, used by Client----------------then propogate to Server-------- Alias reached at Server : Parse,Compiled, Cached (for the first Time)
      Next time same alias comes up, execute the query executable directly.

      Advantages :

    • Reduce Network Traffic : If client is sending a big query, and may be using the same query very frequently then every bit of the query is send to the network and hence which may increase the network traffic and unnecessary increase the network usage.

    • Faster Query Execution : Since stored procedures are Parsed, Compiled at once, and the executable is cached in the Database. Therefore if same query is repeated multiple times then Database directly executes the executable and hence Time is saved in Parse,Compile etc. This is good if query is used frequently. If query is not used frequently, then it might not be good, because storing cached executable takes space, why to put Load on Database unnecessarily.

    • Modular : If multiple applications wants to use the same query, then with traditional way you are duplicating code unnecessarily at applications, the best way is to put the code close to Database, this way duplication can be alleviated easily.

    • Security: Stored procedures are also developed, keeping in mind about Authorization(means who is privileged to run the query and who is not).So for a specific user you can grant permissions, to others you as DBA can revoke the permission. So its a good way as a point wrt to DBAs a DBA you can know who are right persons to get the access.But such things are not that popular now, you can design your Application Database such that only authorized person can access it and not all. So if you have only Security/Authorization as the point to use Stored Procedures instead of Conventional way of doing things, then Stored procedure might not be appropriate.

提交回复
热议问题