choosing access path

35
1 David Konopnicki -1997 David Konopnicki -1997 Choosing Access Path The basic methods. The basic methods. The access paths and when The access paths and when they are available. they are available. How the optimizer chooses How the optimizer chooses among the access paths. among the access paths.

Upload: wyatt

Post on 06-Jan-2016

32 views

Category:

Documents


3 download

DESCRIPTION

Choosing Access Path. The basic methods. The access paths and when they are available. How the optimizer chooses among the access paths. Access Methods. Full Table Scans Read each row and determine of it satisfies the statement’s WHERE clause. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Choosing Access Path

11

David Konopnicki -1997David Konopnicki -1997

Choosing Access PathChoosing Access Path

The basic methods.The basic methods. The access paths and when they are The access paths and when they are

available.available. How the optimizer chooses among the How the optimizer chooses among the

access paths.access paths.

Page 2: Choosing Access Path

22

David Konopnicki -1997David Konopnicki -1997

Access MethodsAccess Methods

Full Table ScansFull Table Scans Read each row and determine of it satisfies the Read each row and determine of it satisfies the

statement’s WHERE clause.statement’s WHERE clause. Implemented very efficiently using multi-block Implemented very efficiently using multi-block

reads.reads. Each data block is read only once.Each data block is read only once.

Table Access by ROWID: fastest way.Table Access by ROWID: fastest way.

Page 3: Choosing Access Path

33

David Konopnicki -1997David Konopnicki -1997

Indexes, Clusters and Hash ClustersIndexes, Clusters and Hash Clusters

Indexes are created on one or more columns Indexes are created on one or more columns of a table. Very useful for range conditions. of a table. Very useful for range conditions. They are independent of the table.They are independent of the table.

Clusters are an optional method for storing Clusters are an optional method for storing the table. Clusters group together tables the table. Clusters group together tables because they share columns and often used because they share columns and often used together.together.

The related columns in a cluster is the The related columns in a cluster is the Cluster key (always indexed)Cluster key (always indexed)

Page 4: Choosing Access Path

44

David Konopnicki -1997David Konopnicki -1997

Example: ClusterExample: Cluster

Cluster key(DEPTNO)

10 DNAME LOCSALES BOSTON

EMPNO ENAME1 KING4 SMITH

20 DNAME LOCADMIN NEW-YORK

EMPNO ENAME8 WILSON

10 NORMAN

Indexed

Page 5: Choosing Access Path

55

David Konopnicki -1997David Konopnicki -1997

Hash ClustersHash Clusters

Organization like simple clusters but...Organization like simple clusters but... A row is stored in a hash cluster based on A row is stored in a hash cluster based on

the result of applying a hash function to the the result of applying a hash function to the row’s cluster key value. row’s cluster key value.

All rows with the same key value are stored All rows with the same key value are stored together on the disk.together on the disk.

Page 6: Choosing Access Path

66

David Konopnicki -1997David Konopnicki -1997

Example: Hash ClusterExample: Hash Cluster

Hash Key Cluster Key

TRIALNO Other Columns237 1235 ...

2363 ...7262 ...

238 16262 ...83747 ...

Page 7: Choosing Access Path

77

David Konopnicki -1997David Konopnicki -1997

Access Methods (cont...)Access Methods (cont...)

Cluster Scans:Cluster Scans: Retrieves all the rows that have the same cluster Retrieves all the rows that have the same cluster

key value.key value. ORACLE first obtains the ROWID of one of ORACLE first obtains the ROWID of one of

the selected rows by scanning the cluster index.the selected rows by scanning the cluster index. ORACLE then locates the rows based on this ORACLE then locates the rows based on this

ROWID.ROWID.

Page 8: Choosing Access Path

88

David Konopnicki -1997David Konopnicki -1997

Access Methods (Cont...)Access Methods (Cont...)

Hash Scans:Hash Scans: ORACLE first obtains the hash value by ORACLE first obtains the hash value by

applying the hash function.applying the hash function. ORACLE then scans the data blocks containing ORACLE then scans the data blocks containing

