Oracle Optimizer

47
Oracle Optimizer Oracle Optimizer

description

Oracle Optimizer. Types of Optimizers. There are different modes for the optimizer RULE: Rule-based optimizer (RBO) Deprecated; not updated since 1994 CHOOSE: Default option. Chooses cost-based optimizer (CBO) if statistics available, rule-based optimizer otherwise. - PowerPoint PPT Presentation

Transcript of Oracle Optimizer

Page 1: Oracle Optimizer

Oracle OptimizerOracle Optimizer

Page 2: Oracle Optimizer

Types of OptimizersTypes of Optimizers

• There are different modes for the optimizer

• RULE: Rule-based optimizer (RBO)– Deprecated; not updated since 1994

• CHOOSE: Default option. Chooses cost-based optimizer (CBO) if statistics available, rule-based optimizer otherwise.

ALTER SESSION SET optimizer_mode = {choose|rule|first_rows(_n)|all_rows}

Page 3: Oracle Optimizer

Types of OptimizersTypes of Optimizers

• ALL_ROWS (CBO): Execute the query so that all of the rows are returned as quickly as possible– Merge Join has priority over Block Nested Loop Join

• FIRST_ROWS(n) (CBO): execute the query so that all of the first n rows are returned as quickly as possible– Block Nested Loop Join has priority over Merge

Join

Page 4: Oracle Optimizer

analyze table | index

<table_name> | <index_name>

compute statistics |

estimate statistics [sample <integer>

rows | percent] |

delete statistics;

analyze table Sailors estimate statistics sample 25 percent;

Analyzing the DataAnalyzing the Data

Page 5: Oracle Optimizer

Viewing the Execution PlanViewing the Execution Plan

• You need a PLAN_TABLE table. So, the first

time that you want to see execution plans,

run the command:

• Set autotrace on to see all plans

– Display the execution path for each query, after

being executed

@$ORACLE_HOME/rdbms/admin/utlxplan.sql

Page 6: Oracle Optimizer

Operations that Access Operations that Access TablesTables

• TABLE ACCESS FULL: sequential table scan

– Oracle optimizes by reading multiple blocks

– Used whenever there is no where clause on a

query

select * from Sailors

• TABLE ACCESS BY ROWID: access rows by

their RowID values.

– How do you get the rowid? From an index!

select * from Sailors where sid > 10

Page 7: Oracle Optimizer

Types of IndexesTypes of Indexes

• Unique: each row of the indexed table

contains a unique value for the

indexed column

• Nonunique: the row’s indexed values

can repeat

Page 8: Oracle Optimizer

Operations that Use Operations that Use IndexesIndexes

• INDEX UNIQUE SCAN: Access of an

index that is defined to be unique

• INDEX RANGE SCAN: Access of an

index that is not unique or access of a

unique index for a range of values

Page 9: Oracle Optimizer
Page 10: Oracle Optimizer
Page 11: Oracle Optimizer
Page 12: Oracle Optimizer

When are Indexes Used/Not When are Indexes Used/Not Used?Used?

• If you set an indexed column equal to a value, e.g., sname = 'Jim'

• If you specify a range of values for an indexed column, e.g., sname like 'J%'– sname like '%m': will not use an index

– UPPER(sname) like 'J%' : will not use an index

– sname is null: will not use an index, since null values are not stored in the index

– sname is not null: will not use an index, since every value in the index would have to be accessed

Page 13: Oracle Optimizer

When are Indexes Used? When are Indexes Used? (cont)(cont)

• 2*age = 20: Index on age will not be used. Index on 2*age will be used.

• sname != 'Jim': Index will not be used.

• MIN and MAX functions: Index will be used

• Equality of a column in a leading column of a multicolumn index. For example, suppose we have a multicolumn index on (sid, bid, day)– sid = 12: Can use the index

– bid = 101: Cannot use the index

Page 14: Oracle Optimizer

Optimizer HintsOptimizer Hints

• You can give the optimizer hints about

how to perform query evaluation

• Hints are written in /*+ */ right after

the select

• Note: These are only hints. The Oracle

optimizer can choose to ignore your

hints

Page 15: Oracle Optimizer

HintsHints

• FULL hint: tell the optimizer to perform a

TABLE ACCESS FULL operation on the

specified table

• ROWID hint: tell the optimizer to perform a

TABLE ACCESS BY ROWID operation on the

specified table

• INDEX hint: tells the optimizer to use an

index-based scan on the specified table

Page 16: Oracle Optimizer

ExamplesExamples

Select /*+ FULL (sailors) */ sidFrom sailorsWhere sname=‘Joe’;

Select /*+ INDEX (sailors) */ sidFrom sailorsWhere sname=‘Joe’;

