Overview of Query Evaluation. Overview of Query Evaluation

Similar documents
Relational Algebra and SQL

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Chapter 5. SQL: Queries, Constraints, Triggers

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

Database Management Systems. Chapter 1

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

SQL: Queries, Programming, Triggers

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Lecture 6: Query optimization, query tuning. Rasmus Pagh

Chapter 13: Query Processing. Basic Steps in Query Processing

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

Overview of Storage and Indexing

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

D B M G Data Base and Data Mining Group of Politecnico di Torino

3. Relational Model and Relational Algebra

Overview of Storage and Indexing. Data on External Storage. Alternative File Organizations. Chapter 8

Comp 5311 Database Management Systems. 16. Review 2 (Physical Level)

SQL Query Evaluation. Winter Lecture 23

Query Processing C H A P T E R12. Practice Exercises

Θεµελίωση Βάσεων εδοµένων

Advanced Oracle SQL Tuning

Why Query Optimization? Access Path Selection in a Relational Database Management System. How to come up with the right query plan?

Inside the PostgreSQL Query Optimizer

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Datenbanksysteme II: Implementation of Database Systems Implementing Joins

Evaluation of Expressions

Relational Database: Additional Operations on Relations; SQL

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Physical Design. Phases of database design. Physical design: Inputs.

Chapter 13: Query Optimization

Oracle Database 11g: SQL Tuning Workshop

Chapter 14: Query Optimization

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Best Practices for DB2 on z/os Performance

University of Aarhus. Databases IBM Corporation

Physical Database Design and Tuning

1. Physical Database Design in Relational Databases (1)

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

Oracle Database 11g: SQL Tuning Workshop Release 2

Performance Tuning for the Teradata Database

Data Warehousing und Data Mining

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals

SQL Server Query Tuning

Query-by-Example (QBE)

EFFICIENT EXTERNAL SORTING ON FLASH MEMORY EMBEDDED DEVICES

Partitioning under the hood in MySQL 5.5

Oracle Database 10g: Introduction to SQL

Big Data Technology Map-Reduce Motivation: Indexing in Search Engines

Proposed solutions Project Assignment #1 (SQL)

Oracle SQL. Course Summary. Duration. Objectives

White Paper. Optimizing the Performance Of MySQL Cluster

Query Processing. Q Query Plan. Example: Select B,D From R,S Where R.A = c S.E = 2 R.C=S.C. Himanshu Gupta CSE 532 Query Proc. 1

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Oracle EXAM - 1Z Oracle Database 11g Release 2: SQL Tuning. Buy Full Product.

In-Memory Database: Query Optimisation. S S Kausik ( ) Aamod Kore ( ) Mehul Goyal ( ) Nisheeth Lahoti ( )

CSE132A Solutions HW 1

DATABASE DESIGN - 1DL400

Scalable Data Analysis in R. Lee E. Edlefsen Chief Scientist UserR! 2011

1Z0-117 Oracle Database 11g Release 2: SQL Tuning. Oracle

MOC 20461C: Querying Microsoft SQL Server. Course Overview

SQL: QUERIES, CONSTRAINTS, TRIGGERS

Distributed Aggregation in Cloud Databases. By: Aparna Tiwari

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

Execution Plans: The Secret to Query Tuning Success. MagicPASS January 2015

Guide to Performance and Tuning: Query Performance and Sampled Selectivity

IT2305 Database Systems I (Compulsory)

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Oracle Database 12c: Introduction to SQL Ed 1.1

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

DBMS / Business Intelligence, SQL Server

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Data Warehousing and Decision Support. Introduction. Three Complementary Trends. Chapter 23, Part A

DATA STRUCTURES USING C

Query Optimization for Distributed Database Systems Robert Taylor Candidate Number : Hertford College Supervisor: Dr.

Efficient Processing of Joins on Set-valued Attributes

Evaluation of view maintenance with complex joins in a data warehouse environment (HS-IDA-MD )

Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

Lecture 1: Data Storage & Index

