1 King Saud University College of Computer & Information Sciences IS 335 Database Management System Lecture 6 Query Processing and Optimization (Practice)

Slides:



Advertisements
Similar presentations
Examples of Physical Query Plan Alternatives
Advertisements

Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Query Optimization May 31st, Today A few last transformations Size estimation Join ordering Summary of optimization.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapters 14.
Database Management Systems, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
1 Lecture 11: Basic SQL, Integrity constraints
Advanced Databases: Lecture 2 Query Optimization (I) 1 Query Optimization (introduction to query processing) Advanced Databases By Dr. Akhtar Ali.
1 Overview of Query Evaluation Chapter Objectives  Preliminaries:  Core query processing techniques  Catalog  Access paths to data  Index matching.
SPRING 2004CENG 3521 Query Evaluation Chapters 12, 14.
1 Relational Query Optimization Module 5, Lecture 2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Query Evaluation Chapter 12.
Agenda Overview of the optimizer How SQL is executed Identifying statements that need tuning Explain Plan Modifying the plan.
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.
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.
1 Optimization Recap and examples. 2 Optimization introduction For every SQL expression, there are many possible ways of implementation. The different.
David Konopnicki Choosing Access Path ä The basic methods. ä The access paths and when they are available. ä How the optimizer chooses among the.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Query Evaluation Chapter 12.
1 Query Optimization. 2 Why Optimize? Given a query of size n and a database of size m, how big can the output of applying the query to the database be?
Overview of Query Evaluation R&G Chapter 12 Lecture 13.
1 Optimization - Selection. 2 The Selection Operation Table: Reserves(sid, bid, day, agent) A page (block) can hold 100 Reserves tuples There are 1,000.
Oracle Optimizer. Types of Optimizers There are different modes for the optimizer RULE: Rule-based optimizer (RBO) –Deprecated; not updated since 1994.
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.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
1 Views. 2 What are views good for? (1) Simplifying complex queries: We saw one example. Here is another that allows the user to "pretend" that there.
1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapter 15.
Query Optimization Overview Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems December 2, 2004 Some slide content derived.
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.
Overview of Query Optimization v Plan : Tree of R.A. ops, with choice of alg for each op. –Each operator typically implemented using a `pull’ interface:
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.
Overview of Implementing Relational Operators and Query Evaluation
Introduction to Database Systems1 Relational Query Optimization Query Processing: Topic 2.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
LeongHW, SoC, NUS (UIT2201: Database) Page 1 © Leong Hon Wai, Animation of SQL Queries To illustrate three SQL queries: –Q1: simple select (one.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 12: Overview.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Query Optimization. overview Histograms A histogram is a data structure maintained by a DBMS to approximate a data distribution Equiwidth vs equidepth.
Database systems/COMP4910/Melikyan1 Relational Query Optimization How are SQL queries are translated into relational algebra? How does the optimizer estimates.
Advanced Databases: Lecture 8 Query Optimization (III) 1 Query Optimization Advanced Databases By Dr. Akhtar Ali.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
1 Database Systems ( 資料庫系統 ) December 3, 2008 Lecture #10.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Implementing Relational Operators and Query Evaluation Chapter 12.
Module 4 Database SQL Tuning Section 3 Application Performance.
Introduction to Query Optimization, R. Ramakrishnan and J. Gehrke 1 Introduction to Query Optimization Chapter 13.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Introduction to Query Optimization Chapter 13.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Chapter 5 Index and Clustering
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 12 – Introduction to.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
Cost Estimation For each plan considered, must estimate cost: –Must estimate cost of each operation in plan tree. Depends on input cardinalities. –Must.
1 Indexes ► Sort data logically to improve the speed of searching and sorting operations. ► Provide rapid retrieval of specified rows from the table without.
Database Applications (15-415) DBMS Internals- Part IX Lecture 20, March 31, 2016 Mohammad Hammoud.
Cost Model and Estimating Result Sizes. מודל המחיר Cost Model בהרצאה הראנו איך לחשב את המחיר של כל שיטה (join) כדי לעשות זאת צריך לדעת את גודל היחסים,
Query Optimization. overview Application Programmer (e.g., business analyst, Data architect) Sophisticated Application Programmer (e.g., SAP admin) DBA,
Choosing Access Path The basic methods.
Evaluation of Relational Operations
Overview of Query Optimization
Introduction to Database Systems
Examples of Physical Query Plan Alternatives
Database Applications (15-415) DBMS Internals- Part IX Lecture 21, April 1, 2018 Mohammad Hammoud.
Overview of Query Evaluation
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
Evaluation of Relational Operations: Other Techniques
Database Systems (資料庫系統)
Relational Query Optimization
Relational Query Optimization
Presentation transcript:

1 King Saud University College of Computer & Information Sciences IS 335 Database Management System Lecture 6 Query Processing and Optimization (Practice) Dr. Mourad YKHLEF The slides content is derived from many references

2 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Motivation (1) We would like to find the cheapest way to calculate the join of three tables: Sailors  Reserves  Boats

3 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Motivation (2) We need to decide on the order of operations: (Sailors  Reserves)  Boats or Sailors  (Reserves  Boats) We need to decide which join algorithm to use for each of the operations What information do we need?

4 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Statistics Maintained by DBMS for Relations Cardinality NTuples(R) : Number of tuples in each relation R Size NPages(R) : Number of pages in each relation R

5 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Statistics Maintained by DBMS for Indexes Index Cardinality: Number of distinct key values NKeys(I) for each index I Index Size: Number of pages INPages(I) in each index I Index Height: Number of non-leaf levels IHeight(I) in each B+ Tree index I Index Range: The minimum value ILow(I) and maximum value IHigh(I) for each index I

6 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Note The statistics are updated periodically ( not every time the underlying relations are modified). We cannot use the cardinality for computing select count(*) from R

7 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Estimating Result Sizes Consider The maximum number of tuples is the product of the cardinalities of the relations in the FROM clause The WHERE clause is associating a reduction factor with each term. It reflects the impact of the term in reducing result size. SELECT attribute-list FROM relation-list WHERE term 1 and... and term n

8 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Result Size Estimated result size: maximum size X the product of the reduction factors

9 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Assumptions Containment of value sets: i f NKeys(I1)<NKeys(I2) for attribute Y, then every Y-value of R will be a Y-value of S Empirically-obtained reduction factor is 1/10 if no additional info is available

10 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Estimating Reduction Factors column = value: 1/NKeys(I) –There is an index I on column. –This assumes a uniform distribution. –Otherwise, use 1/10. column1 = column2: 1/Max(NKeys(I1),NKeys(I2)) –There is an index I1 on column1 and an index I2 on column2. –Containment of value sets assumption –If only one column has an index, we use it to estimate the value. –Otherwise, use 1/10.

11 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Estimating Reduction Factors column > value: (High(I)-value)/(High(I)- Low(I)) if there is an index I on column.

12 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Example Cardinality(R) = 100,000 Cardinality(S) = 40,000 NKeys(Index on R.agent) = 100 High(Index on Rating) = 10, Low = 0 Reserves (sid, agent), Sailors(sid, rating) SELECT * FROM Reserves R, Sailors S WHERE R.sid = S.sid and S.rating > 3 and R.agent = ‘Joe’

13 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Example (cont.) Maximum cardinality: 100,000 * 40,000 Reduction factor of R.sid = S.sid: 1/40,000 –sid is a primary key of S Reduction factor of S.rating > 3: (10–3)/(10-0) = 7/10 Reduction factor of R.agent = ‘Joe’: 1/100 Total Estimated size: 700

Creating Indexes Using Oracle

15 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Index Map between –the row key –the row location Oracle has two kinds of indexes –B* tree –Bitmap Sorted

16 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef B* tree Root * 3*5* 7*14*16* 19*20*22*24*27* 29*33*34* 38* 39* 14

17 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Creating an Index Syntax: create [bitmap] [unique] index iname on table(column [, column ]...)

18 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Unique Indexes Create an index that will guarantee the uniqueness of the key. Fail if any duplicate already exists. When you create a table with a –primary key constraint or –unique constraint a "unique" index is created automatically create unique index rating_bit on Sailors(rating);

19 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Bitmap Indexes Appropriate for columns that may have very few possible values For each value c that appears in the column, a vector v of bits is created, with a 1 in v[i] if the i -th row has the value c – Vector length = number of rows Oracle can automatically convert bitmap entries to RowIDs during query processing

20 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Bitmap Indexes: Example create bitmap index rating_bit on Sailors(rating); Corresponding bitmaps: – 3: – 7: –10: SidSnameagerating 12Jim553 13John467 14Jane Sam373

21 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef When to Create an Index Large tables, on columns that are likely to appear in where clauses as a simple equality where s.sname = ‘John’ and s.age = 50 where s.age = r.age

22 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Function-Based Indexes You can't use an index on sname for the following query: select * from Sailors where UPPER(sname) = 'SAM'; You can create a function-based index to speed up the query: create index upp_sname on Sailors(UPPER(sname));

23 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Index-Organized Tables An index organized table keeps its data sorted by the primary key Rows do not have RowIDs They store their data as if they were an index create table Sailors( sid number primary key, sname varchar2(30), age number, rating number) organization index;

24 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Index-Organized Tables (2) What advantages does this have? –Primary key is not duplicated in the index –Improve performance of queries based on the primary key What disadvantages? –expensive to add columns, dynamic data When to use? –where clause on the primary key –static data

25 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Clustering Tables Together You can ask Oracle to store several tables with common columns together on the disk This is useful if you often join these tables Cluster: area on the disk where the rows of the tables are stored Cluster key: the columns by which the tables are usually joined in a query

26 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Clustering Tables Together: Syntax create cluster sailor_reserves (X number); –Create a cluster with nothing in it create table Sailors( sid number primary key, sname varchar2(30), age number, rating number) cluster sailor_reserves(sid); –create the table in the cluster

27 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Clustering Tables Together: Syntax (cont.) create index sailor_reserves_index on cluster sailor_reserves –Create an index on the cluster create table Reserves( sid number, bid number, day date, primary key(sid, bid, day) ) cluster sailor_reserves(sid); –A second table is added to the cluster

28 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Reserves sidbidday /7/ /10/ /12/96 Sailors sidsnameratingage 22Dustin Lubber Rusty Stored sidsnameratingagebidday 22Dustin /7/ /10/96 31Lubber Rusty /12/96

The Oracle Optimizer

30 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Types of Optimizers There are different modes for the optimizer RULE: Rule-based optimizer (RBO) –deprecated CHOOSE: Cost-based optimizer (CBO); picks a plan based on statistics (e.g. number of rows in a table, number of distinct keys in an index) –Need to analyze the data in the database using analyze command ALTER SESSION SET optimizer_mode = {choose|rule|first_rows(_n)|all_rows}

31 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Types of Optimizers ALL_ROWS: 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): 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

32 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef analyze table | index | compute statistics | estimate statistics [sample rows | percent] | delete statistics; analyze table Sailors estimate statistics sample 25 percent; Analyzing the Data

33 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Viewing the Execution Plan (Option 1) 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 Or C:\oracle\ora92\rdbms\admin\utlxplan.sql

34 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef PLAN_TABLE create table PLAN_TABLE ( statement_id varchar2(30), plan_id number, timestamp date, remarks varchar2(4000), operation varchar2(30), options varchar2(255), object_node varchar2(128), object_owner varchar2(30),

35 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef PLAN_TABLE object_name varchar2(30), object_alias varchar2(65), object_instance numeric, object_type varchar2(30), optimizer varchar2(255), search_columns number, id numeric, parent_id numeric, depth numeric,

36 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef PLAN_TABLE position numeric, cost numeric, cardinality numeric, bytes numeric, other_tag varchar2(255), partition_start varchar2(255), partition_stop varchar2(255), partition_id numeric, other long,

37 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef PLAN_TABLE distribution varchar2(30), cpu_cost numeric, io_cost numeric, temp_space numeric, access_predicates varchar2(4000), filter_predicates varchar2(4000), projection varchar2(4000), time numeric, qblock_name varchar2(30) );

38 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef Viewing the Execution Plan (Option 2) Another option: explain plan set statement_id='test' for SELECT * FROM Sailors S WHERE sname='Joe'; explain plan set statement_id=‘ ’ for Select … from Plan_Table where statement_id= ‘ test ’ ;

39 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef 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

40 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef 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

41 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef 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

42 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef

43 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef

44 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef

45 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef 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

46 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef 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

47 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef 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

48 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef 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

49 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef 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’;

50 IS 335 – Query Processing and Optimization - Dr. Mourad Ykhlef