Presentation is loading. Please wait.

Presentation is loading. Please wait.

BTrees & Sorting 11/3. Announcements I hope you had a great Halloween. Regrade requests were due a few minutes ago…

Similar presentations


Presentation on theme: "BTrees & Sorting 11/3. Announcements I hope you had a great Halloween. Regrade requests were due a few minutes ago…"— Presentation transcript:

1 BTrees & Sorting 11/3

2 Announcements I hope you had a great Halloween. Regrade requests were due a few minutes ago…

3 Indexing “If you don’t find it in the index, look very carefully through the entire catalog” -- Sears, Roebuck, and Co., Consumers Guide, 1897 “If you don’t find it in the index, look very carefully through the entire catalog” -- Sears, Roebuck, and Co., Consumers Guide, 1897

4 Index Motivation A file contains some records, say products We want faster access to those records – E.g., Give me all products made by Sony Intuition: Build a second file that organizes the records “by product” to make this faster – NB: we don’t always have to build a second file

5 Indexes An index on a file speeds up selections on the search key fields for the index. – Search key properties Any subset of fields is not the same as key of a relation Product(name, maker, price) On which attributes would you build indexes?

6 More precisely An index contains a collection of data entries, and supports efficient retrieval of all data entries k* with a given key value k. Product(name, maker, price) Sample queries? Indexing is one the most important facilities provided by a database for performance

7 What’s stored in an index? Three main options: a key k maps to a data entry which is 1.k*, the actual record 2.(k,rid), the key plus the record id 3.(k, rid list) To avoid duplication of records, we usually only have one index that uses choice 1

8 Operations on an Index Search: Given a key find all records – More sophisticated variants as well. Why? Insert /Remove entries – Bulk Load. Why? Real difference between structures: costs of ops determines which index you pick and why

9 Data File with Several Index Files 1180 1210 1220 1375 NameAgeSal Bob1210 Cal1180 Joe1220 Sue1375 11 12 13 1012 2012 7513 8011 80 10 20 75 Equality Query: Age = 12 and Sal = 90? Range Query: Age = 5 and Sal > 5? Composite keys in Dictionary Order

10 High-level of Index Structures

11 Outline Btrees – Very good for range queries, sorted data – Some old databases only implemented Btrees Hash Tables – There are variants of this basic structure to deal with IO The data structures we present here are “IO aware”

12 B+ Trees Search trees – B does not mean binary! Idea in B Trees: – make 1 node = 1 physical page – Balanced, height adjusted tree (not the B either) Idea in B+ Trees: – Make leaves into a linked list (range queries)

13 Each node has >= d and <= 2d keys (except root) B+ Trees Basics 30120240 Keys k < 30 Keys 30<=k<120Keys 120<=k<240 Keys 240<=k 405060 405060 Next leaf Each leaf has >=d and <= 2d keys: Parameter d = the degree Internal Nodes Leaf Nodes

14 B+ Tree Example 80 2060100120140 101518203040506065808590 101518203040506065808590 d = 2 1. No data in internal nodes. 2. Links between leaf pages.

15 Searching a B+ Tree Exact key values: – Start at the root – Proceed down, to the leaf Range queries: – As above – Then sequential traversal Select name From people Where age = 25 Select name From people Where 20 <= age and age <= 30

16 B+ Tree Example 80 2060100120140 101518203040506065808590 101518203040506065808590 K = 30? 30 < 80. 30 in [20,60) To the data!

17 B+ Tree Example 80 2060100120140 101518203040506065808590 101518203040506065808590 K in [30,85] 30 < 80. 30 in [20,60) To the data! Use those leaf pointers!

18 B+ Tree Design How large is d ? Example: – Key size = 4 bytes – Pointer size = 8 bytes – Block size = 4096 byes 2d x 4 + (2d+1) x 8 <= 4096 d = 170 Observable Universe contains ≈ 10 80 atoms. What is height of a B+tree that indexes it? NB: Oracle allows 64k pages TiB is 2 40 bytes. What is the height to index with 64k Pages?

19 B+ Trees in Practice Typical order: 100. Typical fill-factor: 67%. – average fanout = 133 Typical capacities: – Height 4: 133 4 = 312,900,700 records – Height 3: 133 3 = 2,352,637 records Top levels of tree sit in the buffer pool: – Level 1 = 1 page = 8 Kbytes – Level 2 = 133 pages = 1 Mbyte – Level 3 = 17,689 pages = 133 MBytes Typically, pay for one IO!

20 Insert!

21 Insertion in a B+ Tree Insert (K, P) Find leaf where K belongs, insert If no overflow (2d keys or less), halt If overflow (2d+1 keys), split node, insert in parent: If leaf, keep K3 too in right node When root splits, new root has 1 key only K1K2K3K4K5 P0P1P2P3P4p5 K1K2 P0P1P2 K4K5 P3P4p5 (K3, ) to parent

22 Insertion in a B+ Tree 80 2060100120140 101518203040506065808590 101518203040506065808590 Insert K=19

23 Insertion in a B+ Tree 80 2060100120140 10151819203040506065808590 10151820304050606580859019 After insertion

24 Insertion in a B+ Tree 80 2060100120140 10151819203040506065808590 10151820304050606580859019 Now insert 25

25 Insertion in a B+ Tree 80 2060100120140 1015181920253040506065808590 10151820253040606580859019 After insertion 50

26 Insertion in a B+ Tree 80 2060100120140 1015181920253040506065808590 10151820253040606580859019 But now have to split ! 50

