CS 540 Database Management Systems

Slides:



Advertisements
Similar presentations
Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
Advertisements

CS 245Notes 71 CS 245: Database System Principles Notes 7: Query Optimization Hector Garcia-Molina.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Algorithms for SELECT and JOIN Operations (8) Implementing the JOIN Operation: Join (EQUIJOIN, NATURAL.
CS 540 Database Management Systems
1 Lecture 23: Query Execution Friday, March 4, 2005.
Join Processing in Database Systems with Large Main Memories ACM Transactions on Database Systems Vol. 11, No. 3, Sep 1986 Leonard D. Shapiro Donghui Zhang,
Lecture 13: Query Execution. Where are we? File organizations: sorted, hashed, heaps. Indexes: hash index, B+-tree Indexes can be clustered or not. Data.
CS CS4432: Database Systems II Operator Algorithms Chapter 15.
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.
CS 245Notes 71 CS 245: Database System Principles Notes 7: Query Optimization Hector Garcia-Molina.
Lecture 24: Query Execution Monday, November 20, 2000.
CSCI 5708: Query Processing II Pusheng Zhang University of Minnesota Feb 5, 2004.
1  Simple Nested Loops Join:  Block Nested Loops Join  Index Nested Loops Join  Sort Merge Join  Hash Join  Hybrid Hash Join Evaluation of Relational.
SPRING 2004CENG 3521 Join Algorithms Chapter 14. SPRING 2004CENG 3522 Schema for Examples Similar to old schema; rname added for variations. Reserves:
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
1 Lecture 22: Query Execution Wednesday, March 2, 2005.
Query Optimization 3 Cost Estimation R&G, Chapters 12, 13, 14 Lecture 15.
Introduction to Database Systems 1 Join Algorithms Query Processing: Lecture 1.
1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
CS 4432query processing - lecture 171 CS4432: Database Systems II Lecture #17 Join Processing Algorithms (cont). Professor Elke A. Rundensteiner.
Evaluation of Relational Operations. Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation.
Query Execution Chapter 15 Section 15.1 Presented by Khadke, Suvarna CS 257 (Section II) Id
Sorting and Query Processing Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 29, 2005.
1 Relational Operators. 2 Outline Logical/physical operators Cost parameters and sorting One-pass algorithms Nested-loop joins Two-pass algorithms.
CPS216: Advanced Database Systems Notes 07:Query Execution Shivnath Babu.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
CS4432: Database Systems II Query Processing- Part 3 1.
CS411 Database Systems Kazuhiro Minami 11: Query Execution.
CS 257 Chapter – 15.9 Summary of Query Execution Database Systems: The Complete Book Krishna Vellanki 124.
Lecture 24 Query Execution Monday, November 28, 2005.
Multi pass algorithms. Nested-Loop joins Tuple-Based Nested-loop Join Algorithm: FOR each tuple s in S DO FOR each tuple r in R DO IF r and s join to.
CS4432: Database Systems II Query Processing- Part 2.
CSCE Database Systems Chapter 15: Query Execution 1.
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations – Join Chapter 14 Ramakrishnan and Gehrke (Section 14.4)
Query Processing CS 405G Introduction to Database Systems.
Lecture 17: Query Execution Tuesday, February 28, 2001.
Query Execution. Where are we? File organizations: sorted, hashed, heaps. Indexes: hash index, B+-tree Indexes can be clustered or not. Data can be stored.
CS 440 Database Management Systems Lecture 5: Query Processing 1.
Lecture 3 - Query Processing (continued) Advanced Databases Masood Niazi Torshiz Islamic Azad university- Mashhad Branch
Computing & Information Sciences Kansas State University Wednesday, 08 Nov 2006CIS 560: Database System Concepts Lecture 32 of 42 Monday, 06 November 2006.
Relational Operator Evaluation. overview Projection Two steps –Remove unwanted attributes –Eliminate any duplicate tuples The expensive part is removing.
Implementation of Database Systems, Jarek Gryz1 Evaluation of Relational Operations Chapter 12, Part A.
Query Execution Query compiler Execution engine Index/record mgr. Buffer manager Storage manager storage User/ Application Query update Query execution.
Tallahassee, Florida, 2016 COP5725 Advanced Database Systems Query Processing Spring 2016.
Query Processing and Query Optimization Database System Implementation CSE 507 Some slides adapted from Silberschatz, Korth and Sudarshan Database System.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Evaluation of Relational Operations Chapter 14, Part A (Joins)
1 Lecture 23: Query Execution Monday, November 26, 2001.
Query Processing COMP3017 Advanced Databases Nicholas Gibbins
CS4432: Database Systems II Query Processing- Part 1 1.
CS 540 Database Management Systems
CS 440 Database Management Systems
Database Management System
Database Applications (15-415) DBMS Internals- Part VII Lecture 16, October 25, 2016 Mohammad Hammoud.
Evaluation of Relational Operations
Evaluation of Relational Operations: Other Operations
Database Applications (15-415) DBMS Internals- Part VII Lecture 19, March 27, 2018 Mohammad Hammoud.
Lecture 2- Query Processing (continued)
Slides adapted from Donghui Zhang, UC Riverside
Lecture 13: Query Execution
CS505: Intermediate Topics in Database Systems
Overview of Query Evaluation: JOINS
Lecture 22: Query Execution
Sorting We may build an index on the relation, and then use the index to read the relation in sorted order. May lead to one disk block access for each.
Lecture 22: Query Execution
Lecture 11: B+ Trees and Query Execution
Lecture 22: Friday, November 22, 2002.
Lecture 24: Query Execution
Lecture 20: Query Execution
Presentation transcript:

CS 540 Database Management Systems Lecture 5: Query Processing

DBMS Architecture User/Web Forms/Applications/DBA query transaction Query Parser Transaction Manager Query Rewriter Today’s lecture Logging & Recovery Query Optimizer Lock Manager Query Executor Files & Access Methods Buffers Lock Tables Buffer Manager Main Memory Storage Manager Storage

Query Execution Plans   Query Plan: logical plan (declarative) manf SELECT B. manf FROM Beers B, Sells S WHERE B.name=S.beer AND S.price < 20  price < 20 ( nested loops) (Table scan) (Index scan) name=beer Query Plan: logical plan (declarative) physical plan (procedural) – procedural implementation of each logical operator – scheduling of operations Beers Sells

Logical versus physical operators Logical operators Relational Algebra Operators Join, Selection, Projection, Union, … Physical operators Algorithms to implement logical operators. Hash join, nested loop join, … More than one physical operator for each logical operator

Communication between operators: iterator model Each physical operator implements three functions: Open: initializes the data structures. GetNext: returns the next tuple in the result. Close: ends the operation and frees the resources. It enables pipelining

Physical operators Logical operator: selection read the entire or selected tuples of relation R. tuples satisfy some predicate Table-scan: R resides in the secondary storage, read its blocks one by one. Index-scan: If there is an index on R, use the index to find the blocks. more efficient Other operators for join, union, group by, ... join is the most important one. focus of our lecture

Both relations fit in main memory Internal memory join algorithms Nested-loop join: check for every record in R and every record in S; time = O(|R||S|) Sort-merge join: sort R and S followed by merging; time = O(|S|*log|S|) (if |R|<|S|) Hash join: build a hash table for R; for every record in S, probe the hash table; time =O(|S|) (if |R|<|S|)

External memory join algorithms At least one relation does not fit into main memory I/O access is the dominant cost B(R): number of blocks of R. |R| or T(R) : number of tuples in R. Memory requirement M: number of blocks that fit in main memory Example: internal memory join algorithms : B(R) + B(S) We do not consider the cost of writing the output. The results may be pipelined and never written to disk.

Nested-loop join of R and S For each block of R, and for each tuple r in the block: For each block of S, and for each tuple s in the block: Output rs if join condition evaluates to true over r and s R is called the outer table; S is called the inner table cost: B(R) + |R| · B(S) Memory requirement: 4 (if double buffering is used) block-based nested-loop join - For each block of R, and for each block of S: For each r in the R block, and for each s in the S block: … cost: B(R) + B(R) · B(S) Memory requirement: 4 (if double buffering is used)

Improving nested-loop join Use up the available memory buffers M Read M - 2 blocks from R Read blocks of S one by one and join its tuples with R tuples in main memory Cost: B(R) + [ B(R) / (M – 2) ] B(S) almost B(R) B(S) / M Memory requirement: M

Index-based (zig-zag) join Join R and S on R.A = S.B Use ordered indexes over R.A and S.B to join the relations. B+ tree Use current indexes or build new ones. Similar to sort-merge join without sorting step. Use the larger value to probe the other index

Two pass, multi-way merge sort . . . Problem: sort relation R that does not fit in main memory Phase 1: Read R in groups of M blocks, sort, and write them as runs of size M on disk. Main memory Disk Relation R runs 1 2 M-1 . . . B(R) M Buffers . . . . . .

Two pass, multi-way merge sort . . . Phase 2: Merge M – 1 blocks at a time and write the results to disk. Read one block from each run. Keep one block for the output. Relation R (sorted) Disk runs 1 2 M-1 M-1 Buffers 1 Output buffer 2 . . . . . . B(R) Disk Main Memory

Two pass, multi-way merge Sort Cost: 2B(R) in the first pass + B(R) in the second pass. Memory requirement: M B(R) <= M (M – 1) or simply B(R) <= M2

General multi-way merge sort Pass 0: read M blocks of R at a time, sort them, and write out a level-0 run There are [B(R) / M ] level-0 sorted runs Pass i: merge (M – 1) level-(i-1) runs at a time, and write out a level-i run (M – 1) memory blocks for input, 1 to buffer output # of level-i runs = # of level-(i–1) runs / (M – 1) Final pass produces 1 sorted run

Example of general multi-way merge sort Input: 1, 7, 4, 5, 2, 8, 9, 6, 3, 0 Each block holds one number, and memory has 3 blocks Pass 0 1, 7, 4 ->1, 4, 7 5, 2, 8 -> 2, 5, 8 9, 6, 3 -> 3, 6, 9 0 -> 0 Pass 1 1, 4, 7 + 2, 5, 8 -> 1, 2, 4, 5, 7, 8 3, 6, 9 + 0 -> 0, 3, 6, 9 Pass 2 (final) 1, 2, 4, 5, 7, 8 + 0, 3, 6, 9 -> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Analysis of multi-way merge sort Number of passes: cost #passes · 2 · B(R): each pass reads the entire relation once and writes it once Subtract B(R) for the final pass Simply O( B(R) · log M B(R) ) Memory requirement: M

Sort-merge join algorithm Sort R and S according to the join attribute, then merge them r, s = the first tuples in sorted R and S Repeat until one of R and S is exhausted: If r.A > s.B then s = next tuple in S else if r.A < s.B then r = next tuple in R else output all matching tuples, and r, s = next in R and S Cost: sorting + 2 B(R)+ 2 B(S) (join on foreign key primary key) What if more than M blocks match on join attribute? use nested loop join algorithm B(R) B(S) if everything joins Memory Requirement: B(R) <= M2 , B(S) <= M2

Optimized sort-merge join algorithm Combine join with the merge phase of sort Sort R and S in M runs (overall) of size M on disk. Merge and join the tuples in one pass. Disk Runs of R and S R S merge . . . join merge . . . Main Memory

Optimized two-pass sort-merge join algorithm Cost: 3B(R) + 3B(S) Memory Requirement: B(R) + B(S) <= M2 because we merge them in one pass More efficient but more strict requirement.

(Partitioned) Hash join or R and S Step 1: Hash S into M buckets send all buckets to disk Step 2 Hash R into M buckets Send all buckets to disk Step 3 Join corresponding buckets If tuples of R and S are not assigned to corresponding buckets, they do not join

bucket Ri ( < M-1 pages) Hash Join M main memory buffers Disk Original Relation OUTPUT 2 INPUT 1 hash function h M-1 Buckets . . . Partition both relations using hash fn h: R tuples in partition i will only match S tuples in partition i. Buckets of R & S Input buffer for Si Hash table for bucket Ri ( < M-1 pages) M main memory buffers Disk Output buffer Join Result Read in a partition of R, hash it using h2 (<> h!). Scan matching partition of S, search for matches. R hash fn h2 h2 S 14

Bucket Ri ( < M-1 pages) Hash join Cost: 3 B(R) + 3 B(S). Memory Requirement: The smaller bucket must fit in main memory. Let min( B(R), B(S)) = B(R) B(R) / (M – 1) <= M, roughly B(R) <= M2 Buckets of R & S Input buffer for Si Bucket Ri ( < M-1 pages) M main memory buffers Disk Output buffer Join Result 23

Hybrid Hash join When partitioning S, keep the records of the first bucket in memory as a hash table; When partitioning R, for records of the first bucket, probe the hash table directly; Saving: no need to write R1 and S1 to disk or read them back to memory.

Handle partition overflow Overflow on disk: an R partition is larger than memory size Solution: recursive partition.

Hash-based versus sort-based join Hash join need smaller amount of main memory sqrt (min(B(R), B(S))) < sqrt (B(R) + B(S) ) Hash join wins if the relations have different sizes Hash join performance depends on the quality of hashing It may be hard to generate balanced buckets for hash join Sort-based join wins if the relations are in sorted order Sort-based join generates sorted results useful when there is Order By in the query useful the following operators need sorted input Sort-based join can handle inequality join predicates

Duality of Sort and Hash Divide-and-conquer paradigm Handling very large inputs Sorting: multi-level merge Hashing: recursive partitioning Sorting: physical division, logical combination Hashing: logical division, physical combination

What you should know How nested-loop, zig-zag, sort-merge, and hash join processing algorithms work How to compute their costs and memory requirements What are their advantages and disadvantages

Carry Away Messages Old conclusions/assumptions regularly need to be re-examined because of the change in the world System R abandoned hash join, but the availability of large main memories changed the story In general, changes in the world bring opportunities for innovations, so be alert about any changes How have the needs for data management changed over time? Have the technologies for data management been tracking such changes?