Storage and File Structure. Architecture of a DBMS.
-
Upload
shona-holmes -
Category
Documents
-
view
238 -
download
0
Transcript of Storage and File Structure. Architecture of a DBMS.
Outline
• Overview of Physical Storage Media• Magnetic Disks• RAID• Tertiary Storage • Storage Access• File Organization• Organization of Records in Files
• DBMS stores information on (“hard”) disks.– A disk is a sequence of bytes, each has a disk
address.– READ: transfer data from disk to main memory (RAM).– WRITE: transfer data from RAM to disk.
• Data are stored and retrieved in units called disk blocks or pages.– Each page has a fixed size, say 512 bytes. It contains
a sequence of records.– Typically records in a page have the same size, say
100 bytes.– Typically records implement relational tuples.
Disks and Files
Cache
Main memory
Flash memory
Magnetic disk
Optical disk
Magnetic tapes
Storage-device hierarchy
Small capacityPower on
Small capacityPower on
medium capacityPower off
large capacityPower off
large capacityPower off
Very large capacityPower off
Very fastVery expensive
Very fastexpensive
SlowerCheap
Slowercheap
Slowercheap
Very slowVery cheap
Why Not Store Everything in Main Memory?
• Costs too much. RAM is much more expensive than disk.
• 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).
Arranging Pages on Disk
• Next block concept: – blocks on same track,
followed by– blocks on same
cylinder, followed by– blocks on adjacent
cylinder
• If blocks in a file are arranged sequentially on disk (by `next’), we minimize seek and rotational delay.
Platters
Spindle
Disk head
Arm movement
Arm assembly
Tracks
Cylinder
Only one head reads/writes a block at any one time.
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 (Accessing main memory location – 60 nanoseconds)
• Key to lower I/O cost: reduce seek/rotation delays!
RAID
• Redundant Arrays of Independent Disks
Disk array – several disks organized to increase performance and reliability of a storage system.
Data striping - distributes data over several disks – data is partitioned and distributed in a round-robin manner.
Striping unit - e.g. bit (bit interleaved) or block (block interleaved)
C C C C
P P P
RAID 0: Non-Redundant Striping
RAID 1: Mirrored Disks
RAID 2: Memory Style Error Correcting Codes – Hamming Code
RAID Level
Hamming Code
• Use of extra parity bits to identify single error• Parity bits: All bit positions that are powers of two E.g. 1, 2, 4, 8, …
• Position 1: check 1 bit, skip 1 bit, check 1 bit, skip 1 bit, etc. (1,3,5,7,9,11,13,15,...)
• Position 2: check 2 bits, skip 2 bits, check 2 bits, skip 2 bits, etc. (2,3,6,7,10,11,14,15,...)
• Position 4: check 4 bits, skip 4 bits, check 4 bits, skip 4 bits, etc. (4,5,6,7,12,13,14,15,20,21,22,23,...)
• Position 8: check 8 bits, skip 8 bits, check 8 bits, skip 8 bits, etc. (8-15,24-31,40-47,...)
Set parity bit to 1 if the number of “ones” in the checked bits is odd, set parity bit to 0 if it is even.Given data: 10011010000 ---- ? ? 1 ? 0 0 1 ? 1 0 1 0 0 0 0
Position 1 checks bits 1,3,5,7,9,11,13,15: ? _ 1 _ 0 0 1 _ 1 0 1 0 0 0 0. Even parity so set position 1 to a 0
Position 2 checks bits 2,3,6,7,10,11, 14,15:0 ? 1 _ 0 0 1 _ 1 0 1 0 0 0 0. Odd parity so set position 2 to a 1
Position 4 checks bits 4,5,6,7,12,13,14,15:0 1 1 ? 0 0 1 _ 1 0 1 0 0 0 0. Odd parity so set position 4 to a 1
Position 8 checks bits 8,9,10,11,12,13,14,15:0 1 1 1 0 0 1 ? 1 0 1 0 0 0 0. Even parity so set position 8 to a 0
Code word: 011100101010000
Suppose bit 5 has error 011100101010000 011110101010000
Parity bit discrepencies indicates position of error : 1010
P P P
RAID 2: Memory Style Error Correcting Codes
P -- disks for the parity bits, with 7 bits,bits 1, 2, 4 are parity bits and 3,5,6,7 are data bits.
Costly -- we need several parity disks to locate the single disk failure (note that it does not handle > 1 failed disk)
RAID
• Disk controller can find out which disk has failed.• no need to locate failure by parity bits• only keep one parity bit for the 4 data bits.
• If the number of ones in checked bits and parity bit is odd, the data bit should be corrected.
• 0 1 0 0 0 -- change 0 to 1
• 1 1 0 1 1 -- no change
P
RAID 3: Bit Interleaved Parity
Disk controller knows this disk has failed
RAID
• RAID 3 : A single block is distributed to all disks.
• RAID 4 : a single block can be served by a single disk
• Both have a single parity disk, which is a bottleneck since each write operation accesses this disk.
• Hence we may distribute the parity blocks also
P
RAID 3: Bit Interleaved Parity
P
RAID 4: Block Interleaved Parity
P
P
P PP P
P P
P P
P P
RAID 3: Bit Interleaved Parity
RAID 4: Block Interleaved Parity
P P P
RAID 5: Block Interleaved Distributed Parity
RAID 6: P + Q Redundancy: for > 1 error
PP
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
• If requested page is not in pool:– Choose a frame for replacement– If frame is dirty (updated), write it to disk– Read requested page into chosen frame
• Pin the page and return its address to the requestor.
If requests can be predicted (e.g., sequential scans)
pages can be pre-fetched several pages at a time!
Buffer Management in a DBMS
When a Page is Requested
• To release a page, requestor of a page must unpin it, and indicate whether the page has been modified: – dirty bit is used for this.
• Page in pool may be requested many times, – a pin count is used. A page is a candidate for
replacement iff pin count = 0.
• Concurrency control & recovery may entail additional I/O when a frame is chosen for replacement.
• Frame is chosen for replacement by a replacement policy: Least-recently-used (LRU), MRU etc.
Buffer Management in a DBMS
Record Formats: Fixed Length
• Information about field types the same for all records in a file; stored in system catalogs.
Base address (B)
L1 L2 L3 L4
F1 F2 F3 F4
Address = B+L1+L2
Record Formats: Variable Length
Two alternative formats (# fields is fixed):
Second format offers direct access to the i’th field
4 $ $ $ $
FieldCount
Fields Delimited by Special Symbols
F1 F2 F3 F4
F1 F2 F3 F4
Array of Field Offsets
Fixed Length Records on a page
Record id = <page id, slot #>. In the PACKED 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
Variable Length Records on a page
Can move records on a page without changing rid; so, attractive for fixed-length records too.
Page iRid = (i,N)
Rid = (i,2)
Rid = (i,1)
Pointerto startof freespace
SLOT DIRECTORY
N . . . 2 1 20 16 24 N
# slots
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)
Heap File –(randomly orderd file)-Implemented as a List
• The header page id and Heap file name must be stored somewhere.
• Each page contains 2 `pointers’ plus data.
HeaderPage
DataPage
DataPage
DataPage
DataPage
DataPage
DataPage Pages with
Free Space
Full Pages
Heap File Using a Page Directory
• The entry for a page can include the number of free bytes on the page.
• The directory is a collection of pages• Can easily search for a page with enough space for a
record to be inserted.
Data Page 1
DataPage 2
DataPage N
HeaderPage
DIRECTORY
Alternative File Organizations
• 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.
Daniels,22,6003
Ashby,25,3000
Bristow,29,2007
Basu, 33, 4003
Jones, 40, 6003
Smith, 44, 3000
Tracy, 44, 5004
Cass, 50, 5004
Sorted file onAge field
Smith, 44, 3000
Jones, 40, 6003
Tracy, 44, 5004
Ashby, 25, 3000
Basu, 33, 4003
Bristow,29,2007
Cass,50, 5004
Daniels,22,6003
Heap file(randomlyOrdered)
Indexing
• Indexing mechanisms speed up access to desired data.– E.g., author catalog in library
• Search Key - set of attributes used to look up records in a file.• An index file consists of records (called index entries) of the
form
• Index files are typically much smaller than the original file • Two basic kinds of indices:
– Ordered indices: search keys are stored in sorted order– Hash indices: search keys are distributed uniformly across
“buckets” using a “hash function”.
search-key pointer
Indexes• 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 candidate key or superkey (set of fields that uniquely identify a record in a relation).
• An index contains a collection of index entries
• An index entry is denoted as k* where k is a search key value and * tells where to find record(s) containing k
Given k, an index helps to retrieves all index entries k*
k1* k2*
k4* k5*
k3*
Employee
eid ename address sex dname
Index Entry k* in Index
1. <k, record id (rid) of data record with search key value k>
2. <k, list of rids of data records with search key value k>
assuming field ‘name’ is the search key
1. <“Lin Wang”, 10101> where 10101 is the rid of a record that contains “Lin Wang”
2. <“Lin Wang”, 10101, 10111, 11010> where 10101, 10111, 11010 are records which all contain “Lin Wang”.
Index Classification
• Unique index: Search key contains a candidate key.
• Clustered vs. unclustered: If order of data records is the same as, or `close to’, order
of index entries in the index, then it is called a clustered index.
• A file can be clustered on at most one search key.
• Cost of retrieving data records through index varies greatly based on whether index is clustered or not!
Clustered and unclustered Indexes
Smith, 44, 3000
Jones, 40, 6003
Tracy, 44, 5004
Ashby, 25, 3000
Basu, 33, 4003
Bristow,29,2007Cass,50, 5004
Daniels,22,6003
Heap file(randomlyOrdered)
Daniels,22,6003
Ashby,25,3000
Bristow,29,2007Basu, 33, 4003
Jones, 40, 6003
Smith, 44, 3000
Tracy, 44, 5004
Cass, 50, 5004
File sorted onAge field
Ashby
Basu
Bristow
Cass
Daniels
Jones
Smith
Tracy
Index on search key ‘name’, also the primary keyunclustered
22
33
44
Index on searchKey ‘age’clustered
Overflow page in Clustered index
Ashby,25,3000
Basu, 33, 4003
Bristow,29,2007
Cass, 50, 5004
Daniels,22,6003
Jones, 40, 6003
Smith, 44, 3000
Tracy, 44, 5004
Sorted file onname field
Ashby
Cass
Smith
Index on searchKey ‘name’clustered
We must use anoverflow page
Edward,22,2500
Insert record:(Edward, 22, 2500)
–Note: overflow pages may be needed for inserts. –(Thus, order of data records is ‘close to’, but not identical to, the sort order.)
Dense vs Sparse Index
• Dense vs Sparse:
• If there is at least one index entry in the index per search key value, then dense.
Sparse Index
Ashby, 25, 3000
Smith, 44, 3000
Ashby
Cass
Smith
22
25
30
40
44
44
50On name
Data FileDense Index
On Age
33
Bristow, 30, 2007
Basu, 33, 4003
Cass, 50, 5004
Tracy, 44, 5004
Daniels, 22, 6003
Jones, 40, 6003
Every sparse index is clustered!
Composite search keys
Composite Search Keys:
a combination of fields.
• Equality query: every field value is equal to a constant value. E.g. age=30 and sal =75
<age, sal> or <sal, age> allows efficient search.
sue 30 75
bob
cal
joe 20
10
20
8018
20
name age sal
<sal, age>
<age, sal> <age>
<sal>
20,20
20,10
18,80
30,75
20,20
10,20
75,30
80,18
18
20
20
30
10
20
75
80
Data recordssorted by name
index entries in indexsorted by <sal,age>
index entriessorted by <sal>
Composite search keys
Composite Search Keys: a combination of fields.
• Range query: Some field value is not a constant. E.g.:
• age =30; • age=30 and sal > 10
index entries in an index is sorted by the search key -- support range queries on the search key.
<age, sal> and <sal, age> allow 1 pointer search
<sal> requires 3 pointer searches –
for ages 20, 75, 80
sue 30 75
bob
cal
joe 20
10
20
8018
20
name age sal
<sal, age>
<age, sal> <age>
<sal>
20,20
20,10
18,80
30,75
20,20
10,20
75,30
80,18
18
20
20
30
10
20
75
80
Data recordssorted by name
index entries in indexsorted by <sal,age>
index entriessorted by <sal>
Index only evaluation
Index only query evaluation– no need to retrieve data records
E.g. find the salaries of employees with
• age=30
– <age, sal> or <sal, age>
allows index only evaluation.
– <age, sal> allows faster search.
sue 30 75
bob
cal
joe 20
10
20
8018
20
name age sal
<sal, age>
<age, sal> <age>
<sal>
20,20
20,10
18,80
30,75
20,20
10,20
75,30
80,18
18
20
20
30
10
20
75
80
Data recordssorted by name
index entries in indexsorted by <sal,age>
index entriessorted by <sal>
Choice of Indexes
• Consider the most important queries in turn. Consider the best query evaluation plan using the
current indexes, and see if a better plan is possible with an additional index.
• to do this we must understand how a DBMS evaluates queries and creates query evaluation plans!
• Before creating an index, must also consider the impact on the updates– Trade-off: Indexes can make queries go faster,
but updates slower. Require disk space, too.
Index Selection Guidelines
• Multi-attribute search keys should be considered when a WHERE clause contains several conditions.
– Order of attributes is important for range queries.
– can sometimes enable index-only strategies for important queries.
For index-only strategies, clustering is not important!
• 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.