Oracle improves query efficiency

Source: Internet
Author: User
Welcome to the Oracle community forum and interact with 2 million technical staff. (1) select the most efficient table name sequence (only valid in the rule-based Optimizer ): the ORACLE parser processes the table names in the FROM clause in the order FROM right to left. The table written in the FROM clause (basic table drivingtable) will be processed first, and

Welcome to the Oracle community forum and interact with 2 million technical staff> go to (1) select the most efficient table name sequence (only valid in the rule-based Optimizer ): the ORACLE parser processes the table names in the FROM clause in the order FROM right to left. The table written in the FROM clause (basic table driving table) will be first processed in the FROM clause.

Welcome to the Oracle community forum and interact with 2 million technical staff> enter

(1) select the most efficient table name sequence (only valid in the rule-based Optimizer): The ORACLE parser processes the table names in the FROM clause in the order FROM right to left, the base table driving table written in the FROM clause will be processed first. When the FROM clause contains multiple tables, you must select a table with the least number of records as the base table. If there are more than three tables for join query, You need to select the cross table (interp table) as the basic table, the cross table is the table referenced by other tables.

(2) join order in the WHERE clause.: ORACLE uses the bottom-up sequence to parse the WHERE clause. According to this principle, the join between tables must be written before other WHERE conditions, the conditions that can filter out the maximum number of records must be written at the end of the WHERE clause.

(3) Avoid using '*' in the SELECT clause: during ORACLE parsing, '*' is converted to all column names in sequence, this task is completed by querying the data dictionary, which means it will take more time

(4) Reduce the number of visits to the database: ORACLE has performed a lot of work internally: parsing SQL statements, estimating the index utilization, binding variables, and reading data blocks;

(5) re-set the ARRAYSIZE parameter in SQL * Plus, SQL * Forms, and Pro * C to increase the retrieval data volume for each database access. The recommended value is 200.

(6) use the DECODE function to reduce processing time: Use the DECODE function to avoid repeated scanning of the same record or joining the same table.

(7) simple integration and unrelated database access: If you have several simple database query statements, you can integrate them into a single query (even if there is no relationship between them)

(8) deleting duplicate records: the most efficient method for deleting duplicate records

(Because ROWID is used) Example: delete from emp e where e. ROWID>

(Select min (X. ROWID) from emp x where x. EMP_NO = E. EMP_NO); (9) replace DELETE with TRUNCATE: When deleting records in a table, rollback segments are usually used to store information that can be recovered.

If you do not have a COMMIT transaction, ORACLE will recover the data to the State before the deletion (which is precisely the State before the deletion command is executed). When TRUNCATE is used, rollback segments no longer store any recoverable information. after the command is run, the data cannot be restored. therefore, few resources are called and the execution time is short.

(The translator Press: TRUNCATE is only applicable to deleting the entire table, and TRUNCATE is DDL rather than DML) (10) Try to use COMMIT as much as possible: Use COMMIT as much as possible in the program, in this way, the performance of the program is improved, and the demand will be reduced because of the resources released by COMMIT:.

Information used to restore data on the rollback segment.

B.

The lock obtained by the Program Statement c.

Space d in redo log buffer.

ORACLE uses the Where clause to replace the HAVING clause for managing internal spending (11) of the preceding three types of resources: avoid HAVING clause, HAVING filters the result set only after all records are retrieved.

This process requires sorting, total, and other operations.

If the WHERE clause can be used to limit the number of records, this overhead can be reduced.

(In non-oracle) where on, where, and having can be added, on is the first statement to execute, where is the second clause, and having is the last clause, because on filters out records that do not meet the conditions before making statistics, it can reduce the data to be processed by intermediate operations. It is reasonable to say that the speed is the fastest, where should also be faster than having, because it performs sum only after filtering data, and on is used only when two tables are joined, so in a table, then we can compare where with having. In the case of single-Table query statistics, if the filter condition does not involve fields to be calculated, the results will be the same, but the where technology can be used, having cannot be used. In terms of speed, the latter must be slow. If a calculated field is involved, the value of this field is uncertain before calculation, according to the workflow written in the previous article, the where function time is completed before computing, and having works only after computing. In this case, the results are different. In multi-table join queries, on takes effect earlier than where. The system first combines multiple tables into a temporary table based on the join conditions between tables, then filters them by where, then computes them, and then filters them by having after calculation. It can be seen that to filter a condition to play a correct role, you must first understand when the condition should take effect, and then decide to put it there.

