sorting joins
play

Sorting & Joins Database Systems Andy Pavlo Lecture #11 - PowerPoint PPT Presentation

Sorting & Joins Database Systems Andy Pavlo Lecture #11 15-445/15-645 Computer Science Dept. Fall 2017 Carnegie Mellon Univ. ADMINISTRIVIA Homework #3 is due TODAY @ 11:59pm Homework #4 is due Wednesday October 11 th @ 11:59pm CMU


  1. Sorting & Joins Database Systems Andy Pavlo Lecture #11 15-445/15-645 Computer Science Dept. Fall 2017 Carnegie Mellon Univ.

  2. ADMINISTRIVIA Homework #3 is due TODAY @ 11:59pm Homework #4 is due Wednesday October 11 th @ 11:59pm CMU 15-445/645 (Fall 2017) 2

  3. STATUS Query Planning We will continue our discussion on how the DBMS executes Operator Execution queries. Access Methods We will focus on a couple of Buffer Pool Manager frequently used relational Disk Manager operators. CMU 15-445/645 (Fall 2017) 3

  4. TODAY'S AGENDA Sorting algorithms Join algorithms CMU 15-445/645 (Fall 2017) 4

  5. WHY DO WE NEED TO SORT? Relational model → Tuples in a table have no specific order SELECT ... ORDER BY → Users often want to retrieve tuples in a specific order → Trivial to support duplicate elimination (DISTINCT) → Bulk loading sorted tuples into a B+ tree index is faster SELECT ... GROUP BY → Sort-merge join algorithm CMU 15-445/645 (Fall 2017) 5

  6. SORTING ALGORITHMS Data fits in memory: Then we can use a standard sorting algorithm like quick-sort . Data does not fit in memory: Sorting data that does not fit in main-memory is called external sorting . CMU 15-445/645 (Fall 2017) 6

  7. EXTERNAL MERGE SORT A frequently used external sorting algorithm. Idea: Hybrid sort-merge strategy → Sorting phase: Sort small chunks of data that fit in main-memory, and then write back the sorted data to a file on disk. → Merge phase: Combine sorted sub-files into a single larger file. CMU 15-445/645 (Fall 2017) 7

  8. OVERVIEW Let’s start with a simple example: 2-way external merge sort. Later generalize it to k-way external merge sort. Files are broken up into N pages. The DBMS has a finite number of B fixed-size buffers. CMU 15-445/645 (Fall 2017) 8

  9. 2-WAY EXTERNAL MERGE SORT Pass 0: → Reads every B pages of the table into memory → Sorts them, and writes them back to disk. → Each sorted set of pages is a run Pass 1,2,3,…: → Recursively merges pairs of runs into runs twice as long → Uses three buffer pages (two for input pages, one for output) Memory Memory Memory 9 Disk CMU 15-445/645 (Fall 2017)

  10. 2-WAY EXTERNAL MERGE SORT NULL INPUT FILE 3 , 4 6 , 2 9 , 4 8 , 7 5 , 6 3 , 1 2 In each pass, we read and PASS 3 , 4 2 , 6 4 , 9 7 , 8 5 , 6 1 , 3 2 1-PAGE RUNS #0 write each page in file. 2-PAGE RUNS PASS 2 , 3 4 , 7 1 , 3 Number of passes 4 , 6 8 , 9 5 , 6 2 #1 = 1 + ⌈ log 2 N ⌉ 2 , 3 4-PAGE RUNS PASS 4 , 4 1 , 2 Total I/O cost #2 6 , 7 3 , 5 8 , 9 6 = 2N · ( # of passes ) 8-PAGE RUNS 1 , 2 Divide and conquer strategy: PASS 2 , 3 #3 3 , 4 Sort sub-files and merge 4 , 5 6 , 6 7 , 8 9 CMU 15-445/645 (Fall 2017) 10

  11. 2-WAY EXTERNAL MERGE SORT This algorithm only requires three buffer pages (B=3). Even if we have more buffer space available (B>3), it does not effectively utilize them. Let’s next generalize the algorithm to make use of extra buffer space. CMU 15-445/645 (Fall 2017) 11

  12. GENERAL EXTERNAL MERGE SORT Pass 0: Use B buffer pages. Produce ⌈ N / B ⌉ sorted runs of size B Pass 1,2,3,…: Merge B-1 runs. ( K-way merge ) Number of passes = 1 + ⌈ log B-1 ⌈ N / B ⌉ ⌉ Total I/O Cost = 2N·(# of passes) CMU 15-445/645 (Fall 2017) 12

  13. K-WAY MERGE ALGORITHM Input: K sorted sub-arrays Efficiently computes the minimum element of all K sub-arrays Repeatedly transfers that element to output array Internally maintains a heap to efficiently compute minimum element Time Complexity = O(N log 2 K) CMU 15-445/645 (Fall 2017) 13

  14. EXAMPLE Sort 108 page file with 5 buffer pages: N=108, B=5 → Pass 0: ⌈ N / B ⌉ = ⌈ 108 / 5 ⌉ = 22 sorted runs of 5 pages each (last run is only 3 pages) → Pass 1: ⌈ N ’ / B-1 ⌉ = ⌈ 22 / 4 ⌉ = 6 sorted runs of 20 pages each (last run is only 8 pages) → Pass 2: ⌈ N ’’ / B-1 ⌉ = ⌈ 6 / 4 ⌉ = 2 sorted runs, 80 pages and 28 pages → Pass 3: Sorted file of 108 pages 1+ ⌈ log B-1 ⌈ N / B ⌉ ⌉ = 1+ ⌈ log 4 22 ⌉ = 1+ ⌈ 2.229... ⌉ à 4 passes CMU 15-445/645 (Fall 2017) 14

  15. USING B+TREES Scenario: Table that must be sorted already has a B+ tree index on the sort attribute(s). Can we accelerate sorting? Idea: Retrieve tuples in desired sort order by simply traversing the leaf pages of the tree. Cases to consider: → Clustered B+ tree → Unclustered B+ tree CMU 15-445/645 (Fall 2017) 15

  16. CASE 1: CLUSTERED B+TREE Index Traverse to the left-most leaf (Directs search) Data Entries page, and then retrieve tuples ("Sequence set") from all leaf pages. Always better than external 101 102 103 104 sorting. Good idea! Data Records CMU 15-445/645 (Fall 2017) 16

  17. CASE 2: UNCLUSTERED B+TREE Index Chase each pointer to the (Directs search) Data Entries page that contains the data. ("Sequence set") In general, one I/O per data record. Bad idea!! 101 102 103 104 Data Records CMU 15-445/645 (Fall 2017) 17

  18. ALTERNATIVES TO SORTING What if we don’t need the data to be ordered? → Forming groups in GROUPBY (no ordering) → Removing duplicates in DISTINCT (no ordering) Can we remove duplicates without sorting? → Hashing is a better alternative in this scenario → Only need to remove duplicates, no need for ordering → Can be computationally cheaper than sorting! CMU 15-445/645 (Fall 2017) 18

  19. SORTING: SUMMARY External merge sort minimizes disk I/O → Pass 0: Produces sorted runs of size B → Later Passes: Recursively merge runs Next week: Query optimizer picks a sorting or hashing operator based on ordering requirements in the query plan. CMU 15-445/645 (Fall 2017) 19

  20. TODAY'S AGENDA Sorting algorithms Join algorithms CMU 15-445/645 (Fall 2017) 20

  21. WHY DO WE NEED TO JOIN? Relational model → Unnecessary repetition of information must be avoided → We decompose tables using normalization theory SELECT...JOIN → Reconstruct original tables via joins → No information loss CMU 15-445/645 (Fall 2017) 21

  22. Anybody here into sailing? CMU 15-445/645 (Fall 2017) 22

  23. SAILING CLUB DATABASE SAILORS RESERVES SID SNAME RATING AGE SID BID DAY RNAME 1 Andy 999 45.0 6 103 2014-02-01 Matlock 3 Obama 50 52.0 1 102 2014-02-02 Macgyver 2 Tupac 32 26.0 2 101 2014-02-02 A-team 6 Bieber 10 19.0 1 101 2014-02-01 Dallas Sailors(sid: int, sname: varchar, rating: int, age: real) Reserves(sid: int, bid: int, day: date, rname: varchar) CMU 15-445/645 (Fall 2017) 23

  24. SAILING CLUB DATABASE SAILORS RESERVES SID SNAME RATING AGE SID BID DAY RNAME 1 Andy 999 45.0 6 103 2014-02-01 Matlock 3 Obama 50 52.0 1 102 2014-02-02 Macgyver 2 Tupac 32 26.0 2 101 2014-02-02 A-team 6 Bieber 10 19.0 1 101 2014-02-01 Dallas Each tuple is 50 bytes Each tuple is 40 bytes 80 tuples per page 100 tuples per page 500 pages total 1000 pages total N=500, p S =80 M=1000, p R =100 CMU 15-445/645 (Fall 2017) 24

  25. JOIN VS CROSS-PRODUCT R ⨝ S is very common and thus must be carefully optimized. R × S followed by a selection is inefficient because the cross-product is large. There are many algorithms for reducing join cost, but no particular algorithm works well in all scenarios. CMU 15-445/645 (Fall 2017) 25

  26. JOIN ALGORITHMS Join algorithms we will cover in today’s lecture: → Simple Nested Loop Join → Block Nested Loop Join → Index Nested Loop Join → Sort-Merge Join → Hash Join (next lecture) CMU 15-445/645 (Fall 2017) 26

  27. I/O COST ANALYSIS Assume: → M pages in R, p R tuples per page, m tuples total → N pages in S, p S tuples per page, n tuples total → In our examples, R is Reserves and S is Sailors. We will consider more complex join conditions later. Cost metric: # of I/Os We will ignore output costs CMU 15-445/645 (Fall 2017) 27

  28. JOIN QUERY EXAMPLE SELECT * FROM Reserves R, Sailors S WHERE R.sid = S.sid Assume that we don’t know anything about the tables and we don’t have any indexes. CMU 15-445/645 (Fall 2017) 28

  29. JOIN ALGORITHMS Join algorithms we will cover: → Simple Nested Loop Join → Block Nested Loop Join → Index Nested Loop Join → Sort-Merge Join CMU 15-445/645 (Fall 2017) 29

  30. SIMPLE NESTED LOOP JOIN foreach tuple r of R foreach tuple s of S output, if r and s match R(A,..) S(A, ......) CMU 15-445/645 (Fall 2017) 30

  31. SIMPLE NESTED LOOP JOIN Outer table foreach tuple r of R foreach tuple s of S output, if r and s match Inner table R(A,..) S(A, ......) CMU 15-445/645 (Fall 2017) 31

  32. SIMPLE NESTED LOOP JOIN Why is this algorithm bad? → For every tuple in R, it scans S once Number of disk accesses → Cost: M + (m · N) R(A,..) S(A, ......) M pages, m tuples N pages, n tuples CMU 15-445/645 (Fall 2017) 32

  33. SIMPLE NESTED LOOP JOIN Actual number: → M + (m · N) = 1000 + (100 · 1000) · 500 ≈ 50 M I/Os → At 0.1 ms/IO, Total time ≈ 1.3 hours What if smaller table (S) is used as the outer table? → N + (n · M) = 500 + (80 ·500) · 1000 ≈ 40 M I/Os → Slightly better. What assumptions are being made here? → 2 buffers for streaming the tables (and 1 for storing output) CMU 15-445/645 (Fall 2017) 33

  34. JOIN ALGORITHMS Join algorithms we will cover: → Simple Nested Loop Join → Block Nested Loop Join → Index Nested Loop Join → Sort-Merge Join CMU 15-445/645 (Fall 2017) 34

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend