CSE 544: Lecture 11 Storing Data, Indexes

Slides:



Advertisements
Similar presentations
B+-Trees and Hashing Techniques for Storage and Index Structures
Advertisements

Introduction to Database Systems1 Records and Files Storage Technology: Topic 3.
1 Introduction to Database Systems CSE 444 Lectures 19: Data Storage and Indexes November 14, 2007.
Indexes. Primary Indexes Dense Indexes Pointer to every record of a sequential file, (ordered by search key). Can make sense because records may be much.
Indexes. Primary Indexes Dense Indexes Pointer to every record of a sequential file, (ordered by search key). Can make sense because records may be much.
1 Lecture 18: Indexes Monday, November 10, Midterm Problem 1a: select student.sname, avg(takes.grade) from student, takes where student.sid =
1 Lecture 20: Indexes Friday, February 25, Outline Representing data elements (12) Index structures (13.1, 13.2) B-trees (13.3)
1 Lecture 19: B-trees and Hash Tables Wednesday, November 12, 2003.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
DBMS Internals: Storage February 27th, Representing Data Elements Relational database elements: A tuple is represented as a record CREATE TABLE.
Tree-Structured Indexes. Range Searches ``Find all students with gpa > 3.0’’ –If data is in sorted file, do binary search to find first such student,
Storage and Indexing February 26 th, 2003 Lecture 19.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 8.
Indexing - revisited CS 186, Fall 2012 R & G Chapter 8.
BTrees & Sorting 11/3. Announcements I hope you had a great Halloween. Regrade requests were due a few minutes ago…
1 Indexing. 2 Motivation Sells(bar,beer,price )Bars(bar,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 Query:
File Organizations and Indexing
Storage and Indexing. How do we store efficiently large amounts of data? The appropriate storage depends on what kind of accesses we expect to have to.
CS411 Database Systems Kazuhiro Minami 10: Indexing-1.
CS4432: Database Systems II
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 8 Jianping Fan Dept of Computer Science UNC-Charlotte.
Tree-Structured Indexes. Introduction As for any index, 3 alternatives for data entries k*: – Data record with key value k –  Choice is orthogonal to.
Storage and File Organization
CS522 Advanced database Systems
CS 540 Database Management Systems
CS522 Advanced database Systems
Tree-Structured Indexes
Storage and Indexes Chapter 8 & 9
COP Introduction to Database Structures
Indexing ? Why ? Need to locate the actual records on disk without having to read the entire table into memory.
Database Applications (15-415) DBMS Internals- Part III Lecture 15, March 11, 2018 Mohammad Hammoud.
CS222P: Principles of Data Management Notes #6 Index Overview and ISAM Tree Index Instructor: Chen Li.
Lecture 12 Lecture 12: Indexing.
Introduction to Database Systems File Organization and Indexing
B+-Trees and Static Hashing
File Organizations and Indexing
File Organizations and Indexing
CS222/CS122C: Principles of Data Management Notes #07 B+ Trees
Tree-Structured Indexes
Introduction to Database Systems
Lecture 21: Indexes Monday, November 13, 2000.
Lecture 15: Midterm Review Data Storage
Lecture 19: Data Storage and Indexes
B+Trees The slides for this text are organized into chapters. This lecture covers Chapter 9. Chapter 1: Introduction to Database Systems Chapter 2: The.
CSE 544: Lectures 13 and 14 Storing Data, Indexes
Tree-Structured Indexes
Lecture 21: B-Trees Monday, Nov. 19, 2001.
Lecture 6: Data Storage and Indexes
Storage and Indexing May 17th, 2002.
DATABASE IMPLEMENTATION ISSUES
Indexing 1.
CS222/CS122C: Principles of Data Management Notes #6 Index Overview and ISAM Tree Index Instructor: Chen Li.
Storage and Indexing.
ICOM 5016 – Introduction to Database Systems
General External Merge Sort
Introduction to Database Systems CSE 444 Lectures 19: Data Storage and Indexes May 16, 2008.
Files and access methods
Indexing February 28th, 2003 Lecture 20.
Lecture 11: B+ Trees and Query Execution
Database Implementation Issues
Lecture 20: Indexes Monday, February 27, 2006.
CS4433 Database Systems Indexing.
Tree-Structured Indexes
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #05 Index Overview and ISAM Tree Index Instructor: Chen Li.
Lecture 15: Data Storage Tuesday, February 20, 2001.
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #06 B+ trees Instructor: Chen Li.
Lecture 20: Representing Data Elements
CS222P: Principles of Data Management UCI, Fall Notes #06 B+ trees
Index Structures Chapter 13 of GUW September 16, 2019
Presentation transcript:

CSE 544: Lecture 11 Storing Data, Indexes Monday, 5/1/2006

Outline Storing data: disks and files - 9.5-9.7 Types of Indexes - Chapter 8.3 B-trees - Chapter 10

Managing Free Blocks By the OS By the RDBMS (typical: why ?) Linked list of free blocks Bit map

Files of Records Types of files: Heap file - unordered Sorted file Clustered file - sorted, plus a B-tree Will discuss heap files only; the others are similar, only sorted by the key

Pages with some free space Heap Files Full pages Linked list of pages: Data page Data page Data page Data page Header page Pages with some free space Data page Data page Data page Data page

Heap Files Better: directory of pages Data page Header Data page

Page Formats Issues to consider 1 page = fixed size (e.g. 8KB) Records: Fixed length Variable length Record id = RID Typically RID = (PageID, SlotNumber) Log entries refer to RIDs. Plus, foreign keys may be optimized to RID’s, but I’m not sure if this is implemented in any system Why do we need RID’s in a relational DBMS ?

Page Formats Slot 1 Slot 2 Slot N Free space N Fixed-length records: packed representation Slot 1 Slot 2 Slot N Free space N Need to move records around when one is deleted Problems ?

Page Formats Free space Slot directory Variable-length records

Record Formats Field 1 Field 2 . . . Field K Fixed-length records --> all fields have fixed length Field 1 Field 2 . . . Field K

Record Formats Field 1 Field 2 . . . Field K Variable length records Record header Remark: NULLS require no space at all (why ?)

Spanning Records Across Blocks When records are very large Or even medium size: saves space in blocks Commercial RDBMS avoid this block header block header R1 R2 R3 R2

LOB Large objects Supported by modern database systems Binary large object: BLOB Character large object: CLOB Supported by modern database systems E.g. images, sounds, texts, etc. Storage: attempt to cluster blocks together

Modifications: Insertion File is unsorted (= heap file) add it to the end (easy ) File is sorted: Is there space in the right block ? Yes: we are lucky, store it there Is there space in a neighboring block ? Look 1-2 blocks to the left/right, shift records If anything else fails, create overflow block

Overflow Blocks Blockn-1 Blockn Blockn+1 Overflow After a while the file starts being dominated by overflow blocks: time to reorganize

Modifications: Deletions Free space in block, shift records Maybe be able to eliminate an overflow block Can never really eliminate the record, because others may point to it Place a tombstone instead (a NULL record)

Modifications: Updates If new record is shorter than previous, easy  If it is longer, need to shift records, create overflow blocks

Record Formats: Fixed Length Base address (B) Address = B+L1+L2 Information about field types same for all records in a file; stored in system catalogs. Finding i’th field requires scan of record. Note the importance of schema information! 9

Indexes Search key = can be any set of fields not the same as the primary key, nor a key Index = collection of data entries Data entry for key k can be: The actual record with key k (k, RID) (k, list-of-RIDs) 7

Index Classification Primary/secondary Clustered/unclustered Primary = may reorder data according to index Secondary = cannot reorder data Clustered/unclustered Clustered = records close in the index are close in the data Unclustered = records close in the index may be far in the data Dense/sparse Dense = every key in the data appears in the index Sparse = the index contains only some keys B+ tree / Hash table / …

Primary Index File is sorted on the index attribute Dense index: sequence of (key,pointer) pairs 10 20 10 20 30 40 30 40 50 60 70 80 50 60 70 80

Primary Index Sparse index 10 20 30 40 50 60 70 80 10 30 50 90 70 110 130 150 50 60 70 80

Primary Index with Duplicate Keys Dense index: 10 10 20 30 40 10 20 50 60 70 80 20 30 40

Primary Index with Duplicate Keys Sparse index: pointer to lowest search key in each block: Search for 20 ...but need to search here too 10 10 20 30 20 is here... 10 20 20 30 40

Primary Index with Duplicate Keys Better: pointer to lowest new search key in each block: Search for 20 Search for 15 ? 35 ? 10 10 20 10 20 30 40 20 is here... ...ok to search from here 30 50 60 70 80 30 40 50

Secondary Indexes To index other attributes than primary key Always dense (why ?) 20 30 10 20 30 20 20 30 10 20 10 30

Clustered/Unclustered Primary indexes = usually clustered Secondary indexes = usually unclustered

Clustered vs. Unclustered Index Data entries Data entries (Index File) (Data file) Data Records Data Records CLUSTERED UNCLUSTERED 12

Secondary Indexes index other attributes than primary key index unsorted files (heap files) index clustered data

Applications of Secondary Indexes Secondary indexes needed for heap files Also for Clustered data: Company(name, city), Product(pid, maker) Select city From Company, Product Where name=maker and pid=“p045” Select pid From Company, Product Where name=maker and city=“Seattle” Products of company 2 Products of company 3 Products of company 1 Company 1 Company 2 Company 3

Composite Search Keys Composite Search Keys: Search on a combination of fields. Equality query: Every field value is equal to a constant value. E.g. wrt <sal,age> index: age=20 and sal =75 Range query: Some field value is not a constant. E.g.: age =20; or age=20 and sal > 10 Examples of composite key indexes using lexicographic order. 11,80 11 12,10 12 name age sal 12,20 12 13,75 bob 12 10 13 <age, sal> cal 11 80 <age> joe 12 20 10,12 sue 13 75 10 20,12 Data records sorted by name 20 75,13 75 80,11 80 <sal, age> <sal> Data entries in index sorted by <sal,age> Data entries sorted by <sal> 13

B+ Trees Search trees Idea in B Trees: Idea in B+ Trees: make 1 node = 1 block Idea in B+ Trees: Make leaves into a linked list (range queries are easier)

B+ Trees Basics Parameter d = the degree Each node has >= d and <= 2d keys (except root) Each leaf has >=d and <= 2d keys: 30 120 240 Keys k < 30 Keys 30<=k<120 Keys 120<=k<240 Keys 240<=k 40 50 60 Next leaf 40 50 60

B+ Tree Example d = 2 Find the key 40 80 40  80 20 60 100 120 140 20 < 40  60 10 15 18 20 30 40 50 60 65 80 85 90 30 < 40  40 10 15 18 20 30 40 50 60 65 80 85 90

Searching a B+ Tree Exact key values: Range queries: 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

B+ Tree Design How large d ? Example: 2d x 4 + (2d+1) x 8 <= 4096 Key size = 4 bytes Pointer size = 8 bytes Block size = 4096 byes 2d x 4 + (2d+1) x 8 <= 4096 d = 170

B+ Trees in Practice Typical order: 100. Typical fill-factor: 67%. average fanout = 133 Typical capacities: Height 4: 1334 = 312,900,700 records Height 3: 1333 = 2,352,637 records Can often hold top levels in buffer pool: Level 1 = 1 page = 8 Kbytes Level 2 = 133 pages = 1 Mbyte Level 3 = 17,689 pages = 133 MBytes

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 parent parent K3 K1 K2 K3 K4 K5 P0 P1 P2 P3 P4 p5 K1 K2 P0 P1 P2 K4 K5 P3 P4 p5

Insertion in a B+ Tree Insert K=19 80 20 60 100 120 140 10 15 18 20 30 50 60 65 80 85 90 10 15 18 20 30 40 50 60 65 80 85 90

Insertion in a B+ Tree After insertion 80 20 60 100 120 140 10 15 18 19 20 30 40 50 60 65 80 85 90 10 15 18 19 20 30 40 50 60 65 80 85 90

Insertion in a B+ Tree Now insert 25 80 20 60 100 120 140 10 15 18 19 30 40 50 60 65 80 85 90 10 15 18 19 20 30 40 50 60 65 80 85 90

Insertion in a B+ Tree After insertion 80 20 60 100 120 140 10 15 18 19 20 25 30 40 50 60 65 80 85 90 10 15 18 19 20 25 30 40 50 60 65 80 85 90

Insertion in a B+ Tree But now have to split ! 80 20 60 100 120 140 10 15 18 19 20 25 30 40 50 60 65 80 85 90 10 15 18 19 20 25 30 40 50 60 65 80 85 90

Insertion in a B+ Tree After the split 80 20 30 60 100 120 140 10 15 18 19 20 25 30 40 50 60 65 80 85 90 10 15 18 19 20 25 30 40 50 60 65 80 85 90

Deletion from a B+ Tree Delete 30 80 20 30 60 100 120 140 10 15 18 19 25 30 40 50 60 65 80 85 90 10 15 18 19 20 25 30 40 50 60 65 80 85 90

Deletion from a B+ Tree After deleting 30 May change to 40, or not 80 20 30 60 100 120 140 10 15 18 19 20 25 40 50 60 65 80 85 90 10 15 18 19 20 25 40 50 60 65 80 85 90

Deletion from a B+ Tree Now delete 25 80 20 30 60 100 120 140 10 15 18 19 20 25 40 50 60 65 80 85 90 10 15 18 19 20 25 40 50 60 65 80 85 90

Deletion from a B+ Tree After deleting 25 Need to rebalance Rotate 80 20 30 60 100 120 140 10 15 18 19 20 40 50 60 65 80 85 90 10 15 18 19 20 40 50 60 65 80 85 90

Deletion from a B+ Tree Now delete 40 80 19 30 60 100 120 140 10 15 18 50 60 65 80 85 90 10 15 18 19 20 40 50 60 65 80 85 90

Deletion from a B+ Tree After deleting 40 Rotation not possible Need to merge nodes 80 19 30 60 100 120 140 10 15 18 19 20 50 60 65 80 85 90 10 15 18 19 20 50 60 65 80 85 90

Deletion from a B+ Tree Final tree 80 19 60 100 120 140 10 15 18 19 20 50 60 65 80 85 90 10 15 18 19 20 50 60 65 80 85 90

In Class Suppose the B+ tree has depth 4 and degree d=200 How many records does the relation have (maximum) ? How many index blocks do we need to read and/or write during: A key lookup An insertion A deletion