rows with that hash value.rows with that hash value.

Page 9: Choosing Access Path

99

David Konopnicki -1997David Konopnicki -1997

Access Methods Access Methods

Index Scans:Index Scans: ORACLE searches the index for the indexed ORACLE searches the index for the indexed

column values accessed by the statement.column values accessed by the statement. If the statement accesses only columns of the If the statement accesses only columns of the

index, ORACLE reads the values only from the index, ORACLE reads the values only from the index.index.

In the other case, ORACLE uses the ROWID In the other case, ORACLE uses the ROWID found in the index to read the specific row from found in the index to read the specific row from the table.the table.

Page 10: Choosing Access Path

1010

David Konopnicki -1997David Konopnicki -1997

Index Scan (cont...)Index Scan (cont...)

An index scan can be one of these types:An index scan can be one of these types: Unique: a unique scan of the index returns only Unique: a unique scan of the index returns only

a single ROWID.a single ROWID. Range: A range scan can return more than one Range: A range scan can return more than one

ROWID.ROWID.

Page 11: Choosing Access Path

1111

David Konopnicki -1997David Konopnicki -1997

The Rank of the access pathsThe Rank of the access paths

Page 12: Choosing Access Path

1212

David Konopnicki -1997David Konopnicki -1997

1 - Single Row By ROWID1 - Single Row By ROWID

If the Where Clause identifies the ROWIDIf the Where Clause identifies the ROWID Example:Example:

SELECT *SELECT *

FROM empFROM emp

WHERE WHERE ROWID=‘00000DC5.0000.0001’ROWID=‘00000DC5.0000.0001’

Page 13: Choosing Access Path

1313

David Konopnicki -1997David Konopnicki -1997

2 - Single Row by Cluster Join2 - Single Row by Cluster Join

For statements that join tables stored in the For statements that join tables stored in the same cluster if:same cluster if: the statement equates every column of the the statement equates every column of the

cluster in each table.cluster in each table. there is a condition that guarantees that only there is a condition that guarantees that only

one row will be returned.one row will be returned.

Page 14: Choosing Access Path

1414

David Konopnicki -1997David Konopnicki -1997

ExampleExample

SELECT *SELECT *

FROM emp,deptFROM emp,dept

WHERE emp.deptno =dept.deptnoWHERE emp.deptno =dept.deptno

AND emp.empno=7800AND emp.empno=7800

Page 15: Choosing Access Path

1515

David Konopnicki -1997David Konopnicki -1997

3 - Single Row by Hash Cluster Key with Unique or Primary Key3 - Single Row by Hash Cluster Key with Unique or Primary Key

The WHERE clause uses all columns of a The WHERE clause uses all columns of a hash cluster key with equality conditions.hash cluster key with equality conditions.

The statement is guaranteed to return only The statement is guaranteed to return only one row because the columns of the cluster one row because the columns of the cluster key makes also a primary or unique key.key makes also a primary or unique key.

Page 16: Choosing Access Path

1616

David Konopnicki -1997David Konopnicki -1997

ExampleExample

SELECT *SELECT *

FROM ordersFROM orders

WHERE orderno = 73468376WHERE orderno = 73468376

Page 17: Choosing Access Path

1717

David Konopnicki -1997David Konopnicki -1997

4 - Single Row by Unique or Primary key4 - Single Row by Unique or Primary key

The WHERE clause uses all the columns of The WHERE clause uses all the columns of a unique or primary key in equality a unique or primary key in equality condition.condition.

ORACLE uses the index of the key to find ORACLE uses the index of the key to find the ROWID and then accesses the row in the ROWID and then accesses the row in the table.the table.

Page 18: Choosing Access Path

1818

David Konopnicki -1997David Konopnicki -1997

ExampleExample

SELECT *SELECT *

FROM empFROM emp

WHERE empno=7800WHERE empno=7800

Page 19: Choosing Access Path

1919

David Konopnicki -1997David Konopnicki -1997

5 - Clustered Join5 - Clustered Join

