SQL Server Stored Procedure writing experience and Optimization

Source: Internet
Author: User
I. Preface: After a period of storage process development, I wrote some summary and experience during development to share with you. I hope it will be helpful to you, mainly for Sybase and SQL Server databases, however, other databases should have some commonalities. Ii. target readers: Database developers, who have a large amount of data in the database, involve project developers who are interested in SP (Stored Procedure) optimization. Iii. Introduction: During database development, complex business logic and database operations are often encountered. In this case, SP is used to encapsulate database operations. If there are many SP projects and there is no certain specification for writing, it will affect the system maintenance difficulties in the future and the difficulty of understanding the SP series, in addition, if the database has a large amount of data or the project has high performance requirements for the SP, you will encounter optimization problems. Otherwise, the speed may be slow. After hands-on experience, an Optimized SP is hundreds of times more efficient than a poorly performing sp. Iv. content:1. If developers use tables or views of other databases, they must create a view in the current database to perform cross-database operations. It is best not to directly use "Databse. DBO. table_name ", because sp_depends cannot display the cross-database table or view used by the SP, it is not convenient to verify. 2. Before submitting the SP, the developer must have used set showplan on to analyze the query plan and perform its own query optimization check. 3. High program running efficiency and application optimization. Pay attention to the following points during SP writing: a) SQL usage specifications: I. avoid large transaction operations as much as possible, and use the holdlock clause with caution to improve the system concurrency capability. Ii. Try to avoid repeated accesses to the same or several tables, especially tables with large data volumes. You can consider extracting data to a temporary table based on the conditions and then connecting it. III. avoid using a cursor whenever possible because the cursor is inefficient. If the cursor operation contains more than 10 thousand rows of data, it should be rewritten. If the cursor is used, try to avoid table join operations in the cursor loop. IV. note that when writing where statements, the order of statements must be taken into account. The order before and after condition clauses should be determined based on the index order and range size, and the field order should be consistent with the index order as much as possible, the range is from large to small. V. do not perform functions, arithmetic operations, or other expression operations on the left side of "=" in the WHERE clause. Otherwise, the system may not be able to correctly use the index. VI. use exists instead of select count (1) to determine whether a record exists. The count function is used only when all the rows in the statistical table are used, and count (1) is more efficient than count. VII. Try to use "> =" instead of "> ". VIII. Note the replacement between some or clause and union clause IX. Note the data types connected between tables to avoid the connection between different types of data. X. Pay attention to the relationship between parameters and data types in stored procedures. XI. Pay attention to the data volume of insert and update operations to prevent conflicts with other applications. If the data volume exceeds 200 data pages (400 Kb), the system will update the lock and the page lock will be upgraded to the table lock. B) usage specification of indexes: I. The creation of indexes should be combined with the application. We recommend that the large OLTP table have no more than 6 indexes. II. try to use the index field as the query condition, especially the clustered index. If necessary, you can use index index_name to forcibly specify the index III. avoid table scan when querying large tables. If necessary, create an index. IV. when using an index field as a condition, if the index is a joint index, you must use the first field in the index as the condition to ensure that the system uses the index, otherwise, the index will not be used. V. Pay attention to index maintenance, rebuild indexes periodically, and recompile the stored procedure. C) usage specification of tempdb: I. Avoid using distinct, order by, group by, having, join, and cumpute as much as possible, because these statements will increase the burden on tempdb. Ii. Avoid frequent creation and deletion of temporary tables and reduce the consumption of system table resources. III. when creating a temporary table, if a large amount of data is inserted at one time, you can use select into instead of create table to avoid logs and increase the speed. If the data volume is small, in order to ease the system table resources, we recommend that you first create table and then insert. IV. if the temporary table has a large amount of data and requires an index, you should place the process of creating a temporary table and creating an index in a single sub-storage process, in this way, the system can use the index of the temporary table. V. if a temporary table is used, you must explicitly delete all temporary tables at the end of the stored procedure. First truncate the table and then drop the table, so that the system table can be locked for a long time. Vi. Use caution when connecting large temporary tables to other large tables to query and modify them, reducing the burden on the system table, because this operation will use the tempdb system table multiple times in one statement. D) Reasonable algorithm usage: compare multiple algorithms based on the aforementioned SQL optimization technology and the SQL Optimization content in the ASE tuning manual and the actual application, to obtain the method with the least resource consumption and the highest efficiency. Specific ASE optimization commands are available: Set statistics Io on, set statistics time on, set showplan on, and so on.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.