What's the point of a stored procedure?

前端 未结 12 1025
逝去的感伤
逝去的感伤 2021-01-21 12:13

Are they useful for anything outside of a database administrator? If I understand them correctly it\'s merely queries that can be saved directly into MySQL, so it\'d be useless

12条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 12:22

    Here are two good, simple advantages not covered in the other answers:

    • Security - parameterized stored procedures are safer than contencating strings for SQL (Google SQL Injection for about a million documents on this) However, parameterized queries are also good for this if your language supports them
    • Simplification of Maintenance - It's a heck of a lot easier to update a stored procedure than to recompile code and re-deploy. In my 15 years of development I've learned this the hard way. If there's a chance the query might change, put it in a stored proc. It's SOOO much easier to work with than having to recompile and redeploy code.

    Added

    They also reduce network chatter. If you have a lot of complex wueries to run, you can have them all in one stored procedure, and your app only needs to make one call to do all the work.

    Also, in most platforms, stored procedures have performance benefits. In SQL Server, for example, the Database Engine optimizes and saves the executio plan to speed things up. these links also answer your question:

    http://blog.sqlauthority.com/2007/04/13/sql-server-stored-procedures-advantages-and-best-advantage/

    http://searchsqlserver.techtarget.com/news/1052737/Why-use-stored-procedures

    And I can't take credit for this answer, but I think this quote is a good point, even though I consider myself to be pretty skilled on both sides of the equation - there is something to be said for specialized knowledge.

    Advantage 4: Stored procedures are usually written by database developers/administrators. Persons holding these roles are usually more experienced in writing efficient queries and SQL statements. This frees the GUI application developers to utilize their skills on the functional and graphical presentation pieces of the application. If you have your people performing the tasks to which they are best suited, then you will ultimately produce a better overall application.

提交回复
热议问题