Oracle Optimizer. Types of Optimizers There are different modes for the optimizer RULE: Rule-based optimizer (RBO) –Deprecated; not updated since 1994.

Slides:



Advertisements
Similar presentations
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Database Management Systems 3ed, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 14, Part B.
Database Management Systems, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
Query Optimization Goal: Declarative SQL query
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
SPRING 2004CENG 3521 Query Evaluation Chapters 12, 14.
1 Relational Query Optimization Module 5, Lecture 2.
1 King Saud University College of Computer & Information Sciences IS 335 Database Management System Lecture 6 Query Processing and Optimization (Practice)
Midterm Review Lecture 14b. 14 Lectures So Far 1.Introduction 2.The Relational Model 3.Disks and Files 4.Relational Algebra 5.File Org, Indexes 6.Relational.
Agenda Overview of the optimizer How SQL is executed Identifying statements that need tuning Explain Plan Modifying the plan.
1 Implementation of Relational Operations Module 5, Lecture 1.
Query Rewrite: Predicate Pushdown (through grouping) Select bid, Max(age) From Reserves R, Sailors S Where R.sid=S.sid GroupBy bid Having Max(age) > 40.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
David Konopnicki Choosing Access Path ä The basic methods. ä The access paths and when they are available. ä How the optimizer chooses among the.
1 Views. 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
Evaluation of Relational Operations. Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation.
Optimization Exercises. Question 1 How do you think the following query should be computed? What indexes would you suggest to use? SELECT E.ename, D.mgr.
Oracle Optimizer. Combining Output From Multiple Index Scans AND-EQUAL: –select * from sailors where sname = 'Jim' and rating = 10 Suppose we have 2 indexes:
1 Advanced Topics XML and Databases. 2 XML u Overview u Structure of XML Data –XML Document Type Definition DTD –Namespaces –XML Schema u Query and Transformation.
1 Implementation of Relational Operations: Joins.
Query Optimization, part 2 CS634 Lecture 13, Mar Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 12: Overview.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Query Optimization. overview Histograms A histogram is a data structure maintained by a DBMS to approximate a data distribution Equiwidth vs equidepth.
Database Management 9. course. Execution of queries.
11-1 Improve response time of interactive programs. Improve batch throughput. To ensure scalability of applications load vs. performance. Reduce system.
Relational Operator Evaluation. Overview Index Nested Loops Join If there is an index on the join column of one relation (say S), can make it the inner.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
SQL Performance and Optimization l SQL Overview l Performance Tuning Process l SQL-Tuning –EXPLAIN PLANs –Tuning Tools –Optimizing Table Scans –Optimizing.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Oracle tuning: a tutorial Saikat Chakraborty. Introduction In this session we will try to learn how to write optimized SQL statements in Oracle 8i We.
1 Database Systems ( 資料庫系統 ) December 3, 2008 Lecture #10.
Module 4 Database SQL Tuning Section 3 Application Performance.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Introduction to Query Optimization Chapter 13.
CMPT 258 Database Systems SQL Queries (Chapter 5).
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan Evaluation of Relational Operations Chpt 14.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Implementation of Database Systems, Jarek Gryz1 Evaluation of Relational Operations Chapter 12, Part A.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Alon Levy 1 Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation. – Projection ( ) Deletes.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Evaluation of Relational Operations Chapter 14, Part A (Joins)
1 CS122A: Introduction to Data Management Lecture #7 Relational Algebra I Instructor: Chen Li.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Cost Model and Estimating Result Sizes. מודל המחיר Cost Model בהרצאה הראנו איך לחשב את המחיר של כל שיטה (join) כדי לעשות זאת צריך לדעת את גודל היחסים,
XML: Extensible Markup Language
Choosing Access Path The basic methods.
Relational Algebra Chapter 4, Part A
Evaluation of Relational Operations
Evaluation of Relational Operations: Other Operations
Relational Operations
CS4222 Principles of Database System
Implementation of Relational Operations
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
Evaluation of Relational Operations: Other Techniques
Introduction to the Optimizer
Evaluation of Relational Operations: Other Techniques
Presentation transcript:

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. ALTER SESSION SET optimizer_mode = {choose|rule|first_rows(_n)|all_rows}

