1 storing data: disks and files yanlei diao umass amherst feb 15, 2007 slides courtesy of r....

24
Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 es Courtesy of R. Ramakrishnan and J. Gehrke

Post on 19-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

1

Storing Data: Disks and Files

Yanlei DiaoUMass AmherstFeb 15, 2007

Slides Courtesy of R. Ramakrishnan and J. Gehrke

Page 2: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

2

Memory Hierarchy

Primary storage: Main Memory (and cache) Random access, fast; usually volatile Main memory for currently used data.

Secondary storage: Magnetic Disk Random access, relatively slow; nonvolatile Disk for the main database.

Tertiary storage: Tape Sequential scan (read the entire tape for the last type); nonvolatile

Tapes for archiving older versions of the data.

Page 3: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

3

Disks and DBMS Design

DBMS stores data on disks. This has major implications on DBMS design! READ: transfer data from disk to main memory (RAM) for data processing.

WRITE: transfer data from RAM to disk for persistent storage.

Both are high-cost operations, relative to in-memory operations, so must be planned carefully!

Page 4: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

4

Why Not Store Everything in Main Memory?

Main memory is volatile. We want data to be saved between runs. (Obviously!)

Costs too much. E.g., $100 will buy you either 1GB of RAM or 160GB of disk.

32-bit addressing limitation. 232 bytes can be directly addressed in memory.

Number of objects cannot exceed this number.

Page 5: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

5

Basics of Disks

Unit of storage and retrieval: disk block or page. A disk block/page is a contiguous sequence of bytes.

Size is a DBMS parameter, 4KB or 8KB. Like RAM, disks support direct access to a page.

Unlike RAM, time to retrieve a page varies! It depends upon the location on disk. Therefore, relative placement of pages on disk has major impact on DBMS performance!

Page 6: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

6

Components of a Disk

Platters

Spindle Platters

•E.g. spin at 7200 rpm (revolutions per minute)

An array of disk heads connected by an arm assembly.

Spindle

Disk head

Arm movement

Arm assembly

Arm assembly moves in or out to position a head on a desired location. Only one head reads/ writes at any one time.

Page 7: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

7

Data on Disk

Platters

Spindle

Disk head

Arm movement

Arm assembly

A platter consists of concentric rings called tracks.

Single vs. double-sided platters.

Tracks under heads make a cylinder (imaginary!).

Each track is divided into sectors (whose size is fixed).

Block (page) size is set to be a multiple of sector size.

Sector

Track

Page 8: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

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 less than 1msec for a 4KB page

Key to lower I/O cost: reduce seek/rotation delays! Hardware vs. software solutions?

Page 9: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

9

Arranging Pages on Disk

`Next’ block concept: blocks on the same track, followed by blocks on the same cylinder, followed by blocks on an adjacent cylinder

Blocks in a file should be arranged sequentially on disk (by `next’), to minimize seek and rotational delay. Scan of the file is a sequential scan. For a sequential scan, pre-fetching several pages at a time can be a big win.

Page 10: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

10

Disk Space Manager

Disk space manager is the lowest layer of DBMS managing space on disk.

Higher levels call upon this layer to: allocate/de-allocate a page or a sequence of pages

read/write a page Requests for a sequence of pages are 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.

Page 11: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

11

Files

Access method layer offers an abstraction of disk-resident data: a file of records residing on multiple pages A number of fields are organized in a record

A collection of records are organized in a page

A collection of pages are organized in a file

Page 12: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

12

Record Format: Fixed Length

Record type: the number of fields and type of each field, stored in the system catalog.

Fixed length record: (1) the number of fields is fixed, (2) each field has a fixed length.

Store fields consecutively in a record. Finding i’th field does not require a scan of the record.

L1 L2 L3 L4

F1 F2 F3 F4

Base address (B) Address = B+L1+L2

Page 13: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

13

Record Format: Variable Length

Variable length record: (1) number of fields is fixed, (2) some fields are of variable length

