Storage and Index: Chapter 8, 9
Storage and Index: Chapter 8, 9
Chapter 8, 9
READ: transfer data from disk to main memory (RAM). WRITE: transfer data from RAM to disk. Both are high-cost operations, relative to in-memory operations, so must be planned carefully!
Main memory (RAM) for currently used data. Disk for the main database (secondary storage). Tapes for archiving older versions of the data (tertiary storage).
3
Disks
Secondary storage device of choice. Main advantage over tapes: random access vs. sequential. Data is stored and retrieved in units called disk blocks or pages. Unlike RAM, time to retrieve a disk page varies depending upon location on disk.
Therefore, relative placement of pages on disk has major impact on DBMS performance!
4
Components of a Disk
Disk head
Spindle Tracks
The arm assembly is moved in or out to position a head on a desired track. Tracks under heads make a cylinder (imaginary!). Only one head reads/writes at any one time.
Arm movement
Platters
Arm assembly
Top view
Top view
Track one
Top view
2 Sector one 3 4
1
8 7 6
seek time (moving arms to position disk head on track) rotational delay (waiting for block to rotate under head) transfer time (actually moving data to/from disk surface)
Seek time varies from about 1 to 20msec Rotational delay varies from 0 to 10msec Transfer rate is about 1msec per 4KB page
Key to lower I/O cost: reduce seek/rotation delays! Hardware vs. software solutions?
9
blocks on same track, followed by blocks on same cylinder, followed by blocks on adjacent cylinder
Blocks in a file should be arranged sequentially on disk (by `next), to minimize seek and rotational delay. For a sequential scan, pre-fetching several pages at a time is a big win!
Index Design
Basic requirement:
Keep all data! Store in a paginated file. Allow browsing of data.
Efficiency requirement:
Efficient search (on SSN, age, etc.). Efficient insertion/deletion/update. Efficient index size.
12
Index Types
A primary index is an index which controls the actual storage of a table. Typically this index is built using the primary key of the table.
A data entry is one record of the table.
13
An Example
10
0123 Donghui 33 Zhang 0222 James Bond 2418 Betty Wu 55 21 617-3732177 617-3246685 951-6668888
primary index
3325 6632
30
6632 Tim Varner 9688 Stephanie Chen 55 33 617-3336782 951-2430104
20
417-3326659 433-2221956 33
secondary index
21: {10, 20} 33: {10, 20, 30} 55: {10, 30}
14
15
Request for a sequence of pages must be satisfied by allocating the pages sequentially on disk! Higher levels dont need to know how this is done, or how free space is managed.
16
disk page
free frame
MAIN MEMORY
DISK
DB
Data must be in RAM for DBMS to operate on it! Table of <frame#, pageid> pairs is maintained.
Choose a frame for replacement If frame is dirty, write it to disk Read requested page into chosen frame
Requestor of page must unpin it, and indicate whether page has been modified:
a pin count is used. A page is a candidate for replacement iff pin count = 0.
CC & recovery may entail additional I/O when a frame is chosen for replacement. (Write-Ahead Log protocol; more later.)
19
Policy can have big impact on # of I/Os; depends on the access pattern. Sequential flooding: Nasty situation caused by LRU + repeated sequential scans.
# buffer frames < # pages in file means each page request causes an I/O. MRU much better in this situation (but not in all situations, of course).
20
pin a page in buffer pool, force a page to disk (important for implementing CC & recovery), adjust replacement policy, and pre-fetch pages based on access patterns in typical DB operations.
21
F2
L2
F3
L3
F4
L4
Address = B+L1+L2
Information about field types same for all records in a file; stored in system catalogs. Finding ith field does not require scan of record.
4
Field Count
Array of Field Offsets Second offers direct access to ith field, efficient storage of nulls (special dont know value); small directory overhead.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23
...
Slot N
...
Record
id = <page id, slot #>. In first alternative, moving records for free space management changes rid; may not be acceptable.
24
20 N
...
16 2
24 N 1 # slots
SLOT DIRECTORY
Can
move records on page without changing rid; so, attractive for fixed-length records too.
25
Files of Records
Page or block is OK when doing I/O, but higher levels of DBMS operate on records, and files of records. FILE: A collection of pages, each containing a collection of records. Must support:
insert/delete/modify record read a particular record (specified using record id) scan all records (possibly with some conditions on the records to be retrieved)
26
keep track of the pages in a file keep track of free space on pages keep track of the records on a page
Data Page
Data Page
Full Pages
Data Page
Data Page
Data Page
The header page id and Heap file name must be stored someplace. Each page contains 2 `pointers plus data.
Data Page 2
DIRECTORY
Data Page N
The entry for a page can include the number of free bytes on the page. The directory is a collection of pages; linked list implementation is just one alternative.
29