Types 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

analyze table | index | compute statistics | estimate statistics [sample rows | percent] | delete statistics; analyze table Sailors estimate statistics sample 25 percent; Analyzing the Data

Viewing 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

Operations that Access Tables 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

Types 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

Operations that Use Indexes 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

When are Indexes Used/Not 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

When are Indexes Used? (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

Optimizer 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

Hints 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

Examples Select /*+ FULL (sailors) */ sid From sailors Where sname=‘Joe’; Select /*+ INDEX (sailors) */ sid From sailors Where sname=‘Joe’; Select /*+ INDEX (sailors s_ind) */ sid From sailors S, reserves R Where S.sid=R.sid AND sname=‘Joe’;

Combining Output From Multiple Index Scans Suppose we have 2 indexes: sname, rating select * from sailors where sname = 'Jim' and rating = 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)

Operations that Manipulate Data Sets 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;

Operations that Manipulate Data Sets –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)

Is the table always accessed? What if there is no index?

Operations that Manipulate Data Sets Consider the query: –select sname from sailors union select bname from boats;

Operations that Manipulate Data Sets Consider the query: –select sname from sailors minus select bname from boats; How do you think that Oracle implements intersect? union all?

Select MIN(age), COUNT(*) from Sailors GROUP BY rating SELECT STATEMENT Optimizer=CHOOSE 1 0 SORT (GROUP BY) 2 1 TABLE ACCESS (FULL) OF 'SAILORS' Operations that Manipulate Data Sets

Distinct What should Oracle do when processing the query (assuming that sid is the primary key): –select distinct sid from Sailors Try it!!

Join 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

Nested 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 join NESTED LOOPS TABLE ACCESS FULL OF our_outer_table TABLE ACCESS BY ROWID OF our_inner_table INDEX RANGE SCAN OF inner_table_index

Nested Loops Joins Select * from reserves r, sailors s where r.sid=s.sid; SELECT STATEMENT Optimizer=CHOOSE 1 0 NESTED LOOPS 2 1 TABLE ACCESS (FULL) OF 'RESERVES‘ 3 1 TABLE ACCESS (BY INDEX ROWID) OF 'SAILORS‘ 4 3 INDEX (UNIQUE SCAN) OF 'SYS_C ' (UNIQUE)

When Are Nested Loops Joins Used? If tables are of unequal size If results should be returned online

Hash Join Plan HASH JOIN TABLE ACCESS FULL OF table_A TABLE ACCESS FULL OF table_B

When Are Hash Joins Used? If tables are small If results should be returned online

Sort-Merge Join Plan MERGE JOIN SORT JOIN TABLE ACCESS FULL OF table_A SORT JOIN TABLE ACCESS FULL OF table_B

When Are Sort/Merge Joins Used? Performs badly when tables are of unequal size. Why?

Hint Examples for Joins Select /*+ USE_NL (sailors) */ sid From sailors S, reserves R Where S.sid=R.sid AND sname=‘Joe’; Select /*+ USE_MERGE (sailors, reserves) */ sid From sailors S, reserves R Where S.sid=R.sid AND sname=‘Joe’; Select /*+ USE_HASH */ sid From sailors S, reserves R Where S.sid=R.sid AND sname=‘Joe’; inner table

XML (Extensible Markup Language) and the Semi-Structured Data Model

Motivation 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.

Querying 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

Understanding the Web In order to query the web, we must be able to understand it. 2 Computer Science Approaches: –Artificial Intelligence Approach –Database Approach

Database 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

Jeff Cohen Irma Levy Example XML Document Opening Tag Attribute Element Closing Tag

Very Unstructured XML The insured’s Corolla broke through the guard rail and plummeted into the ravine. The cause was determined to be faulty brakes. Amazingly there were no casualties.

XML 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

Characteristics of Semistructured Data (From Missing or additional attributes Multi-valued attributes Different types in different objects Heterogeneous collections Self-describing, irregular data, no a priori structure

Relational vs Semi-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

Data 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?

Separating Content from Style 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

Write Once Use Everywhere XML Data XSL WML (hand-held devices) XSL HTML (web browser XSL TEXT (Excel)

Using 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