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
What are :is and :where? :is() and :where() are p...
1. What affects database query speed? 1.1 Four fa...
For some systems with large amounts of data, the ...
Table of contents User Management Create a new us...
1. Create a scheduling task instruction crontab -...
Table of contents Preface 1. Style penetration 1....
First, your container must be running You can vie...
Two ways to enable proxy React does not have enca...
Table of contents Preface keep-avlive hook functi...
1. What is Continuous Delivery The software produ...
Recently, when I was learning Django, I needed to...
Recently, I need to use a lot of fragmented pictu...
Preface Generator functions have been in JavaScri...
Table of contents Two ways to solve the problem o...
This article records the specific method of insta...