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
Recently, when I was doing a practice project, I ...
Table of contents 1. View hook 1. Things to note ...
<br />Use of line break tag<br>The lin...
This article shares the MySQL installation and co...
Linux is generally used as a server, and the serv...
1. useState: Let functional components have state...
Problem: The partition where MySQL stores data fi...
Table of contents How to view the source code of ...
Generally, during Qingming Festival, the National...
Preface As you know, Linux supports many file sys...
This article shares the specific code for JavaScr...
Sorting query (order by) In e-commerce: We want t...
Directory Structure . │ .env │ docker-compose.yml...
Table of contents React upload file display progr...
Table of contents Preface $attrs example: $listen...