SQL query optimization 2

Source: Internet
Author: User

I. Operator Optimization

1. In Operator

SQL statements written in are easy to write and understand, which is suitable for modern software development. However, SQL statements using in always have low performance. The following differences exist between SQL statements using in and SQL statements without in:

Oracle tries to convert it to the join of multiple tables. If the conversion fails, it first executes the subquery in and then queries the outer table records, if the conversion is successful, multiple tables are directly connected for query. It can be seen that at least one conversion process is added to SQL statements using in. General SQL statements can be converted successfully, but SQL statements that contain grouping statistics cannot be converted.

Recommended solution:Use exists instead of the in operator in business-intensive SQL statements.

2. Not in Operator

This operation is not recommended for strong columns because it cannot apply table indexes.

Recommended solution:Use the not exists solution instead.

3. Is null or is not null operation(Check whether the field is empty)

The index is usually not used to determine whether a field is null, because the index does not have a null value.

Recommended Solution: Replace it with other operation operations with the same function, for example, changing a is not null to a> 0 or a>. Fields that are not allowed to be empty are replaced by a default value. If the Status field in the application is not allowed to be empty, the default value is apply.

4.> and <operator (greater than or less than operator)

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, but in some cases it can be optimized. For example, a table has 1 million records, for a numeric field a, 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.

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

6. 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:
Select * From gc_dfys
Union
Select * From ls_jg_dfys
This 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:The Union all operator is used to replace union, because the Union all operation simply merges the two results and returns them.

Select * From gc_dfys
Union all
Select * From ls_jg_dfys

Ii. Influence of SQL writing

1. The impact of different SQL statements on the same function and performance.

For example, an SQL statement inProgramThe member writes select * From zl_yhjbqk.

Programmer B writes select * From dlyx. zl_yhjbqk (with the table owner prefix)

The C programmer writes select * From dlyx. zlyhjbqk (Capital table name)

D programmers write select * From dlyx. zlyhjbqk (spaces are added 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, the shared memory also leaves only one analysis result, which not only reduces the time for SQL analysis, but also reduces duplicate information in the shared memory. Oracle can also accurately count the execution frequency of SQL.

2. Effect of conditional order after where

The conditional order after the WHERE clause directly affects the query of the big data table. For example:
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 and xh_bz fields 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.

3. query the influence of table order

The list order in the table after from will affect the SQL Execution performance. If there is no index and Oracle does not perform statistical analysis on the table, Oracle will link according to the order in which the table appears, it can be seen that when the table order is incorrect, data that consumes a lot of server resources will be generated. (Note: If statistical analysis is performed on the table, Oracle will automatically link the small table to the large table)

Iii. SQL statement index Utilization

1. Operator optimization (same as above)

2. Optimization of condition fields

Fields processed by functions cannot use indexes,For example:

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

Trunc (sk_rq) = trunc (sysdate), optimization: sk_rq> = trunc (sysdate) and sk_rq <trunc (sysdate + 1)

Fields with explicit or implicit operations cannot be indexed, for example, ss_df + 20> 50. 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.For example, ys_df> cx_df cannot be optimized.
Qc_bh | kh_bh = '000000', optimization: qc_bh = '000000' and kh_bh = '000000'

4. Others

The prompt function of Oracle is a strong function and a complicated application, and the prompt is just a suggestion for Oracle execution, sometimes Oracle may not follow the prompts for cost considerations. According to practical applications, it is generally not recommended for developers to Apply Oracle prompts. Because the performance of each database and server is different, the performance may be improved in one place, but the performance may be decreased in another place, oracle has been mature in SQL Execution analysis. If the analysis execution path is incorrect, it should first be in the database structure (mainly index), current server performance (shared memory, disk file fragments) and whether the statistical information of database objects (tables and indexes) is correct.

Http://jcyanfan.javaeye.com/blog/266423

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.