Select /*+ INDEX (sailors s_ind) */ sidFrom sailors S, reserves RWhere S.sid=R.sid AND sname=‘Joe’;

Page 17: Oracle Optimizer

Combining Output From Combining Output From Multiple Index ScansMultiple Index Scans

• Suppose we have 2 indexes: sname, rating

• select * from sailorswhere sname = 'Jim' and rating = 10----------------------------------

0 SELECT STATEMENT Optimizer=CHOOSE

1 0 TABLE ACCESS (BY INDEX ROWID) OF 'SAILORS‘

2 1 AND-EQUAL

3 2 INDEX (RANGE SCAN) OF 'SNAME_IND' (NON-UNIQUE)

4 2 INDEX (RANGE SCAN) OF 'RAT' (NON-UNIQUE)

Page 18: Oracle Optimizer

Operations that Manipulate Data Operations that Manipulate Data SetsSets

• Up until now, all operations returned the rows as they were found

• There are operations that must find all rows before returning a single row

• Try to avoid these operations for online users!– SORT ORDER BY: query with order by

select sname, age

from Sailors

order by age;

Page 19: Oracle Optimizer

Operations that Manipulate Data Operations that Manipulate Data SetsSets

– SORT UNIQUE: sorting records while

eliminating duplicates (e.g., query

with distinct; query with minus,

intersect or union)

– SORT AGGREGATE, SORT GROUP BY:

queries with aggregate or grouping

functions (like MIN, MAX)

Page 20: Oracle Optimizer

Is the table always Is the table always accessed?accessed?

What if there is no index?

Page 21: Oracle Optimizer

Operations that Manipulate Data Operations that Manipulate Data SetsSets

• Consider the query:

– select sname from sailors

union

select bname from boats;

Page 22: Oracle Optimizer

Operations that Manipulate Data Operations that Manipulate Data SetsSets

• Consider the query:

– select sname from sailors

minus

select bname from boats;

How do you think that

Oracle implements intersect?union all?

Page 23: Oracle Optimizer

• Select MIN(age), COUNT(*)

from Sailors

GROUP BY rating

------------------------------------------0 SELECT

STATEMENT Optimizer=CHOOSE

1 0 SORT (GROUP BY)

2 1 TABLE ACCESS (FULL) OF 'SAILORS'

Operations that Manipulate Data Operations that Manipulate Data SetsSets

Page 24: Oracle Optimizer

DistinctDistinct

• What should Oracle do when

processing the query (assuming that

sid is the primary key):

– select distinct sid

from Sailors

• Try it!!

Page 25: Oracle Optimizer

Join MethodsJoin Methods

• Select * from Sailors, Reserves

where Sailors.sid = Reserves.sid

• Oracle can use an index on Sailors.sid

or on Reserves.sid (note that both will

not be used)

• Join Methods: MERGE JOIN, NESTED

LOOPS, HASH JOIN

Page 26: Oracle Optimizer

Nested Loops JoinsNested Loops Joins

• Block nested loop join

NESTED LOOPS

TABLE ACCESS FULL OF our_outer_table

TABLE ACCESS FULL OF our_inner_table

• Index nested loop joinNESTED LOOPS

TABLE ACCESS FULL OF our_outer_table

TABLE ACCESS BY ROWID OF our_inner_table

INDEX RANGE SCAN OF inner_table_index

Page 27: Oracle Optimizer

Nested Loops JoinsNested Loops Joins

• Select *from reserves r, sailors swhere r.sid=s.sid;

-------------------------------------------------------0 SELECT STATEMENT Optimizer=CHOOSE1 0 NESTED LOOPS2 1 TABLE ACCESS (FULL) OF 'RESERVES‘3 1 TABLE ACCESS (BY INDEX ROWID) OF 'SAILORS‘4 3 INDEX (UNIQUE SCAN) OF 'SYS_C00628777' (UNIQUE)

Page 28: Oracle Optimizer

When Are Nested Loops When Are Nested Loops Joins Used?Joins Used?

• If tables are of unequal size

• If results should be returned

online

Page 29: Oracle Optimizer

Hash Join PlanHash Join Plan

HASH JOINTABLE ACCESS FULL OF table_ATABLE ACCESS FULL OF table_B

Page 30: Oracle Optimizer

When Are Hash Joins When Are Hash Joins Used?Used?

• If tables are small

• If results should be returned online

Page 31: Oracle Optimizer

Sort-Merge Join PlanSort-Merge Join Plan

MERGE JOINSORT JOINTABLE ACCESS FULL OF table_ASORT JOINTABLE ACCESS FULL OF table_B

Page 32: Oracle Optimizer

When Are Sort/Merge Joins When Are Sort/Merge Joins Used?Used?

• Performs badly when tables are

of unequal size. Why?