2nd choice offers direct access to i’th field, efficient storage of nulls; but small directory overhead.

F1 F2 F3 F4

S1 S2 S3 S4 E4Array of Field Offsets

$ $ $ $

Scan

Fields Delimited by Special Symbols

F1 F2 F3 F4

Page 14: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

14

Page Format

How to store a collection of records on a page?

Consider a page as a collection of slots, one for each record.

A record is identified by rid = <page id, slot #>

Record ids (rids) are used in indexes (Alternatives 2 and 3).

Page 15: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

15

Page Format: Fixed Length Records

Moving records for free space management changes rid! Unacceptable for performance.

Slot 1Slot 2

Slot N

PACKED

N

FreeSpace

number of records

. . .

M10. . .

M ... 3 2 1

UNPACKED, BITMAP

Slot 1Slot 2

Slot N

Slot M

11

numberof slots

. . .

Page 16: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

16

Page Format: Variable Length Records

Can move records on a page without changing rids; so, attractive for fixed-length records too. (level of indirection)

Page iRid = (i,N)

Rid = (i,2)

Rid = (i,1)

20 16 24 N Pointerto startof freespace

SLOT DIRECTORY

N . . . 2 1# slots

Page 17: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

17

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 records read a particular record (specified using record id)

scan all records (possibly with some conditions on the records to be retrieved)

Files in DBMS versus Files in OS?

Page 18: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

18

Unordered (Heap) Files

Simplest file structure contains records in no particular order.

As a file grows and shrinks, disk pages are allocated and de-allocated.

To support record level operations, we must: keep track of the pages in a file keep track of free space on pages keep track of the records on a page Many alternatives for keeping track of this.

Page 19: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

19

Heap File Implemented as a List

DBMS: table of (heap file name, header page id), stored in a known place.

Two doubly linked lists, for full pages & pages with space. Each page contains 2 `pointers’ plus data.

Upon insertion, scan the list of pages with space, or ask disk space manager to allocate a new page

HeaderPage

DataPage

DataPage

DataPage

DataPage

DataPage

DataPage Pages with

Free Space

Full Pages

Page 20: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

20

Heap File Using a Page Directory

A directory entry per page: (a pointer to the page, the number of free bytes on the page).

The directory is a collection of pages; linked list is just one implementation. Much smaller than linked list of all HF pages!

Search for space for insertion: fewer I/Os

DataPage 1

DataPage 2

DataPage N

HeaderPage

DIRECTORY

Page 21: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

21

System Catalogs For each index:

structure (e.g., B+ tree) and search key fields For each relation:

name, file name, file structure (e.g., Heap file)

attribute name and type, for each attribute index name, for each index integrity constraints

For each view: view name and definition

Plus statistics, authorization, buffer pool size, etc.

Catalogs are themselves stored as relations!

Page 22: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

22

Attr_Cat(attr_name, rel_name, type, position)

attr_name rel_name type positionattr_name Attribute_Cat string 1rel_name Attribute_Cat string 2type Attribute_Cat string 3position Attribute_Cat integer 4sid Students string 1name Students string 2login Students string 3age Students integer 4gpa Students real 5fid Faculty string 1fname Faculty string 2sal Faculty real 3

Catalogs are themselves stored as relations!

Page 23: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

23

Summary Disks provide cheap, non-volatile storage. Random access But cost depends on location of page on disk Important to arrange data sequentially to minimize seek and rotation delays.

Variable length record format with field offset directory offers support for direct access to i’th field and null values.

Slotted page format supports variable length records and allows records to move on page.

Page 24: 1 Storing Data: Disks and Files Yanlei Diao UMass Amherst Feb 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke

24

Summary (Contd.)

File organization keeps track of pages in a file, supports abstraction of a collection of records. Pages (full or with free space) identified using linked list or directory structure.

Indexes support efficient retrieval of records based on the values in some fields.

Catalog relations store information about relations, indexes and views. (Information that is common to all records in a given collection.)