The statement joins tables stored in the The statement joins tables stored in the same cluster, that is the WHERE clause same cluster, that is the WHERE clause equates the columns of the cluster columns equates the columns of the cluster columns in the two tables.in the two tables.

To execute the statement, ORACLE To execute the statement, ORACLE performs a nested loop operation.performs a nested loop operation.

Page 20: Choosing Access Path

2020

David Konopnicki -1997David Konopnicki -1997

ExampleExample

SELECT *SELECT *

FROM emp,deptFROM emp,dept

WHERE emp.deptno = dept.deptnoWHERE emp.deptno = dept.deptno

Page 21: Choosing Access Path

2121

David Konopnicki -1997David Konopnicki -1997

6 - Hash Cluster Key6 - Hash Cluster Key

The WHERE clause uses all the columns of The WHERE clause uses all the columns of the hash key in equality conditions.the hash key in equality conditions.

ORACLE calculates the hash value and ORACLE calculates the hash value and then performs a hash scan.then performs a hash scan.

SELECT *SELECT *

FROM line_itemsFROM line_items

WHERE deptno=09870897WHERE deptno=09870897

Page 22: Choosing Access Path

2222

David Konopnicki -1997David Konopnicki -1997

7 - Indexed Cluster Key7 - Indexed Cluster Key

ORACLE searches the cluster index to find ORACLE searches the cluster index to find the ROWID.the ROWID.

ORACLE then scans the rows with the ORACLE then scans the rows with the same cluster key using this ROWID.same cluster key using this ROWID.

SELECT * SELECT *

FROM empFROM emp

WHERE deptno = 10WHERE deptno = 10

Page 23: Choosing Access Path

2323

David Konopnicki -1997David Konopnicki -1997

8 - Composite Index8 - Composite Index

All the columns of the index are in the All the columns of the index are in the WHERE clause in equality conditions.WHERE clause in equality conditions.

ORACLE scans the index for the ROWIDs ORACLE scans the index for the ROWIDs and then accesses the tableand then accesses the table

SELECT *SELECT *

FROM empFROM emp

WHERE job=‘CLERK’ AND deptno=30WHERE job=‘CLERK’ AND deptno=30

Page 24: Choosing Access Path

2424

David Konopnicki -1997David Konopnicki -1997

9 - Single Column indexes9 - Single Column indexes

SELECT * FROM emp WHERE SELECT * FROM emp WHERE job=‘CLERK’job=‘CLERK’

ORACLE can merge indexes if the query ORACLE can merge indexes if the query conditions uses columns of many single conditions uses columns of many single column indexescolumn indexes

Page 25: Choosing Access Path

2525

David Konopnicki -1997David Konopnicki -1997

10 - Bounded Range Search on Indexed Columns10 - Bounded Range Search on Indexed Columns

The conditions uses either a single-column The conditions uses either a single-column index or the leading portion of a composite index or the leading portion of a composite index:index:

column = exprcolumn = exprcolumn >[=] expr AND column <[=] exprcolumn >[=] expr AND column <[=] expr

column BETWEEN expr AND exprcolumn BETWEEN expr AND expr

column LIKE ‘c%’column LIKE ‘c%’

ORACLE performs a range scan on the index ORACLE performs a range scan on the index and then accesses the table by ROWID.and then accesses the table by ROWID.

Page 26: Choosing Access Path

2626

David Konopnicki -1997David Konopnicki -1997

11 - Unbounded Range Search on Indexed Columns 11 - Unbounded Range Search on Indexed Columns

column >[=] exprcolumn >[=] expr

column <[=] exprcolumn <[=] expr

Page 27: Choosing Access Path

2727

David Konopnicki -1997David Konopnicki -1997

12 - Sort Merge Join12 - Sort Merge Join

Join on non-clustered columns.Join on non-clustered columns.

Page 28: Choosing Access Path

2828

David Konopnicki -1997David Konopnicki -1997

13 - Max or Min of Indexed Column13 - Max or Min of Indexed Column

SELECT MAX(sal)SELECT MAX(sal)

FROM empFROM emp ORACLE performs a range scan on the ORACLE performs a range scan on the

indexindex