27 Insertion in a B+ Tree 80 203060100120140 1015181920256065808590 10151820253040606580859019 After the split 50 304050

28 Key concepts (exam) How to search in a B+tree – which pages are touched Understanding the impact of various design decisions. Will not ask for the details of the insert algorithm, but do need to know it remains balanced.

29 Clustered Indexes

30 Index Classification An index is clustered if the data is ordered in the same way as the underlying data.

31 Clustered vs. Unclustered Index Index entries direct search Data entries (Index File) (Data file) Data Records CLUSTERED UNCLUSTERED Clustered (or not) dramatically impacts cost

32 A Simple Cost Models

33 Operations on an Index Search: Given a key find all records – More sophisticated variants as well. Real difference between structures: costs of ops which index you pick and why

34 Cost Model for Our Analysis We ignore CPU costs, for simplicity: – N: The number of records – R: Number of records per page Measure number of page I/O’s Goal: Good enough to show the overall trends…

35 Clustered v. Unclustered Fanout of Tree is F. Range query for M entries (100 per page) IOs to search for a single item? Traversal of the tree: log F (1.5N) Range search Query : log F (1.5N) + ceil(M/100) Traversal of the tree: log F (1.5N) Range search Query : log F (1.5N) + M Unclustered Clustered Which of these IOs are random/sequential?

36 Plugging in Some numbers Clustered: log F (1.5N) + ceil(M/100) ~ 1 Random IO (10ms) Unclustered: log F (1.5N) + M Random IO (M*10ms) If M is 1 then there is no difference! If M is 100,000 records, ~10 minutes vs. 10ms

37 Takeaway B+Tree are a workhorse index. You can write down a cost model. – Databases actually do this! Clustered v. unclustered is a big deal.

38 Sorting.

39 Why Sort? Data requested in sorted order – e.g., find students in increasing GPA order Sorting is first step in bulk loading B+ tree index. A classic problem in computer science!

40 More reasons to sort… Sorting useful for eliminating duplicate copies in a collection of records (Why?) Sort-merge join algorithm involves sorting. Problem: sort 1Tb of data with 1Gb of RAM. – why not use virtual memory?

41 Do people care? Sort benchmark bares his name http://sortbenchmark.org

42 Simplified External Sorts.

43 Two Ideas behind external sort I/O optimized: long sequential disk access Observation: Merging sorted files is easy Sort small sets in memory, then merge.

44 3 Buffer Pages Sort 18,8,5,30 Main Memory 44,10,33,55

45 Phase I: Buffer with 3 Pages Sort Main Memory 44,10,33,55 Sort it! (Quicksort) 10,33,44,55 Phase 1, Per Page 2 IOs (1 Read,1 Write) 18,8,5,30 5,8,18,30 End: All pages sorted.

46 Phase II: Merge Main Memory 10,33,44,55 5,8,18,30 10,33,44,55 5,8,18,30 1. Read Pages 5,8,10,18 5 5,8 2. Merge

47 Phase II: Merge Main Memory 10,33,44,55 5,8,18,30 10,33,44,55 5,8,18,30 1. Read Pages 5,8,10,18 5 5,8 2. Merge

48 Phase II: Merge Main Memory 10,33,44,55 5,8,18,30 10,33,44,55 5,8,18,30 1. Read Pages 5,8,10,18 5,8 5,8,10 2. Merge

49 Phase II: Merge Main Memory 10,33,44,55 5,8,18,30 10,33,44,55 5,8,18,30 1. Read Pages 5,8,10,18 5,8,10 5,8,10,18 2. Merge 3 rd Page is filled

50 Phase II: Merge Main Memory 10,33,44,55 5,8,18,30 10,33,44,55 5,8,18,30 1. Read Pages 5,8,10,18 2. Merge 3. Write Back Keep on Merging!

51 3 Buffer Pages Sort 30,33,44,55 Main Memory 5,8,10,18 Now, runs of length 2. If our file has 16 pages, what is next?

52 Phase II: Merge Main Memory 10,33,44,55 5,8,18,30 10,33,44,55 5,8,18,30 1. Read Pages 5,8,10,18 2. Merge 3. Write Back

53 Two-Way External Merge Sort Each pass we read + write each page in file. N pages in the file => the number of passes So toal cost is: Idea: Divide and conquer: sort subfiles and merge Input file 1-page runs 2-page runs 4-page runs 8-page runs PASS 0 PASS 1 PASS 2 PASS 3 9 3,4 6,2 9,48,75,63,1 2 3,45,62,64,97,8 1,32 2,3 4,6 4,7 8,9 1,3 5,62 2,3 4,4 6,7 8,9 1,2 3,5 6 1,2 2,3 3,4 4,5 6,6 7,8

54 More Buffer Pages Sort 18,8,5,30 Main Memory 44,10,33,55 What if we have B+1 Buffer Pages? Sort IOs: 2N(1 + log B (N/(B+1))) 1 st Pass: Runs of Length B+1 Merge Phase: B-way Merge.

55 Number of Passes of External Sort Engineer’s rule of thumb: You sort in 3 passes

56 Other Optimizations Can get twice as long runs – Tournament sort (used in Postgres) Can Improve IO performance by using bigger buffers to “prefetch” or “double buffer” – Prefetch: Hide latency – Bigger Batch Sizes: Amortize expensive sequential reads and writes.


Download ppt "BTrees & Sorting 11/3. Announcements I hope you had a great Halloween. Regrade requests were due a few minutes ago…"

Similar presentations


Ads by Google