In this session, we use the table ZZTELE with approx. 115,000 records for the examples. The primary key is defined on the columns NAME,VORNAME,STR

Parallel Databases. Parallel Architectures. Parallelism Terminology 1/4/2015. Increase performance by performing operations in parallel

DB2 for Linux, UNIX, and Windows Performance Tuning and Monitoring Workshop

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

14 Databases. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

ENHANCEMENTS TO SQL SERVER COLUMN STORES. Anuhya Mallempati #

16.1 MAPREDUCE. For personal use only, not for distribution. 333

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Introduction to Microsoft Jet SQL

MapReduce Jeffrey Dean and Sanjay Ghemawat. Background context

Safe Harbor Statement

Raima Database Manager Version 14.0 In-memory Database Engine

Data Management in the Cloud

Query Optimization in a Memory-Resident Domain Relational Calculus Database System

Introducing Cost Based Optimizer to Apache Hive

Query tuning by eliminating throwaway

Introduction to IR Systems: Supporting Boolean Text Search. Information Retrieval. IR vs. DBMS. Chapter 27, Part A

Transcription:

Overview of Query Evaluation Chapter 12 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Overview of Query Evaluation Plan: Tree of R.A. ops, with choice of alg for each op. Each operator is typically implemented using a `pull interface: when an operator is `pulled for the next output tuples, it `pulls on its inputs and computes them. Two main issues in query optimization: For a given query, what plans are considered? Algorithm to search plan space for the cheapest (estimated) plan. How is the cost of a plan estimation? Ideally: Want to find the best plan. Practically: Avoid worst plans! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Overview of Query Evaluation SELECT S.sname FROM Reserves R, Sailors S WHERE R.sid=S.sid AND R.bid=100 AND S.rating>5 RA Tree: sname bid=100 rating > 5 sid=sid Reserves Sailors Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Cost Estimation For each plan considered, must estimate cost: Must estimate cost of each operation in plan tree. Depends on input cardinalities. Must also estimate size of result for each operation in tree! Use information about the input relations. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Relational Algebra Basic operations: Selection ( ) Selects a subset of rows from a relation. Projection ( ) Deletes unwanted columns from relation. Cross-product ( ) Allows us to combine two relations. Set-difference ( ) Tuples in reln. 1, but not in reln. 2. Union ( ) Tuples in reln. 1 or in reln. 2 (or both). Additional operations: Intersection, join, division, renaming: Not essential, but (very!) useful. Since each operation returns a relation, operations can be composed! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Statistics and Catalogs Need information about the relations and indexes involved. Catalogs typically contain at least: # tuples (NTuples) and # pages (NPages) for each relation. # distinct key values (NKeys) and NPages for each index. Index height, low/high key values (Low/High) for each tree index. Catalogs updated periodically. Updating whenever data changes is too expensive; lots of approximation anyway, so slight inconsistency ok. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

Size Estimation and Reduction Factors Consider a query block: SELECT attribute list FROM relation list WHERE term1 AND... AND termk Maximum # tuples in result is the product of the cardinalities of relations in the FROM clause. Reduction factor (RF) associated with each term reflects the impact of the term in reducing result size. Result cardinality = Max # tuples * product of all RF s. Implicit assumption that terms are independent! Term col=value has RF 1/NKeys(I), given index I on col Term col1=col2 has RF 1/MAX(NKeys(I1), NKeys(I2)) Term col>value has RF (High(I)-value)/(High(I)-Low(I)) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Some Common Techniques Algorithms for evaluating relational operators use some simple ideas extensively: Indexing: Can use WHERE conditions to retrieve small set of tuples (selections, joins) Iteration: Sometimes, faster to scan all tuples even if there is an index. (And sometimes, we can scan the data entries in an index instead of the table itself.) Partitioning: By using sorting or hashing, we can partition the input tuples. * Watch for these techniques as we discuss query evaluation! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

Access Path An access path is a method of retrieving tuples: File scan, or index that matches a selection (in the query) A tree index matches (a conjunction of) terms that involve only attributes in a prefix of the search key. E.g., Tree index on <a, b, c> matches the selection a=5 AND b=3, and a=5 AND b>6, but not b=3. A hash index matches (a conjunction of) terms that has a term attribute = value for every attribute in the search key of the index. E.g., Hash index on <a, b, c> matches a=5 AND b=3 AND c=5; but it does not match b=3, or a=5 AND b=3, or a>5 AND b=3 AND c=5. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 One Approach to Selections Find the most selective access path, retrieve tuples using it, and apply any remaining terms that do not match the index: Most selective access path: An index or file scan that we estimate will require the fewest page I/Os. Terms that match this index reduce the number of tuples retrieved; other terms are used to discard some retrieved tuples, but do not affect number of tuples/pages fetched. Consider day<8/1/2016 AND bid=5 AND sid=3. A B+ tree index on day can be used; then, bid=5 and sid=3 must be checked for each retrieved tuple. Similarly, a hash index on <bid, sid> could be used; day<8/1/2016> must then be checked. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

Relational Operation - Selection No indexes => scan the file With an index, cost depends on #qualifying tuples, and clustering. Cost of finding qualifying data entries (typically small) plus cost of retrieving records (could be large w/o clustering). In example, assuming uniform distribution of names, about 10% of tuples qualify (100 pages, 10000 tuples). With a clustered B+ tree index, cost is little more than 100 I/Os; if unclustered, up to 10000 I/Os! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Projection SELECT DISTINCT R.sid, R.bid FROM Reserves R The expensive part is removing duplicates. SQL systems don t remove duplicates unless the keyword DISTINCT is specified in a query. Sorting Approach: Sort on <sid, bid> and remove duplicates. (Can optimize this by dropping unwanted information while sorting.) Hashing Approach: Hash on <sid, bid> to create partitions. Load partitions into memory one at a time and eliminate duplicates. If there is an index with both R.sid and R.bid in the search key, may be cheaper to sort data entries in the index. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12

Query Optimizer Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 Schema for Examples Sailors (sid: integer, sname: string, rating: integer, age: real) Reserves (sid: integer, bid: integer, day: dates, rname: string) Similar to old schema; rname added for variations. Reserves: Each tuple is 40 bytes long, 100 tuples per page, 1000 pages. Sailors: Each tuple is 50 bytes long, 80 tuples per page, 500 pages. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

Motivating Example Cost: 1000+1000*500 I/Os By no means the worst plan! Plan: Misses several opportunities: selections could have been `pushed earlier, no use is made of any available indexes, etc. Goal of optimization: To find more efficient plans that compute the same answer. SELECT S.sname FROM Reserves R, Sailors S WHERE R.sid=S.sid AND R.bid=100 AND S.rating>5 RA Tree: sname sid=sid bid=100 rating > 5 Reserves bid=100 rating > 5 sname sid=sid (On-the-fly) (On-the-fly) (Nested Loops) Sailors Reserves Sailors Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Alternative Plan sname (On-the-fly) (Sort-Merge Join) sid=sid Main difference: push selects. With 5 buffers, cost of plan: (Scan; write to temp T1) bid=100 Reserves Scan Reserves (1000) + write temp T1 (10 pages, if we have one hundred boats, uniform distribution). Scan Sailors (500) + write temp T2 (250 pages, if we have 10 ratings). Sort T1 (2*2*10), sort T2 (2*4*250), merge (10+250) Total: 4060 page I/Os. If we `push projections, T1 has only sid, T2 only sid and sname: The cost of the join step drops significantly, total < 2000. (Scan; rating > 5 write to temp T2) Sailors Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

Summary There are several alternative evaluation algorithms for each relational operator. A query is evaluated by converting it to a tree of operators and evaluating the operators in the tree. Must understand query optimization in order to fully understand the performance impact of a given database design (relations, indexes) on a workload (set of queries). Two parts to optimizing a query: Consider a set of alternative plans. Must prune search space; typically, left-deep plans only. Must estimate cost of each plan that is considered. Must estimate size of result and cost for each plan node. Key issues: Statistics, indexes, operator implementations. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17