Analysis of Oracle SQL optimization rules

Source: Internet
Author: User

The following articles mainly introduce the recommended solutions for Oracle SQL optimization rules. If you are a beginner in the practical application of Oracle SQL optimization rules, you can use the following articles to better understand the actual operations of the recommended Oracle SQL optimization rules. The following is a detailed introduction of the article.

Use other operations with the same function, such:

1) CHANGE a is not null to a> 0 or a>.

2) fields that are not allowed to be empty are replaced by a default value. For example, status fields in the application for expansion cannot be empty. The default value is apply.

3) Creating a bitmap index A partitioned table cannot be created, and it is difficult to control Bitmap indexes. If there are too many fields, the index will degrade the performance, and the data block lock will be increased when multiple users update the index)

When the wildcard "%" or "_" is the first character of the query string, the index is not used.

For the connected column "|", the index of the last connected column is invalid. Avoid connections whenever possible. Separate connections or use functions that do not work on columns.

If the index is not function-based, the index will no longer function when a function is used for the index column in The Where clause.

Avoid using calculation in the index column in The Where clause. Otherwise, the entire table is scanned because the index fails.

When comparing columns of different data types, the index will become invalid.

> And <operators greater than or less than operators)

If the value is greater than or less than the operator, you do not need to adjust it. Because it has an index, index search is used. However, in some cases, you can optimize it using Oracle SQL, for example, if A table has 1 million records, A numeric field A, and 0.3 million records A = 3. Therefore, the effect of executing A> 2 and A> = 3 is very different, because ORACLE will first find the record index of 2 and then compare it, when A> = 3, ORACLE directly finds the record Index = 3.

Recommended Solution: Replace "> =" with "> ".

UNION operator

UNION filters out duplicate records after table link. Therefore, after table link, it sorts the generated result sets and deletes duplicate records before returning results. In most applications, duplicate records are not generated. The most common is the UNION of Process Tables and historical tables. For example:

  1. select * from gc_dfys   
  2. union   
  3. select * from ls_jg_dfys  

This Oracle SQL statement extracts the results of two tables at run time, sorts and deletes duplicate records using the sorting space, and finally returns the result set. If the table has a large amount of data, it may cause disk sorting.

Recommended Solution: Use the union all operator to replace UNION, because the union all operation simply merges the two results and returns them.

  1. select * from gc_dfys   
  2. union all   
  3. select * from ls_jg_dfys  

LIKE Operator

The LIKE operator can be used for wildcard queries. The wildcard combinations in the LIKE operator can be used for almost any queries. However, poor use may result in performance problems, for example, LIKE '% 100' does not reference the index, while LIKE 'x5400%' references the range index.

An actual example: the user ID following the Business ID in the YW_YHJBQK table can be used to query the Business ID YY_BH LIKE '% 100'. This condition will generate a full table scan, if you change to YY_BH LIKE 'x5400% 'OR YY_BH LIKE 'b5400%', the index of YY_BH will be used to query the two ranges, and the performance will be greatly improved.

The impact of Oracle SQL writing on shared SQL statements can improve operation efficiency)

Impact of SQL statement writing with the same function and performance

For example, if an SQL statement is written by A programmer

  1. Select * from zl_yhjbqk  

Programmer B writes

Select * from dlyx. zl_yhjbqk with the table owner prefix)

C programmers write

Select * from DLYX. ZLYHJBQK capital table name)

D programmers write

Select * from DLYX. ZLYHJBQK has spaces in the middle)

The results and execution time of the preceding four sqls are the same after ORACLE analysis, but the principle of shared memory SGA from ORACLE is as follows, it can be concluded that ORACLE will analyze each SQL statement once and occupy the shared memory. If the SQL string and format are completely the same, ORACLE will analyze it only once, shared Memory also leaves only one analysis result. This not only reduces the time required to analyze SQL statements, but also reduces duplicate information in shared memory. ORACLE can also accurately count the execution frequency of Oracle SQL statements.

Recommended Solution: Make sure that the query characters are identical for the same SQL statements in different regions, so as to use the SGA shared pool to prevent the same SQL statements from being analyzed multiple times.

Effect of conditional order after WHERE

Oracle processes multiple query conditions in the Where clause from bottom to top. Therefore, the table join statement should be written before other Where conditions. The conditions for filtering the maximum number of records must be written at the end of the Where clause.

The conditional order after the WHERE clause directly affects the query of the big data table, as shown in figure

Select * from zl_yhjbqk where dy_dj = '1k' and xh_bz = 1

Select * from zl_yhjbqk where xh_bz = 1 and dy_dj = '1kv below'

In the preceding two SQL statements, the dy_dj voltage level and xh_bz cancel sign are not indexed. Therefore, full table scan is performed, in the first SQL statement, the dy_dj = '1kv below 'condition is 99% in the record set, while the xh_bz = 1 condition is only 0.5%, when the first SQL statement is executed, 99% records are compared with dy_dj and xh_bz. When the second SQL statement is executed, 0.5% records are compared with dy_dj and xh_bz, the CPU usage of the second SQL statement is obviously lower than that of the first SQL statement.

Query table order

Oracle processes the table names in the From clause From right to left. Therefore, when the From clause contains multiple tables, the table with the least records is placed at the end. Valid only when RBO optimization is adopted)

The list order in the table after FROM will affect the performance of Oracle SQL Execution. ORACLE will link the table in the order it appears if there is no index or ORACLE does not perform statistical analysis on the table, as a result, data that consumes a lot of server resources is generated when the table order is incorrect. NOTE: If statistical analysis is performed on a table, ORACLE automatically links small tables before large tables ).

The non-index column in the Order By statement reduces performance and can be processed By adding an index. Strictly control the use of expressions in Order By statements

When connecting multiple tables in an SQL statement, use the table alias and use it as the prefix of each column. This reduces the resolution time.

Use more internal functions to improve SQL Efficiency

SQL statement index Utilization

For the optimization of operators, see the previous section)

Optimize some Oracle SQL statements for condition fields

Indexes cannot be used for fields processed by functions.

For example:

Substr (hbs_bh, 5400) = '20160301', optimization: hbs_bh like '20160301'

Trunc (sk_rq) = trunc (sysdate), optimization:

  1. sk_rq>=trunc(sysdate) and sk_rq

Fields with explicit or implicit operations cannot be indexed.

For example:

Ss_df + 20> 50, Oracle SQL optimization: ss_df> 30

'X' | hbs_bh> 'x5400021452 '. Optimization: hbs_bh> '123'

Sk_rq + 5 = sysdate, optimized: sk_rq = sysdate-5

Hbs_bh = 5401002554, optimization processing: hbs_bh = '000000'. Note: This condition implicitly converts hbs_bh to to_number, because the hbs_bh field is in bytes type.

When multiple fields in the table are included in the condition, indexes cannot be performed.

Ys_df> cx_df, cannot be optimized

Qc_bh | kh_bh = '000000', Oracle SQL optimization: qc_bh = '000000' and kh_bh = '000000'

Operations that may cause full table Scan

Use NOT or "<>" on the index Column"

Use function or calculation for index Columns

Not in Operation

The wildcard character is the first character of the query string.

Is null or IS NOT NULL

Multi-column index, but its first column is not referenced by the Where clause

Oracle SQL Execution analysis has been relatively mature. If the analysis execution path is incorrect, the database structure should be mainly indexed), the current server performance shared memory, disk file fragments), database object table, index) statistical information is correct.

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: 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.