Oracle SQL Performance Optimization series (9)

Source: Internet
Author: User

27.Basic table Selection

 

Driving Table is the first Table to be accessed (usually accessed in full Table scan ). depending on the optimizer, the selection of basic tables in SQL statements is different.

If you are using the CBO (cost based optimizer), the OPTIMIZER checks the physical size and index status of each table in the SQL statement, and then selects the execution path with the lowest COST.

If you use RBO (rule based optimizer) and all the join conditions have an index, in this case, the base table is the table listed at the end of the FROM clause.

Example:

Select a. NAME, B. MANAGER

From worker,

LODGING B

Where a. LODGING = B. LODING;

Because the LODGING table has an index on the LODING column, and the WORKER table does not have a comparative index, the WORKER table will be used as the basic table in the query.

 

28.Multiple equal Indexes

When the execution path of an SQL statement can use multiple indexes distributed on multiple tables, ORACLE uses multiple indexes at the same time and merges their records at runtime, retrieve records that are valid only for all indexes.

When ORACLE chooses the execution path, the level of the unique index is higher than that of the non-unique index. However, this rule only

It is valid only when the index column and constant in the WHERE clause are compared. If the index column is compared with the index classes of other tables, the level of this clause in the optimizer is very low.

If two indexes of different tables with the same level are referenced, the order of the tables in the FROM clause determines which table will be used first. The index of the last table in the FROM clause will have the highest priority.

If two indexes in the same table of the same level are referenced, the first referenced index in the WHERE clause has the highest priority.

Example:

DEPTNO has a non-unique index and EMP_CAT also has a non-unique index.

Select ename,

FROM EMP

WHERE DEPT_NO = 20

AND EMP_CAT = 'a ';

Here, the DEPTNO index will be first retrieved and then merged with the records retrieved by the EMP_CAT index. The execution path is as follows:

 

TABLE ACCESS BY ROWID ON EMP

AND-EQUAL

Index range scan on DEPT_IDX

Index range scan on CAT_IDX

 

29. Equality comparison and range comparison

If the WHERE clause contains an index Column,ORACLE cannot merge them, and ORACLE will use range comparison.

 

Example:

DEPTNO has a non-unique index and EMP_CAT also has a non-unique index.

SELECT ENAME

FROM EMP

Where deptno> 20

AND EMP_CAT = 'a ';

Here, only the EMP_CAT index is used, and all records are compared one by one with the DEPTNO condition. The execution path is as follows:

TABLE ACCESS BY ROWID ON EMP

Index range scan on CAT_IDX

 

30.Ambiguous index levels

 

When ORACLE cannot determine the level difference of the index, the optimizer will only use one index, which is listed at the beginning in the WHERE clause.

Example:

DEPTNO has a non-unique index and EMP_CAT also has a non-unique index.

SELECT ENAME

FROM EMP

Where deptno> 20

AND EMP_CAT> 'a ';

 

Here, ORACLE only uses the DEPT_NO index. The execution path is as follows:

TABLE ACCESS BY ROWID ON EMP

Index range scan on DEPT_IDX

 

Press:

Let's try the following:

SQL> select index_name, uniqueness from user_indexes where table_name = 'emp ';

 

INDEX_NAME UNIQUENES

---------------------------------------

EMPNO UNIQUE

EMPTYPE NONUNIQUE

 

SQL> select * from emp where empno> = 2 and emp_type = 'a ';

 

No rows selected

 

 

Execution Plan

----------------------------------------------------------

0 select statement Optimizer = CHOOSE

1 0 table access (by index rowid) OF 'emp'

2 1 INDEX (range scan) OF 'empty' (NON-UNIQUE)

 

Although EMPNOIs a unique index,However, because it does range comparison,The level is lower than that of the non-unique index!

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.