The following article mainly analyzes the optimization of Oracle SQL statements IN detail. The first is the operator optimization. Let's talk about the IN operator first! We all know that Oracle SQL written IN has the advantages of easy to write and easy to 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 Oracle SQL statements that contain grouping statistics cannot be converted.
Recommended Solution: avoid using the IN operator IN business-intensive SQL statements.
Not in Operator
This operation is not recommended for strong columns because it cannot apply table indexes.
Recommended Solution: use not exists or external connection + null) instead
Operator is not equal)
The non-equals operator will never use the index, so the processing of it will only generate a full table scan.
Recommended Solution: use operations with the same functions instead,
For example, change a0 to a> 0 or a' to a>''
Is null or is not null to determine whether the field is null)
Generally, indexes are not used to determine whether a field is null, because the B-tree index does not have a null index.
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 are not allowed to be empty, but a default value is used to replace null values. For example, status fields in the application for expansion cannot be empty. The default value is application.
Creating a bitmap index with partitions cannot be created, and it is difficult to control Bitmap indexes. If too many fields are indexed, the performance will be degraded, and data block locks will be increased when multiple users update the index)
> 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, 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.
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.
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 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.
- select * from gc_dfys union all
- select * from ls_jg_dfys
The impact of SQL writing on the same function, same performance, and different SQL statements
For example, if an SQL statement is written by A programmer as Select * from zl_yhjbqk
Programmer B writes Select * from dlyx. zl_yhjbqk with the prefix of the table owner)
The C programmer writes the Select * from DLYX. ZLYHJBQK capital table name)
D programmers write Select * from DLYX. ZLYHJBQK with spaces in the middle)
The results and execution time of the above four Oracle SQL statements 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.
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 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 Oracle 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
The list order in the table after FROM will affect SQL Execution performance. 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 the table, Oracle will automatically link the small table to the large table)
SQL statement index Utilization
For the optimization of operators, see the previous section.) Optimization of condition fields
Fields processed by functions cannot use indexes, for example, substr (hbs_bh, 5400) = '000000'. Optimized Processing: hbs_bh like '000000'
Trunc (sk_rq) = trunc (sysdate), optimization:
Sk_rq> = trunc (sysdate) and sk_rq
Sysdate + 1)
Fields with explicit or implicit operations cannot be indexed, for example:
Ss_df + 20> 50, optimization processing: ss_df> 30 'X' | hbs_bh> 'x5400021452 ', optimization processing: hbs_bh> '123'
Sk_rq + 5 = sysdate, optimization processing: sk_rq = sysdate-5 hbs_bh = 5401002554, optimization processing: hbs_bh = '000000', note: this condition for hbs_bh implicit to_number conversion, because the hbs_bh field is of bytes type.
Indexes cannot be performed when multiple fields in the table are included in the condition. For example, ys_df> cx_df cannot be optimized.
Qc_bh | kh_bh = '000000', optimization: qc_bh = '000000' and kh_bh = '000000'
Apply the HINT prompt of Oracle)
The prompt processing is used when the Oracle SQL analysis execution path generated by Oracle is not satisfied. It can prompt the following for SQL:
Target tips:
COST Optimization by COST) RULE optimization by rules)
CHOOSE Default) Oracle automatically selects cost or rules for optimization) ALL_ROWS returns all rows as soon as possible)
FIRST_ROWS returns the first row of data as soon as possible)
The execution method prompt is as follows:
USE_NL uses the nested loops method) USE_MERGE uses the merge join method)
USE_HASH uses the hash join method)
Index prompt:
Indextable index) use the prompted table INDEX for query)
Other advanced prompts, such as parallel processing)
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 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.