Download Database Indexing: Dense, Sparse, B+-tree and ISAM and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
Indexing: Part I
CPS 216
Advanced Database Systems
2
Announcements (February 3)
Homework #1 due next Tuesday (February 8)
No class next Thursday (February 10)
Homework #2 will be assigned on the following
Tuesday; meanwhile, use the time to think about
course project!
3
Basics
Given a value, locate the record(s) with this value
SELECT * FROM R WHERE A = value ;
SELECT * FROM R , S WHERE R. A = S. B ;
Other search criteria, e.g.
Range search
SELECT * FROM R WHERE A > value ;
Keyword search
database indexing Search
Dense and sparse indexes
Dense: one index entry for each search key value
Sparse: one index entry for each block
Records must be clustered according to the search key
123 Milhouse 10 3.
142 Bart 10 2.
279 Jessica 10 4
345 Martin 8 2.
456 Ralph 8 2.
512 Nelson 10 2.
679 Sherri 10 3.
697 Terri 10 3.
857 Lisa 8 4.
912 Windel 8 3.
123
456
875
Sparse index
on SID
Bart
Jessica
Lisa
Martin
Milhouse
Nelson
Ralph
Sherri
Terri
Windel
Dense index
on name
5
Dense versus sparse indexes
Index size
Requirement on records
Lookup
Update
6
Primary and secondary indexes
Primary index
Created for the primary key of a table
Records are usually clustered according to the primary key
Can be sparse
Secondary index
Usually dense
SQL
PRIMARY KEY declaration automatically creates a primary index,
UNIQUE key automatically creates a secondary index
Secondary index can be created on non-key attribute(s)
CREATE INDEX StudentGPAIndex ON Student(GPA);
Sample B
-tree nodes
Max fan-out: 4
to keys
k < 120
to keys
120 · k < 150
to keys
150 · k < 180
to keys
180 · k
Non-leaf
to records with these k values;
or, store records directly in leaves
Leaf to next leaf node in sequence
11
B
-tree balancing properties
All leaves at the same lowest level
All nodes at least half full (except root)
Max # Max # Min # Min
pointers keys active pointers keys
Non-leaf f f – 1 d f / 2 e d f / 2 e – 1
Root f f – 1 2 1
Leaf f f – 1 b f / 2 c b f / 2 c
12
Lookups
SELECT * FROM R WHERE k = 179;
Max fan-out: 4
Not found
SELECT * FROM R WHERE k = 32;
Range query
SELECT * FROM R WHERE k > 32 AND k < 179;
Max fan-out: 4
Look up 32 …
And follow next-leaf pointers
14
Insertion
Insert a record with search key value 32
Max fan-out: 4
Look up where the
inserted key
should go…
And insert it right there
15
Another insertion example
Insert a record with search key value 152
Max fan-out: 4
Oops, node is already full!
Stealing from a sibling
Max fan-out: 4
Remember to fix the key
in the lowest common ancestor
20
Another deletion example
Delete a record with search key value 179
Max fan-out: 4
Cannot steal from siblings
Then coalesce (merge) with a sibling!
21
Coalescing
Max fan-out: 4
Remember to delete the
appropriate key from parent
Deletion can “propagate” all the way up to the root of the tree (not
illustrated here)
When the root becomes empty, the tree “shrinks” by one level
Performance analysis
How many I/O’s are required for each operation?
h (more or less), where h is the height of the tree
Plus one or two to manipulate actual records
Plus O ( h ) for reorganization (should be very rare if f is large)
Minus one if we cache the root in memory
How big is h?
Roughly log
fan-out
N , where N is the number of records
B
-tree properties guarantee that fan-out is least f / 2 for all non-
root nodes
Fan-out is typically large (in hundreds)—many keys and pointers
can fit into one block
A 4-level B
-tree is enough for typical tables
23
B
-tree in practice
Complex reorganization for deletion often is not
implemented (e.g., Oracle, Informix)
Most commercial DBMS use B
-tree instead of
hashing-based indexes because B
-tree handles
range queries
24
The Halloween Problem
Story from the early days of System R…
UPDATE Payroll
SET salary = salary * 1.
WHERE salary >= 100000;
There is a B
-tree index on Payroll ( salary )
The update never stopped (why?)
Solutions?
B
-tree versus ISAM
ISAM is more static; B
-tree is more dynamic
ISAM is more compact (at least initially)
Fewer levels and I/O’s than B
-tree
Overtime, ISAM may not be balanced
Cannot provide guaranteed performance as B
-tree does
29
B
-tree versus B-tree
B-tree: why not store records (or record pointers) in
non-leaf nodes?
These records can be accessed with fewer I/O’s
Problems?
30
Coming up next
Other tree-based indexs: R-trees and variants, GiST
Hashing-based indexes: extensible hashing, linear
hashing, etc.
Text indexes: inverted-list index, suffix arrays