Stored procedures can effectively improve the reuse rate of SQL statements, and can put a group of related SQL statements into a stored procedure, thereby avoiding connection delays with the MySQL server and occupied network resources caused by multiple queries of the application. The following is an example of a stored procedure that passes in an id to delete the student with the specified id and deletes the student information in the extended table at the same time. In this way, related data can be processed without requiring the application to perform two SQL operations. DROP PROCEDURE IF EXISTS delete_student_by_id; delimiter $$ CREATE PROCEDURE delete_student_by_id(IN p_id INT) BEGIN DELETE FROM t_students WHERE id = p_id; DELETE FROM t_students_info WHERE student_id = p_id; END $$ delimiter ; In general, stored procedures have the following advantages:
Of course, there are always pros and cons, and stored procedures also have some defects:
Therefore, it is usually necessary to keep stored procedures small and concise to avoid the above-mentioned defects. Of course, stored procedures will run faster for some operations, especially when using loops within stored procedures to complete multiple small queries. If the queries are small enough, parsing the SQL statements and network communications become significant factors in the workload being too high. At this time, the advantages of stored procedures will be highlighted. Take the following stored procedure code as an example: DROP PROCEDURE IF EXISTS insert_many_rows; delemiter // CREATE PROCEDURE insert_many_rows(IN loops INT) BEGIN DECLARE v1 INT; SET v1=loops; WHILE v1 > 0 DO INSERT INTO test_table values(NULL, 0, 'aaaaaaaaaaaaabbbbbbbbbb', 'aaaaaaaaaaaaabbbbbbbbbb'); SET v1=v1-1; END WHILE; END // delemiter ; By comparing the same functions with applications, it can be found that the performance of using stored procedures is improved by more than 2 times, and if compared with using MySQL proxy, the performance is improved to 3 times.
The above is the detailed analysis of the advantages and disadvantages of MySQL stored procedures. For more information about the advantages and disadvantages of MySQL stored procedures, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Basic reference types of JavaScript advanced programming
>>: CSS Pick-up Arrows, Catalogs, Icons Implementation Code
UPD 2020.2.26 Currently Ubuntu 20.04 LTS has not ...
Recently, due to business reasons, I need to acce...
Purchase Certificate You can purchase it from Ali...
If you have experience in vue2 project developmen...
This article example shares the specific code of ...
Preface The following are the ways to implement L...
Around version 0.6, privileged was introduced to ...
Table of contents 1. mysqldump command to back up...
mysql-5.7.20-winx64.zipInstallation package witho...
Today, when I was installing CentOS6.2, I couldn&...
A few days ago, the library said that the server ...
Table of contents 1.1 Java environment as a prere...
This article shares the specific code of vue unia...
Table of contents 1. Test environment 1.1 Hardwar...
To search for RocketMQ images, you can search on ...