Download presentation
Presentation is loading. Please wait.
Published byShona Holmes Modified over 9 years ago
1
Storage and File Structure
2
Architecture of a DBMS
3
Outline Overview of Physical Storage Media Magnetic Disks RAID Tertiary Storage Storage Access File Organization Organization of Records in Files
4
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
5
Cache Main memory Flash memory Magnetic disk Optical disk Magnetic tapes Storage-device hierarchy Small capacity Power on Small capacity Power on medium capacity Power off large capacity Power off large capacity Power off Very large capacity Power off Very fast Very expensive Very fast expensive Slower Cheap Slower cheap Slower cheap Very slow Very cheap
6
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).
7
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.
8
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!
9
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)
10
Data bits 1 1 1 0 0 1 1 0 1 … 1 1 1 0 0 1 1 0 1 …. RAID 0: Non-Redundant Striping
11
CCCC PPP RAID 1: Mirrored Disks RAID 2: Memory Style Error Correcting Codes – Hamming Code RAID Level
12
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,...)
13
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
14
PPP 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
15
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
16
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
17
P P PP PPPP PPPP PPPP PPPP RAID 3: Bit Interleaved Parity RAID 4: Block Interleaved Parity PPP RAID 5: Block Interleaved Distributed Parity RAID 6: P + Q Redundancy: for > 1 error PP
18
Buffer Management in a DBMS Data must be in RAM for DBMS to operate on it! Table of pairs is maintained. DB MAIN MEMORY DISK disk page free frame Page Requests from Higher Levels BUFFER POOL choice of frame dictated by replacement policy
19
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
20
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
21
Record Formats: Fixed Length Information about field types the same for all records in a file; stored in system catalogs. Base address (B) L1L2L3 L4 F1 F2F3F4 Address = B+L1+L2
22
Record Formats: Variable Length Two alternative formats (# fields is fixed): * Second format offers direct access to the i’th field 4$$$$ Field Count Fields Delimited by Special Symbols F1 F2 F3 F4 Array of Field Offsets
23
Fixed Length Records on a page *Record id =. In the PACKED alternative, moving records for free space management changes rid; may not be acceptable. Slot 1 Slot 2 Slot N... N M1 0 M... 3 2 1 PACKED UNPACKED, BITMAP Slot 1 Slot 2 Slot N Free Space Slot M 11 number of records number of slots
24
Variable Length Records on a page *Can move records on a page without changing rid; so, attractive for fixed-length records too. Page i Rid = (i,N) Rid = (i,2) Rid = (i,1) Pointer to start of free space SLOT DIRECTORY N... 2 1 20 16 24 N # slots
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
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. Header Page Data Page Data Page Data Page Data Page Data Page Data Page Pages with Free Space Full Pages
27
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 Data Page 2 Data Page N Header Page DIRECTORY
28
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.
29
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 on Age 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 (randomly Ordered)
30
Indexing
31
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
32
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 eidenameaddresssexdname
33
Index Entry k* in Index 1. 2. assuming field ‘name’ is the search key 1. where 10101 is the rid of a record that contains “Lin Wang” 2. where 10101, 10111, 11010 are records which all contain “Lin Wang”.
34
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!
35
Clustered and unclustered Indexes 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 (randomly Ordered) Daniels,22,6003 Ashby,25,3000 Bristow,29,2007 Basu, 33, 4003 Jones, 40, 6003 Smith, 44, 3000 Tracy, 44, 5004 Cass, 50, 5004 File sorted on Age field Ashby Basu Bristow Cass Daniels Jones Smith Tracy Index on search key ‘name’, also the primary key unclustered 22 33 44 Index on search Key ‘age’ clustered
36
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 on name field Ashby Cass Smith Index on search Key ‘name’ clustered We must use an overflow 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.)
37
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 50 On name Data File Dense 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!
38
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 or allows efficient search. sue3075 bob cal joe20 10 20 8018 20 nameagesal 20,20 20,10 18,80 30,75 20,20 10,20 75,30 80,18 18 20 30 10 20 75 80 Data records sorted by name index entries in index sorted by index entries sorted by
39
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. and allow 1 pointer search requires 3 pointer searches – for ages 20, 75, 80 sue3075 bob cal joe20 10 20 8018 20 nameagesal 20,20 20,10 18,80 30,75 20,20 10,20 75,30 80,18 18 20 30 10 20 75 80 Data records sorted by name index entries in index sorted by index entries sorted by
40
Index only evaluation Index only query evaluation– no need to retrieve data records E.g. find the salaries of employees with age=30 – or allows index only evaluation. – allows faster search. sue3075 bob cal joe20 10 20 8018 20 nameagesal 20,20 20,10 18,80 30,75 20,20 10,20 75,30 80,18 18 20 30 10 20 75 80 Data records sorted by name index entries in index sorted by index entries sorted by
41
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.
42
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.