Oracle Stored Procedure learning experience

Source: Internet
Author: User
After a period of development and learning about the Oracle stored procedure, I wrote some summary and learning experiences during development to share with you. I hope this will be helpful to you.

After a period of development and learning about the Oracle stored procedure, I wrote some summary and learning experiences during development to share with you. I hope this will be helpful to you.

I. Preface:

After a period of development and learning about the Oracle stored procedure, I wrote some summary and learning experiences during development to share with you. I hope this will be helpful to you.

Ii. target readers:

Database developers, who have a large amount of data in the database, involve project developers who are interested in optimizing the SP (stored procedure.

Iii. Introduction:

During the development of the Oracle storage process, complex business logic and database operations are often encountered. At this time, database operations are encapsulated using SP. If there are many SP projects and there is no certain specification for writing, it will affect the difficulties of system maintenance and the difficulty of understanding the big SP logic in the future, 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 an optimized SP with poor performance.

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 when writing Oracle storage in SP:

A) SQL usage specifications:

I. Avoid large transaction operations as much as possible. 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. in Oracle stored procedure development, pay attention to where statement writing. The order of statements must be considered. The order of condition clauses should be determined based on the index order and range, make sure that the field order is consistent with the index order, and 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 the or clause and the union clause.

Ix. Pay attention to 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 Oracle 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) Specification for indexing:

I. You should consider creating indexes in combination with applications. We recommend that you create a large OLTP table with no more than six 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 performing 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 the index periodically, and recompile the Oracle storage process.

C) use of tempdb:

I. Try to avoid using distinct, order by, group by, having, join, and *** pute, 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 put the process of creating a temporary table and creating an index in the sub-storage process of a separate Oracle stored procedure, 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:

Based on the SQL optimization technology mentioned above and the SQL Optimization content in the ASE Tuning manual, combined with practical applications, a variety of algorithms are used for comparison, to obtain the Oracle Stored Procedure method that consumes the least resources and has the highest efficiency. Specific ASE optimization commands are available: set statistics io on, set statistics time on, set showplan on, and so on.

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.