(12) Reduce queries to a table: in SQL statements containing subqueries, pay special attention to reducing queries to the table. Example: SELECT TAB_NAME FROM TABLES WHERE

(TAB_NAME, DB_VER) =

(SELECT TAB_NAME, DB_VER FROM TAB_COLUMNS where version = 604)

(13) Improve SQL efficiency through internal functions.

: Complex SQL statements often sacrifice execution efficiency.

Being able to master the above methods for solving problems using functions is very meaningful in practical work.

(14) use the table Alias (Alias): when connecting multiple tables in an SQL statement, use the table Alias and prefix the Alias on each Column. in this way, the parsing time can be reduced and the syntax errors caused by Column ambiguity can be reduced.

(15) Replace IN with EXISTS and not exists to replace not in: IN many basic table-based queries, to satisfy one condition, it is often necessary to join another table. in this case, using EXISTS (or not exists) usually improves the query efficiency.

IN a subquery, the not in Clause executes an internal sorting and merging.

IN either case, not in is the most inefficient

(Because it executes a full table traversal for the table in the subquery ).

To avoid the use of not in, we can rewrite it into an Outer join (Outer Joins) or not exists.

Example: (efficient) SELECT * FROM EMP

(Basic table) where empno> 0 AND EXISTS

(SELECT 'x' from dept where dept. DEPTNO = EMP. deptno and loc = 'melb') (inefficient) SELECT * FROM EMP

(Basic table) where empno> 0 and deptno in (select deptno from dept where loc = 'melb ')

(16) Identifying 'inefficient execute 'SQL statements: although various graphical tools for SQL optimization are emerging, writing your own SQL tools to solve the problem is always the best method: select executions, DISK_READS, BUFFER_GETS, ROUND (BUFFER_GETS-DISK_READS)/BUFFER_GETS, 2) Hit_radio, ROUND (DISK_READS/EXECUTIONS, 2) Reads_per_run, SQL _TEXT FROM V $ SQLAREA WHERE EXECUTIONS> 0 AND BUFFER_GETS> 0 AND

(BUFFER_GETS-DISK_READS)/BUFFER_GETS <0.8 order by 4 DESC;

(17) using indexes to improve efficiency: indexes are a conceptual part of a table to improve data retrieval efficiency. ORACLE uses a complex self-balancing B-tree structure.

Data Query by index is usually faster than full table scan.

When ORACLE finds the optimal path for executing the query and Update statements, the ORACLE optimizer uses the index.

Using indexes when joining multiple tables can also improve efficiency.

Another advantage of using an index is that it provides uniqueness verification of the primary key.

. For those LONG or long raw data types, You Can index almost all columns.

Generally, using indexes in large tables is particularly effective.

Of course, you will also find that using indexes to scan small tables can also improve efficiency.

Although the index can improve the query efficiency, we must pay attention to its cost.

The index requires space for storage and regular maintenance. The index itself is also modified whenever a record is increased or decreased in the table or the index column is modified.

This means that the INSERT, DELETE, and UPDATE operations for each record will pay four or five more disk I/O.

Because indexes require additional storage space and processing, unnecessary indexes will slow the query response time.

. Regular index reconstruction is necessary.

: ALTER INDEX REBUILD

(18) replace DISTINCT with EXISTS: when you submit a query that contains one-to-many table information (such as the Department table and employee table), avoid using DISTINCT in the SELECT clause.

In general, you can consider replacing it with EXIST, and EXISTS makes the query more rapid, because the RDBMS core module will return the result immediately after the subquery conditions are met.

Example:

(Inefficient): select distinct DEPT_NO, DEPT_NAME from dept d, emp e where d. DEPT_NO = E. DEPT_NO

(Efficient): SELECT DEPT_NO, DEPT_NAME FROM DEPT D WHERE EXISTS

(SELECT 'x' from emp e where e. DEPT_NO = D. DEPT_NO );

(19) SQL statements are written in upper case, because oracle always parses SQL statements first, converts lowercase letters to uppercase letters, and then executes

(20) try to use the connector "+" to connect strings in java code!

[1] [2]

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.