The advantages and disadvantages of using stored procedures in the database

I haven't done a server project for many years. Recently, several projects are related to the server, and many performance-related problems have appeared. One of the projects involves such a business. Take it out and share it with everyone.

1. Background

The product business process is generally as follows: swiping the ID card for over-checking, the device end generates an over-checking record id, and the ID card text information and ID card picture information need to be uploaded to the server S1 and the image server S2, respectively, of which ID card picture information The URL of the ID photo on the picture service S2 is saved.
If the mandatory text information is uploaded successfully on the server S1, and then the ID card image is uploaded, it will inevitably affect the image upload time. If this mandatory limit is not imposed, the upload image URL and text information storage time cannot be determined in succession. Whether the url data and text information is executed in the server database by insert or update cannot be determined. At this time, it is necessary to deal with the insert failure and then restart For update operations, you can consider using stored procedures at this time.
On the issue of using stored procedures, discuss with the server-side R & D to decide whether to use stored procedures, so we need to understand the advantages and disadvantages of stored procedures.

2. Advantages

1. Running speed: For very simple SQL, the stored procedure has no advantage. For complex business logic, because the database has been parsed and optimized once when the stored procedure is created. Once the stored procedure is executed, a copy of the stored procedure will be kept in memory, so that the next time the same stored procedure is executed, it can be called directly from the memory, so the execution speed will be faster than ordinary SQL.    
2. Reduce network transmission: The storage process runs directly on the database server, all data access is performed inside the database server, there is no need to transfer data to other servers, so it will reduce certain network transmission.
3. Maintainability: Stored procedures are sometimes easier to maintain than programs. This is because the stored procedures on the DB side can be updated in real time. Some bugs directly modify the business logic in the stored procedure without modifying the terminal code. 
4. Enhanced security: Improve code security and prevent SQL injection. This sql statement can also be done.
5. Scalability: The application and database operations are separated and performed independently instead of being together. Facilitate future expansion and optimization of DBA maintenance.

3. Disadvantages

1. SQL itself is a structured query language, but it is not object-oriented. It is essentially a procedural language. In the face of complex business logic, procedural processing will be difficult. At the same time, SQL is good at data query rather than business logic processing. If you put all business logic in stored procedures, it violates this principle.
2. If you need to change the parameters of the input stored procedure, or you want to change the data returned by it, you still need to update the code in the assembly to add parameters, update calls, and so on, which is probably more cumbersome at this time.
3. Development and debugging are complicated. Due to IDE problems, the development and debugging of stored procedures is more difficult than general procedures, but this is not a problem for R & D with database skills. 4. There is no way to apply the cache. Although there are methods such as global temporary tables that can be used for caching, they also increase the burden on the database. If cache concurrency is severe and locks are often required, the efficiency is really worrying.
5. Does not support clustering, the database server cannot be expanded horizontally, or the database is cut (horizontal or vertical cut). After the database is cut, the storage process is not clear in which database the data is stored.


IV. Summary

1. Appropriate use of stored procedures can improve server performance.
2. Stored procedures should not be used on a large scale, and DBAs should be balanced when designing and maintaining databases.
3. With the emergence of many ORMs, many advantages of stored procedures are not obvious.
4. Combined with the actual business of the project, it is not recommended to use stored procedures for simple database operations with business logic. Complexity requires high real-time data. You can consider design optimization from stored procedure triggers.

Finally, back to our project business. Whether it is used or not needs to be combined with whether the customer has high requirements on the real-time nature of the ID card image information. Finally, confirm with the project product manager that the ID card image information can be delayed offline upload, then the storage process can be completely eliminated.

发布了142 篇原创文章 · 获赞 258 · 访问量 16万+

Guess you like

Origin blog.csdn.net/conconbenben/article/details/103655760