Lecture 20: Indexes Monday, February 27, 2006.

Slides:



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

1 Introduction to Database Systems CSE 444 Lectures 19: Data Storage and Indexes November 14, 2007.
1 Lecture 8: Data structures for databases II Jose M. Peña
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.
COMP 451/651 Indexes Chapter 1.
1 Lecture 18: Indexes Monday, November 10, Midterm Problem 1a: select student.sname, avg(takes.grade) from student, takes where student.sid =
1 Overview of Storage and Indexing Yanlei Diao UMass Amherst Feb 13, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Introduction to Database Systems1 Indexing Techniques Storage Technology: Topic 4.
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.
Primary Indexes Dense Indexes
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.
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
BTrees & Sorting 11/3. Announcements I hope you had a great Halloween. Regrade requests were due a few minutes ago…
Adapted from Mike Franklin
Implementation of Relational Operators/Estimated Cost 1.Select 2.Join.
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:
Index tuning-- B+tree. overview Overview of tree-structured index Indexed sequential access method (ISAM) B+tree.
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.
CS 440 Database Management Systems Lecture 6: Data storage & access methods 1.
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.
Indexing. 421: Database Systems - Index Structures 2 Cost Model for Data Access q Data should be stored such that it can be accessed fast q Evaluation.
CS411 Database Systems Kazuhiro Minami 10: Indexing-1.
1 Tree-Structured Indexes Chapter Introduction  As for any index, 3 alternatives for data entries k* :  Data record with key value k   Choice.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
Tree-Structured Indexes. Introduction As for any index, 3 alternatives for data entries k*: – Data record with key value k –  Choice is orthogonal to.
CPS216: Data-intensive Computing Systems
CS522 Advanced database Systems
Record Storage, File Organization, and Indexes
CS 540 Database Management Systems
Indexing Goals: Store large files Support multiple search keys
Indexing and hashing.
CS 728 Advanced Database Systems Chapter 18
Tree-Structured Indexes
File Organizations and Indexes
Indexing ? Why ? Need to locate the actual records on disk without having to read the entire table into memory.
Lecture 20: Indexing Structures
Database Management Systems (CS 564)
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.
B+-Trees and Static Hashing
Tree-Structured Indexes
Lecture 21: Indexes Monday, November 13, 2000.
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.
Adapted from Mike Franklin
Lecture 28: Index 3 B+ Trees
Database Design and Programming
CSE 544: Lecture 11 Storing Data, Indexes
Indexing 1.
CS222/CS122C: Principles of Data Management Notes #6 Index Overview and ISAM Tree Index Instructor: Chen Li.
Storage and Indexing.
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
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.
Presentation transcript:

Lecture 20: Indexes Monday, February 27, 2006

Outline Data storage Index structures (13.1, 13.2) B-trees (13.3)

Data Storage For persistence, data is stored on disk Basic abstraction Collection of records or file Typically, 1 relation = 1 file A file consists of one or more pages One page corresponds to one block Page is unit of info read/written to/from disk How to organize records in file?

Heap File File is not sorted on any attribute Student(sid: int, age: int, …) 30 18 … 70 21 1 record 20 40 19 1 page 80 19 60 18 10 21 50 22

Heap File Example 10,000 students 10 student records per page Total number of pages: 1,000 pages Find student whose sid is 80 Must read on average 500 pages Find all students older than 20 Must read all 1,000 pages Can we do better?

Sequential File File sorted on an attribute, usually on primary key Student(sid: int, age: int, …) 10 21 … 20 30 18 40 19 50 22 60 18 70 21 80 19

Sequential File Example Total number of pages: 1,000 pages Find student whose sid is 80 Could do binary search, read log2(1,000) ≈ 10 pages Find all students older than 20 Must still read all 1,000 pages Can we do even better?

Indexes An index on a file speeds up selections on the search key fields for the index. Any subset of the fields of a relation can be the search key for an index on the relation. Search key is not the same as key (minimal set of fields that uniquely identify a record in a relation). An index contains a collection of data entries, and supports efficient retrieval of all data entries with a given key value k. 7

Primary Index File is sorted on the index attribute Dense index: sequence of (key,pointer) pairs Index File Data File 1 data entry 10 20 10 20 30 40 30 40 50 60 70 80 1 page 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 Example Let’s assume all pages of index fit in memory Find student whose sid is 80 Index (dense or sparse) points directly to the page Only need to read 1 page from disk. Find all students older than 20 Must still read all 1,000 pages. How can we make both queries fast?

Secondary Indexes To index other attributes than primary key Always dense (why ?) 10 21 20 18 19 30 18 40 19 20 21 22 50 22 60 18 70 21 80 19

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

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

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

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

Large Indexes What if index does not fit in memory? Would like to index the index itself How many index levels do we need? Can we create them automatically? Yes! Can do something even more powerful!

B+ Trees Search trees Idea in B Trees Idea in B+ Trees Make 1 node = 1 page (= 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 Data records 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: Find lowest bound as above Then sequential traversal Select name From Student Where age = 25 Select name From Student 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 bytes 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, also keep K3 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

Summary on B+ Trees Default index structure on most DBMS Very effective at answering ‘point’ queries: productName = ‘gizmo’ Effective for range queries: 50 < price AND price < 100 Less effective for multirange: 50 < price < 100 AND 2 < quant < 20