Physical Level of Databases: B+-Trees

Similar documents
Chapter 11: Indexing and Hashing

Intro to DB CHAPTER 12 INDEXING & HASHING

Chapter 12: Indexing and Hashing. Basic Concepts

Chapter 12: Indexing and Hashing

Chapter 12: Indexing and Hashing

CSIT5300: Advanced Database Systems

Chapter 11: Indexing and Hashing

Chapter 11: Indexing and Hashing

Chapter 12: Indexing and Hashing (Cnt(

Database System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Introduction to Indexing 2. Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana

Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

Chapter 11: Indexing and Hashing" Chapter 11: Indexing and Hashing"

Chapter 11: Indexing and Hashing

Material You Need to Know

CSE 530A. B+ Trees. Washington University Fall 2013

Background: disk access vs. main memory access (1/2)

THE B+ TREE INDEX. CS 564- Spring ACKs: Jignesh Patel, AnHai Doan

Tree-Structured Indexes

CS350: Data Structures B-Trees

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is?

Remember. 376a. Database Design. Also. B + tree reminders. Algorithms for B + trees. Remember

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

Database index structures

Balanced Search Trees

Tree-Structured Indexes

Tree-Structured Indexes

CS 350 : Data Structures B-Trees

DATABASE PERFORMANCE AND INDEXES. CS121: Relational Databases Fall 2017 Lecture 11

Data Structures and Algorithms

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1

CS 525: Advanced Database Organization 04: Indexing

Tree-Structured Indexes. Chapter 10

amiri advanced databases '05

Principles of Data Management. Lecture #5 (Tree-Based Index Structures)

Tree-Structured Indexes ISAM. Range Searches. Comments on ISAM. Example ISAM Tree. Introduction. As for any index, 3 alternatives for data entries k*:

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

The B-Tree. Yufei Tao. ITEE University of Queensland. INFS4205/7205, Uni of Queensland

Tree-Structured Indexes

Chapter 17 Indexing Structures for Files and Physical Database Design

Kathleen Durant PhD Northeastern University CS Indexes

Tree-Structured Indexes

(i) It is efficient technique for small and medium sized data file. (ii) Searching is comparatively fast and efficient.

CS143: Index. Book Chapters: (4 th ) , (5 th ) , , 12.10

Topics to Learn. Important concepts. Tree-based index. Hash-based index

B-Trees & its Variants

Announcements. Reading Material. Recap. Today 9/17/17. Storage (contd. from Lecture 6)

Introduction to Indexing R-trees. Hong Kong University of Science and Technology

Tree-Structured Indexes (Brass Tacks)

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

Introduction. Choice orthogonal to indexing technique used to locate entries K.

CS F-11 B-Trees 1

Multiway Search Trees. Multiway-Search Trees (cont d)

Spring 2017 B-TREES (LOOSELY BASED ON THE COW BOOK: CH. 10) 1/29/17 CS 564: Database Management Systems, Jignesh M. Patel 1

CPS352 Lecture - Indexing

Design and Analysis of Algorithms Lecture- 9: B- Trees

Indexing. Chapter 8, 10, 11. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Tree-Structured Indexes. A Note of Caution. Range Searches ISAM. Example ISAM Tree. Introduction

Overview of Storage and Indexing

CSE 562 Database Systems

Find the block in which the tuple should be! If there is free space, insert it! Otherwise, must create overflow pages!

Indexing: B + -Tree. CS 377: Database Systems

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

B-Trees. Introduction. Definitions

Tree-Structured Indexes

Lecture 13. Lecture 13: B+ Tree

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Database Systems. File Organization-2. A.R. Hurson 323 CS Building

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Data Organization B trees

CS127: B-Trees. B-Trees

Introduction to Data Management. Lecture 15 (More About Indexing)

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree

CS Fall 2010 B-trees Carola Wenk

Indexing and Hashing

Problem. Indexing with B-trees. Indexing. Primary Key Indexing. B-trees: Example. B-trees. primary key indexing

Comp 335 File Structures. B - Trees

Indexing. Week 14, Spring Edited by M. Naci Akkøk, , Contains slides from 8-9. April 2002 by Hector Garcia-Molina, Vera Goebel

Chapter 13: Indexing. Chapter 13. ? value. Topics. Indexing & Hashing. value. Conventional indexes B-trees Hashing schemes (self-study) record

CMPS 2200 Fall 2017 B-trees Carola Wenk

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

C SCI 335 Software Analysis & Design III Lecture Notes Prof. Stewart Weiss Chapter 4: B Trees

CS 245: Database System Principles

Administrivia. Tree-Structured Indexes. Review. Today: B-Tree Indexes. A Note of Caution. Introduction

Chapter 17. Disk Storage, Basic File Structures, and Hashing. Records. Blocking

Trees. (Trees) Data Structures and Programming Spring / 28

Main Memory and the CPU Cache

Lecture 8 Index (B+-Tree and Hash)

Laboratory Module X B TREES

Module 4: Tree-Structured Indexing

Chapter 18 Indexing Structures for Files. Indexes as Access Paths

Database Applications (15-415)

I think that I shall never see A billboard lovely as a tree. Perhaps unless the billboards fall I ll never see a tree at all.

Tree-Structured Indexes

CARNEGIE MELLON UNIVERSITY DEPT. OF COMPUTER SCIENCE DATABASE APPLICATIONS

ΗΥ360 Αρχεία και Βάσεις εδοµένων

Binary Trees

Transcription:

Physical Level of Databases: B+-Trees Adnan YAZICI Computer Engineering Department METU (Fall 2005) 1

B + -Tree Index Files l Disadvantage of indexed-sequential files: performance degrades as file grows, since many overflow blocks get created. Periodic reorganization of entire file is required. l Advantage of B + -tree index files: automatically reorganizes itself with small and local changes, in the face of insertions and deletions. Reorganization of entire file is not required to maintain performance. l Disadvantage of B + -trees: extra insertion and deletion overhead, space overhead. l Advantages of B + -trees outweigh disadvantages, and they are used extensively. 2

B + -Tree Index Files (Cont.) l All paths from root to leaf are of the same length l Each node that is not a root or a leaf has between [n/2] and n children. l Special cases: If the root is not a leaf, it has at least 2 children. If the root is a leaf (that is, there are no other nodes in the tree), it can have between 0 and (n 1) values. 3

B+-Trees as Dynamic Multi-level Indexes B+-Tree is a variation of search trees that allow efficient insertion and deletion of new search values. In B+-Tree, each node corresponds to a disk block. Each node is kept between half-full and completely-full. An insertion into a node that is not full is quite efficient; if a node is full the insertion causes a split into two nodes. Splitting may propagate to other tree levels. A deletion is efficient if a node does not become less than half full. If a deletion causes a node to become less than half full, it must be merged with neighboring nodes. 4

l Typical node B + -Tree Node Structure K i are the search-key values P i are pointers to children (for non-leaf nodes) or pointers to records or buckets of records (for leaf nodes). l The search-keys in a node are ordered K 1 < K 2 < K 3 <... < K n 1 5

l l l Leaf Nodes in B + -Trees For i = 1, 2,..., n, pointer P i either points to a file record with search-key value K i, or to a bucket of pointers to file records, each record having search-key value K i. Only need bucket structure if search-key does not form a primary key. If L i, L j are leaf nodes and i < j, L i s search-key values are less than L j s search-key values P n points to next leaf node in search-key order 6

Non-Leaf Nodes in B + -Trees l Non leaf nodes form a multi-level sparse index on the leaf nodes. For a non-leaf node with m pointers: All the search-keys in the subtree to which P 1 points are less than K 1 For 2 i n 1, all the search-keys in the subtree to which P i points have values greater than or equal to K i 1 and less than K m 1 7

Example of a B + -tree 8

Example of B + -tree! " l All nodes must have between 2 and 4 key values ( (n)/2 and n, with n = 5). l Root must have at least 2 children. 9

Observations about B + -trees l Since the inter-node connections are done by pointers, logically close blocks need not be physically close. l The non-leaf levels of the B + -tree form a hierarchy of sparse indices. l The B + -tree contains a relatively small number of levels (logarithmic in the size of the main file), thus searches can be conducted efficiently. l Insertions and deletions to the main file can be handled efficiently, as the index can be restructured in logarithmic time (as we shall see). 10

Queries on B + -Trees l Find all records with a search-key value of k. 1. Start with the root node 1. Examine the node for the smallest search-key value > k. 2. If such a value exists, assume it is K j. Then follow P i to the child node 3. Otherwise k K m 1, where there are m pointers in the node. Then follow P m to the child node. 2. If the node reached by following the pointer above is not a leaf node, repeat step 1 on the node 3. Else we have reached a leaf node. 1. If for some i, key K i = k follow pointer P i to the desired record or bucket. 2. Else no record with search-key value k exists. 11

Queries on B +- Trees (Cont.) l In processing a query, a path is traversed in the tree from the root to some leaf node. l If there are K search-key values in the file, the path is no longer than log n/2 (K). l A node is generally the same size as a disk block, typically 4 kilobytes, and n is typically around 100 (40 bytes per index entry). l With 1 million search key values and n = 100, at most log 50 (1,000,000) = 4 nodes are accessed in a lookup. l Contrast this with a balanced binary free with 1 million search key values around 20 nodes are accessed in a lookup above difference is significant since every node access may need a disk I/O, costing around 20 milliseconds! 12

B+ Tree 13

B+ Tree l B+ Tree Properties l B+ Tree Searching l B+ Tree Insertion l B+ Tree Deletion 14

Balanced Tree B+ Tree l Same height for paths from root to leaf l Given a search-key K, nearly same access time for different K values B+ Tree is constructed by parameter n l Each Node (except root) has n/2 to n pointers l Each Node (except root) has n/2-1 to n-1 searchkey values Case for n=3 K 1 K 2 P 1 P 2 P 3 General case for n P 1 K 1 K 2 K n-1 P 2 P n-1 P n 15

B+ Tree l Search keys are sorted in order K 1 < K 2 < <K n-1 Non-leaf Node Each key-search values in subtree S i pointed by P i < K i, >=K i-1 K 1 K 2 P 1 P 2 P 3 Key values in S 1 < K 1 K 1 <= Key values in S 2 < K 2 K 1 K 2 Leaf Node P i points record or bucket with search key value K i P n points to the neighbor leaf node P 1 S 1 S 2 S 3 P2 Record of K 1 Record of K 2 Record of K 2 P 3 16

B+ Tree: Search l Given a search-value k Start from the root, look for the largest search-key value (K l ) in the node <= k Follow pointer P l+1 to next level, until reach a leaf node K l <=k<k l+1 K 1 K 2 K l K l+1 K n-1 P 1 P 2 P 3 P n-1 P n P l+1 If k is found to be equal to K l in the leaf, follow P l to search the record or bucket K l K l+1 Record of K l P l k = K l Record of K l 17

l Overflow B+ Tree:Insertion When the number of search-key values exceed n-1 Leaf Node Split into two nodes: 7 9 13 15 Insert 8 1 st node contains (n-1)/2 values 2 nd node contains remaining values Copy the smallest search-key value of the 2 nd node to parent node 9 7 8 9 13 15 18

l Overflow B+ Tree: Insertion When number of search-key values exceed n-1 Non-Leaf Node Split into two nodes: 7 9 13 15 Insert 8 1 st node contains n/2-1 values Move the smallest of the remaining values, together with pointer, to the parent 2 nd node contains the remaining values 9 7 8 13 15 19

B+ Tree: Insertion l Example 1: Construct a B + tree for (1, 4, 7, 10, 17, 21, 31, 25, 19, 20, 28, 42) with n=4. 7 1 4 7 1 4 7 10 7 17 1 4 7 10 17 21 20

B+ Tree: Insertion B+ Tree Insertion l 1, 4, 7, 10, 17, 21, 31, 25, 19, 20, 28, 42 7 17 25 1 4 7 10 17 21 25 31 17 7 20 25 1 4 7 10 17 19 20 21 25 31 21

B+ Tree: Insertion l 1, 4, 7, 10, 17, 21, 31, 25, 19, 20, 28, 42 17 7 20 25 31 1 4 17 19 31 42 7 10 20 21 25 28 22

B+ Tree: Insertion l Example 2: n=3, insert 4 into the following B+Tree 9 10 7 8 Subtree C Subtree D 2 5 Leaf A Leaf B 9 7 10 4 8 C D 2 4 5 A B 23

B+ Tree: Deletion l Underflow When number of search-key values < n/2-1 9 10 Delete 10 Leaf Node Redistribute to sibling Right node not less than left node Replace the between-value in parent by their smallest value of the right node Merge (contain too few entries) Move all values, pointers to left node Remove the between-value in parent 13 18 9 10 13 14 14 18 9 13 14 16 16 13 18 22 18 22 9 10 13 14 9 13 14 24

B+ Tree: Deletion Non-Leaf Node Redistribute to sibling Through parent Right node not less than left node Merge (contain too few entries) Bring down parent Move all values, pointers to left node Delete the right node, and pointers in parent 9 10 Delete 10 13 18 9 10 14 15 16 14 18 9 13 15 16 13 18 22 18 22 9 10 14 16 9 13 14 16 25

B+ Tree: Deletion l Example 3: n=3, delete 3 5 20 3 8 Subtree A 1 3 20 5 8 Subtree A 1 26

B+ Tree: Deletion l Example 4: Delete 28, 31, 21, 25, 19 20 7 17 25 31 50 1 4 7 10 17 19 20 21 25 28 31 42 20 7 17 25 50 1 4 7 10 17 19 20 21 25 31 42 27

B+ Tree: Deletion l Example 4: Delete 28, 31, 21, 25, 19 7 17 20 50 1 4 7 10 17 19 20 25 42 7 17 50 1 4 7 10 17 20 42 28

B + -Tree File Organization l Index file degradation problem is solved by using B + - Tree indices. Data file degradation problem is solved by using B + -Tree File Organization. l The leaf nodes in a B + -tree file organization store records, instead of pointers. l Since records are larger than pointers, the maximum number of records that can be stored in a leaf node is less than the number of pointers in a nonleaf node. l Leaf nodes are still required to be half full. l Insertion and deletion are handled in the same way as insertion and deletion of entries in a B + -tree index. 29

B + -Tree File Organization (Cont.) l Good space utilization important since records use more space than pointers. l To improve space utilization, involve more sibling nodes in redistribution during splits and merges Involving 2 siblings in redistribution (to avoid split / merge where possible) results in each node having at least entries #$ % & ' 30

Indexing Strings l Variable length strings as keys Variable fanout Use space utilization as criterion for splitting, not number of pointers l Prefix compression Key values at internal nodes can be prefixes of full key l Keep enough characters to distinguish entries in the subtrees separated by the key value E.g. Silas and Silberschatz can be separated by Silb 31

B-Tree Index Files n ($ ) * +, $ + n ( +, + $ * n - ' l Nonleaf node pointers B i are the bucket or file record pointers. 32

B-Tree Index File Example B-tree (above) and B+-tree (below) on same data 33

B-Tree Index Files (Cont.) l Advantages of B-Tree indices: May use less tree nodes than a corresponding B + -Tree. Sometimes possible to find search-key value before reaching leaf node. l Disadvantages of B-Tree indices: Only small fraction of all search-key values are found early Non-leaf nodes are larger, so fan-out is reduced. Thus, B-Trees typically have greater depth than corresponding B + -Tree Insertion and deletion more complicated than in B + -Trees Implementation is harder than B + -Trees. l Typically, advantages of B-Trees do not outweigh disadvantages. 34

Index Definition in SQL l Create an index create index <index-name> on <relation-name> (<attribute-list>) E.g.: create index b-index on branch(branch_name) l Use create unique index to indirectly specify and enforce the condition that the search key is a candidate key. Not really required if SQL unique integrity constraint is supported l To drop an index drop index <index-name> 35