Page 29: Choosing Access Path

2929

David Konopnicki -1997David Konopnicki -1997

14 - ORDER BY on indexed column14 - ORDER BY on indexed column

SELECT * SELECT *

FROM empFROM emp

ORDER BY empnoORDER BY empno

Page 30: Choosing Access Path

3030

David Konopnicki -1997David Konopnicki -1997

15 - Full Table Scan15 - Full Table Scan

For any SQL statement.For any SQL statement. Remark: You cannot use a index if you haveRemark: You cannot use a index if you havecolumn1 (>|<|>=|<=) column2column1 (>|<|>=|<=) column2column IS NULLcolumn IS NULLcolumn IS NOT NULLcolumn IS NOT NULLcolumn NOT INcolumn NOT INcolumn != column != column LIKE column LIKE column1 and column2 are in the same table.column1 and column2 are in the same table.

Page 31: Choosing Access Path

3131

David Konopnicki -1997David Konopnicki -1997

Cost-Based OptimizationCost-Based Optimization

To choose among the available access paths, To choose among the available access paths, the optimizer considers these factors:the optimizer considers these factors:

Selectivity of the query. (big % : Full scan, Selectivity of the query. (big % : Full scan, small %: index scan).small %: index scan).

DB_FILE_MULTIBLOCK_READ_COUNT DB_FILE_MULTIBLOCK_READ_COUNT (high: Full scan, small: index scan) (high: Full scan, small: index scan)

Page 32: Choosing Access Path

3232

David Konopnicki -1997David Konopnicki -1997

ExampleExample

If ename is a unique or If ename is a unique or primary key, the primary key, the optimizer determines optimizer determines that this query is that this query is highly selective and highly selective and that it must use the that it must use the index.index.

If ename is not a key, the If ename is not a key, the optimizer uses the optimizer uses the following statistical following statistical values:values:

USER_TAB_COLUMNS.NUM_DISTINCT USER_TAB_COLUMNS.NUM_DISTINCT

USER_TABLES.NUM_ROWSUSER_TABLES.NUM_ROWS

By assuming that the ename By assuming that the ename values are uniformly values are uniformly distributed, the optimizer distributed, the optimizer can evaluate the can evaluate the selectivityselectivity

SELECT * FROM emp WHERE ename = ‘JACKSON’

Page 33: Choosing Access Path

3333

David Konopnicki -1997David Konopnicki -1997

ExampleExample

To estimate the selectivity of this query, the To estimate the selectivity of this query, the optimizer uses the boundary value of 7500 optimizer uses the boundary value of 7500 the statistics the statistics HIGH_VALUEHIGH_VALUE and and LOW_VALUELOW_VALUE in the in the USER_TAB_COLUMNSUSER_TAB_COLUMNS stats table. It stats table. It assumes that the values are evenly assumes that the values are evenly distributed in the range to estimate the distributed in the range to estimate the selectivity of the condition.selectivity of the condition.

SELECT * FROM emp WHERE empno < 7500

Page 34: Choosing Access Path

3434

David Konopnicki -1997David Konopnicki -1997

ExampleExample

The value of the bind variable The value of the bind variable :e1:e1 is is unknown at optimization time, therefore the unknown at optimization time, therefore the optimizer heuristically guesses 25% of optimizer heuristically guesses 25% of selectivity. selectivity.

SELECT * FROM emp WHERE empno < :e1

Page 35: Choosing Access Path

3535

David Konopnicki -1997David Konopnicki -1997

ExampleExample

Let S1 be the selectivity of the condition Let S1 be the selectivity of the condition empno >= 7500.empno >= 7500.

Let S2 be the selectivity of the condition Let S2 be the selectivity of the condition empno <= 7800.empno <= 7800.

The optimizer estimates the selectivity of The optimizer estimates the selectivity of the statement as:the statement as:

S = ABS(S1+S2-1)S = ABS(S1+S2-1)

SELECT *FROM emp WHERE empno BETWEEN 7500 AND 7800

:e1:e2

S2 = 25%

S1 = 25%

S = ABS(.25+.25-1) = 50 %