Introduction to MySQL Stored Procedures
2014-01-14 12:47
274 查看
Definition of stored procedures
A stored procedure is a segment of declarative SQL statements stored inside the database catalog. A stored procedure can be invoked bytriggers, other stored procedures or applications such as Java, C#, PHP, etc.
A stored procedure that calls itself is known as a recursive stored procedure. Most database management system supports recursive stored procedures. However MySQL does not support it very well. You should check your version of
MySQL database before implementing recursive stored procedures in MySQL.
Stored Procedures in MySQL
MySQL is known as the most popular open source RDBMS which is widely used by both community and enterprise. However, during the first decade of its existence, it did not support stored procedures, stored functions,triggers and events. Since MySQL version 5.0, those features were added to MySQL database engine to make it more flexible and powerful.
MySQL stored procedures advantages
Typically stored procedures help increase the performance of the applications. Once created, stored procedures are compiled and stored in the database. However MySQL implements the stored procedures slightly different. MySQL stored procedures are compiledon demand. After compiling a stored procedure, MySQL puts it to a cache. And MySQL maintains its own stored procedure cache for every
single connection. If an application uses a stored procedure multiple times in a single connection, the compiled version is used, otherwise the stored procedure works like a query.
Stored procedures helps reduce the traffic between application and database server because instead of sending multiple lengthy SQL statements, the application has to send only name and parameters of the stored procedure.
Stored procedures are reusable and transparent to any applications. Stored procedures expose the database interface to all applications so that developers don’t have to develop functions that are already supported in stored procedures.
Stored procedures are secure. Database administrator can grant appropriate permissions to applications that access stored procedures in the database without giving any permission on the underlying database tables.
Besides those advantages, stored procedures have their own disadvantages, which you should be aware of before using the store procedures.
MySQL stored procedures disadvantages
If you use a lot of stored procedures, the memory usage of every connection that is using those stored procedures will increase substantially. In addition, if you overuse a large number of logical operations inside store procedures, the CPU usage will alsoincrease because database server is not well-designed for logical operations.
A constructs of stored procedures make it more difficult to develop stored procedures that have complicated business logic.
It is difficult to debug stored procedures. Only few database management systems allow you to debug stored procedures. Unfortunately, MySQL does not provide facilities for debugging stored procedures.
It is not easy to develop and maintain stored procedures. Developing and maintaining stored procedures are often required specialized skill set that not all application developers possess. This may lead to problems in both application development and maintenance
phases.
MySQL stored procedures have their own advantages and disadvantages. When you develop applications, you should decide whether you should or should not use stored procedure based on the
business requirements.
相关文章推荐
- Introduction to MySQL Stored Procedures
- ScottGu博客之翻译-Linq to Sql第7部分--用存储过程更新数据库--LINQ to SQL (Part 7 - Updating our Database using Stored Procedures)
- LINQ to SQL系列Part 7 - Updating our Database using Stored Procedures
- MySQL Stored Procedures
- Cursors in MySQL Stored Procedures
- The Guru's guide to SQL server stored procedures, XML, and HTML
- [转贴]How to work with stored procedures by using script in an Office InfoPath 2003 form
- mysql Error Handling and Raising in Stored Procedures
- JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedur
- PHP + MySql + Stored Procedures, how do I get access an "out" value?
- Using JDBC to Execute Stored Procedures
- MySQL stored procedures with Ruby
- MySQL Error Handling in Stored Procedures
- Good Practices to Write Stored Procedures in SQL Server
- Snippet: Fetching results after calling stored procedures using MySQL Connector/Python
- Introduction to MySQL Cluster for MySQL Users
- 数据仓库Unable to get list of procedures from database meta-data: Table 'mysql.proc' doesn't exist
- To SP or not to SP in SQL Server: an argument for stored procedures
- Question[SQL]: The Fastest Way to Recompile All Stored Procedures
- Mysql:User does not have access to metadata required to determine stored procedure parameter types.