Page 33: Oracle Optimizer

Hint Examples for JoinsHint Examples for Joins

Select /*+ USE_NL (sailors) */ sidFrom sailors S, reserves RWhere S.sid=R.sid AND sname=‘Joe’;

Select /*+ USE_MERGE (sailors, reserves) */ sidFrom sailors S, reserves RWhere S.sid=R.sid AND sname=‘Joe’;

Select /*+ USE_HASH */ sidFrom sailors S, reserves RWhere S.sid=R.sid AND sname=‘Joe’;

inner table

Page 34: Oracle Optimizer

XML (Extensible Markup XML (Extensible Markup Language) Language)

andand

the Semi-Structured Data Modelthe Semi-Structured Data Model

Page 35: Oracle Optimizer

MotivationMotivation

• We have seen that relational databases

are very convenient to query. However:

– There is a LOT of data not in relational

databases!!

• Perhaps the most widely accessed

database is the web, and it certainly

isn’t a relational database.

Page 36: Oracle Optimizer

Querying the WebQuerying the Web

• The web can be queried using a search engine, however, we can’t ask questions like:– What is the lowest price for which a Jaguar

is sold on the web?

• Problems:– There are no facilities for asking complex

questions, such as aggregation of data

Page 37: Oracle Optimizer

Understanding the WebUnderstanding the Web

• In order to query the web, we must be

able to understand it.

• 2 Computer Science Approaches:

– Artificial Intelligence Approach

– Database Approach

Page 38: Oracle Optimizer

Database ApproachDatabase Approach

“The web is unstructured and we will structure it”

• Sometimes problems that are very difficult can be solved easily by enforcing a standard

• Encourage the use of XML as a standard for data exchange on the web

Page 39: Oracle Optimizer

<addresses >

<person friend="yes">

<name> Jeff Cohen</name>

<tel> 04-828-1345 </tel>

<tel> 054-470-778 </tel>

<email> [email protected] </email>

</person>

<person friend="no">

<name> Irma Levy</name>

<tel> 03-426-1142 </tel>

<email>[email protected]</email>

</person>

</addresses>

Example XML DocumentExample XML Document

Opening Tag

AttributeElement

Closing Tag

Page 40: Oracle Optimizer

Very Unstructured XMLVery Unstructured XML

<?xml version=“1.0”?>

<DamageReport>

The insured’s <Vehicle Make = “Toyota”>

Corolla </Vehicle> broke through the guard rail and plummeted into the ravine. The cause was determined to be <Cause>faulty brakes </Cause>. Amazingly there were no casualties.

</DamageReport>

Page 41: Oracle Optimizer

XML Vs. HTMLXML Vs. HTML

• XML and HTML are brothers. They are both

special cases of SGML.

• HTML has specific tag and attribute names.

These are associated with a specific meaning

• XML can have any tag and attribute name.

These are not associated with any meaning

• HTML is used to specify visual style

• XML is used to specify meaning

Page 42: Oracle Optimizer

Characteristics of Characteristics of Semistructured DataSemistructured Data

(From http://www2.cs.uh.edu/~ceick/6340/intro-xml.ppt)

• Missing or additional attributes

• Multi-valued attributes

• Different types in different objects

• Heterogeneous collections

Self-describing, irregular data, no a priori structure

Page 43: Oracle Optimizer

Relational vs Semi-Relational vs Semi-Structured Structured

RelationalSemi-Structured

Abstract

Model

Sets of

tuples

Labeled Directed

Graph

Concrete

Model

TablesXML Documents

Standard

for

Storing

Data

Data Exchange

Separating Content

from Style

Page 44: Oracle Optimizer

Data ExchangeData Exchange

• Problem: Many data sources, each of a different type (different vendor), with a different schema. – How can the data be combined and used

together?

– How can different companies collaborate on their data?

– What format should be used to exchange the data?

Page 45: Oracle Optimizer

Separating Content from Separating Content from StyleStyle

• Web sites develop over time

• Important to separate style from data in order to allow changes to the site structure and appearance

• Using XML, we can store data alone

• CSS separates style from data only in a limited way

• Using XSL, this data can be translated into HTML

• The data can be translated differently as the site develops

Page 46: Oracle Optimizer

Write Once Use Write Once Use EverywhereEverywhere

XML Data

XSL

WML(hand-held

devices)

XSL

HTML(web browser

XSL

TEXT(Excel)

Page 47: Oracle Optimizer

Using XMLUsing XML

• Quering and Searching XML: There are query languages and search engines that query XML and return XML. Examples: Xpath, Xquery /SQL4X, Equix, XSEarch

• Displaying XML: An XML document can have an associated style-sheet which specifies how the document should be translated to HTML. Examples: CSS, XSL