Oracle query optimization skills

Source: Internet
Author: User

Oracle optimization query Skills 1. connection sequence in WHERE clause: ORACLE uses the bottom-up sequence to parse the WHERE clause. According to this principle, the connection 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. 2. avoid using '*' in the SELECT clause: ORACLE converts '*' into all column names in sequence during parsing. This is done by querying the data dictionary, this means that it will take more time 3. use the DECODE function to reduce processing time: Use the DECODE function to avoid repeated scanning of the same record or reconnecting the same table. 4. replace DELETE with TRUNCATE: When deleting records in a table, a ROLLBACK segment is usually used to store recoverable information. 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 applies only to deleting the entire table, and TRUNCATE is DDL rather than DML) 5. use COMMIT as much as possible: if 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 due to the resources released by COMMIT: resources released by COMMIT: information used to restore data in the rollback segment. when the volume is locked by the Program Statement, the space in the volume redo log buffer is spent by ORACLE to manage the internal costs of the above three types of resources. 6. 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. 7. replace IN with EXISTS and not exists instead of not in: IN many basic table-based queries To meet one condition, you often need 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') Inefficiency: SELECT * from emp (basic table) where empno> 0 and deptno in (SELECT DEPTNO FROM DEPT WHE Re loc = 'melb') 8. 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 immediately return results once 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 efficiency: SELECT DEPT_NO, DEPT_NAME from dept d where exists (SELECT 'x' from emp e where e. DEPT_NO = D. DEPT_NO); 9. SQL statements are written in uppercase. Because ORACLE always parses SQL statements first, converts lowercase letters to uppercase and then executes the statements. 10. replace the order by: distinct order by clause with WHERE to use the index only under two strict conditions. all columns in sort order by must be included in the same index and maintained in the ORDER of the index. all columns in sort order by must be defined as non-empty. the indexes used BY the WHERE clause and the indexes used in the order by clause cannot be tied together. for example, the DEPT table contains the following columns: DEPT_CODE pk not null DEPT_DESC not null DEPT_TYPE NULL inefficiency: (the index is NOT used) SELECT DEPT_CODE from dept order by DEPT_TYPE efficiency: (using the index) SELECT DEPT_CODE from dept where DEPT_TYPE> 011. optimize group by: to improve the efficiency of group by statements, you can filter out unnecessary records before group. the following two queries return the same results, but the second one is much faster. inefficient: select job, AVG (SAL) from emp group job having job = 'President 'or job = 'manager' efficiency: select job, AVG (SAL) from emp where job = 'President 'or job = 'manager' GROUP JOB

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.