View
224
Download
6
Tags:
Embed Size (px)
Citation preview
Murali Mani
Overview of Storage and Indexing
(based on slides from Wisconsin)
Murali Mani
Data on External Storage
Disks: Can retrieve random page at fixed cost But reading several consecutive pages is much cheaper
than reading them in random order Tapes: Can only read pages in sequence
Cheaper than disks; used for archival storage File organization: Method of arranging a file of
records on external storage. Record id (rid) is sufficient to physically locate record Indexes are data structures that allow us to find the record
ids of records with given values in index search key fields Architecture: Buffer manager stages pages from
external storage to main memory buffer pool. File and index layers make calls to the buffer manager.
Murali Mani
Why Not Store Everything in Main Memory?
Cost. Disks always cheaper than memory. Main memory is volatile. We want data to be
saved between runs !! Typical storage hierarchy:
Main memory (RAM) for currently used data. Disk for the main database (secondary storage). Tapes for archiving older versions of the data
(tertiary storage).
Murali Mani
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!
Murali Mani
Components of a Disk
The platters spin (say, 90rps). 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.
Platters
Spindle
Disk head
Arm movement
Arm assembly
Tracks
Sector
Block size is a multiple of sector size (which is fixed).
Murali Mani
Accessing a Disk Page
Time to access (read/write) a disk block: 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 and rotational delay dominate. 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?
Murali Mani
Arranging Pages on Disk
`Next’ block concept: blocks on same track, followed byblocks on same cylinder, followed byblocks 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!
Murali Mani
RAID
Disk Array: Arrangement of several disks that gives abstraction of a single, large disk.
Goals: Increase performance and reliability. Two main techniques:
Data striping: Data is partitioned; size of a partition is called the striping unit. Partitions are distributed over several disks.
Redundancy: More disks => can handle more failures. Redundant information allows reconstruction of data if a disk fails.
Murali Mani
Disk Space Management
Lowest layer of DBMS software manages space on disk.
Higher levels call upon this layer to: allocate/de-allocate a page read/write a page
Request for a sequence of pages must be satisfied by allocating the pages sequentially on disk! Higher levels don’t need to know how this is done, or how free space is managed.
Murali Mani
Buffer Management in a DBMS
Data must be in RAM for DBMS to operate on it! Table of <frame#, pageid> pairs is maintained.
DB
MAIN MEMORY
DISK
disk page
free frame
Page Requests from Higher Levels
BUFFER POOL
choice of frame dictatedby replacement policy
Murali Mani
DBMS vs. OS File System
OS does disk space & buffer mgmt: why not let OS manage these tasks?
Differences in OS support: portability issues Some limitations, e.g., files can’t span disks.
Murali Mani
Record Formats: Fixed Length
Information about field types same for all records in a file; stored in system catalogs.
Finding i’th field does not require scan of record.
Base address (B)
L1 L2 L3 L4
F1 F2 F3 F4
Address = B+L1+L2
Murali Mani
Record Formats: Variable Length
Two alternative formats (# fields is fixed):
Second offers direct access to i’th field, efficient storage of nulls (special don’t know value); small directory overhead.
4 $ $ $ $
FieldCount
Fields Delimited by Special Symbols
F1 F2 F3 F4
F1 F2 F3 F4
Array of Field Offsets
Murali Mani
Page Formats: Fixed Length Records
Record id = <page id, slot #>. In first alternative, moving records for free space management changes rid; may not be acceptable.
Slot 1Slot 2
Slot N
. . . . . .
N M10. . .
M ... 3 2 1PACKED UNPACKED, BITMAP
Slot 1Slot 2
Slot N
FreeSpace
Slot M
11
number of records
numberof slots
Murali Mani
Page Formats: Variable Length Records
Can move records on page without changing rid; so, attractive for fixed-length records too.
Page iRid = (i,N)
Rid = (i,2)
Rid = (i,1)
Pointerto startof freespaceSLOT DIRECTORY
N . . . 2 120 16 24 N
# slots
Murali Mani
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)
Murali Mani
Alternative File Organizations
Many alternatives exist, each ideal for some situations, and not so good in others: Heap (random order) files: Suitable when typical access
is a file scan retrieving all records. Sorted Files: Best if records must be retrieved in some
order, or only a `range’ of records is needed. Indexes: Data structures to organize records via trees or
hashing. Like sorted files, they speed up searches for a subset of
records, based on values in certain (“search key”) fields Updates are much faster than in sorted files.
Murali Mani
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 k* with a given key value k.
Murali Mani
B+ Tree Indexes
Leaf pages contain data entries, and are chained (prev & next) Non-leaf pages have index entries; only used to direct searches:
P0 K 1 P 1 K 2 P 2 K m P m
index entry
Non-leafPages
Pages (Sorted by search key)
Leaf
Murali Mani
Example B+ Tree
Find 28*? 29*? All > 15* and < 30* Insert/delete: Find data entry in leaf, then change
it. Need to adjust parent sometimes. And change sometimes bubbles up the tree
2* 3*
Root
17
30
14* 16* 33* 34* 38* 39*
135
7*5* 8* 22*24*
27
27* 29*
Entries <= 17 Entries > 17
Note how data entriesin leaf level are sorted
Murali Mani
Hash-Based Indexes
Good for equality selections. Index is a collection of buckets.
Bucket = primary page plus zero or more overflow pages.
Buckets contain data entries. Hashing function h: h(r) = bucket in which
(data entry for) record r belongs. h looks at the search key fields of r. No need for “index entries” in this scheme.
Murali Mani
Choice of Indexes
What indexes should we create? Which relations should have indexes? What
field(s) should be the search key? Should we build several indexes?
For each index, what kind of an index should it be? Hash/tree?
Murali Mani
Choice of Indexes (Contd.)
One approach: Consider the most important queries in turn. Consider the best plan using the current indexes, and see if a better plan is possible with an additional index. If so, create it. Obviously, this implies that we must understand how a
DBMS evaluates queries and creates query evaluation plans! For now, we discuss simple 1-table queries.
Before creating an index, must also consider the impact on updates in the workload! Trade-off: Indexes can make queries go faster, updates
slower. Require disk space, too.
Murali Mani
Index Selection Guidelines Attributes in WHERE clause are candidates for index keys.
Exact match condition suggests hash index. Range query suggests tree index.
Multi-attribute search keys should be considered when a WHERE clause contains several conditions. Order of attributes is important for range queries. Such indexes can sometimes enable index-only strategies for
important queries. Try to choose indexes that benefit as many queries as
possible. Since only one index can be clustered per relation, choose it based on important queries that would benefit the most from clustering.
Murali Mani
Examples of Indexes B+ tree index on E.age can
be used to get qualifying tuples. How selective is the condition?
Consider the GROUP BY query. If many tuples have E.age >
10, using E.age index and sorting the retrieved tuples may be costly.
Equality queries and duplicates:
SELECT E.dnoFROM Emp EWHERE E.age>40
SELECT E.dno, COUNT (*)FROM Emp EWHERE E.age>10GROUP BY E.dno
SELECT E.dnoFROM Emp EWHERE E.hobby=Stamps
Murali Mani
Indexes with 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 Data entries in index sorted by
search key to support range queries.
sue 13 75
bob
cal
joe 12
10
20
8011
12
name age sal
<sal, age>
<age, sal> <age>
<sal>
12,20
12,10
11,80
13,75
20,12
10,12
75,13
80,11
11
12
12
13
10
20
75
80
Data recordssorted by name
Data entries in indexsorted by <sal,age>
Data entriessorted by <sal>
Examples of composite keyindexes using lexicographic order.
Murali Mani
Composite Search Keys
To retrieve Emp records with age=30 AND sal=4000, an index on <age,sal> would be better than an index on age or an index on sal. Choice of index key orthogonal to clustering etc.
If condition is: 20<age<30 AND 3000<sal<5000: Clustered tree index on <age,sal> or <sal,age> is best.
If condition is: age=30 AND 3000<sal<5000: Clustered <age,sal> index much better than <sal,age> index!
Composite indexes are larger, updated more often.