Storing data : Disk Organization and I/O

26
VIJAY KUMAR SHARMA VIJAY KUMAR SHARMA 1 Storing Data Storing Data Disk Organization Disk Organization and I/O and I/O www.kumarvijaybaswa.blogspot. www.kumarvijaybaswa.blogspot. com com

description

 

Transcript of Storing data : Disk Organization and I/O

Page 1: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 11

Storing DataStoring Data

Disk Organization and I/ODisk Organization and I/O

www.kumarvijaybaswa.blogspot.comwww.kumarvijaybaswa.blogspot.com

Page 2: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 22

DisksDisks DBMS stores information on (“hard”) disks.DBMS stores information on (“hard”) disks. This has major implications for DBMS design!This has major implications for DBMS design!

• READ: READ: transfer data from disk to main memory transfer data from disk to main memory (RAM).(RAM).

• WRITE: WRITE: transfer data from RAM to disk.transfer data from RAM to disk.• Both are high-cost operations, relative to in-Both are high-cost operations, relative to in-

memory operations, so must be planned memory operations, so must be planned carefully!carefully!

Page 3: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 33

Why Not Store Everything in Main Memory?Why Not Store Everything in Main Memory?

Costs too muchCosts too much. . $1000 will buy you $1000 will buy you over 128MB of RAM or 7.5GB of disk over 128MB of RAM or 7.5GB of disk today.today.

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

Page 4: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 44

Typical storage hierarchy:Typical storage hierarchy:• Main memory (RAM) for currently used Main memory (RAM) for currently used

data.data.• Disk for the main database (secondary Disk for the main database (secondary

storage).storage).• Tapes for archiving older versions of the Tapes for archiving older versions of the

data (tertiary storage).data (tertiary storage).

Page 5: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 55

DisksDisks

Secondary storage device of Secondary storage device of choice. choice.

Main advantage over tapes: Main advantage over tapes: random accessrandom access vs. vs. sequentialsequential..

Data is stored and retrieved in Data is stored and retrieved in units called units called disk blocks disk blocks or or pagespages..

Page 6: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 66

Unlike RAM, time to retrieve a disk Unlike RAM, time to retrieve a disk page varies depending upon location page varies depending upon location on disk. on disk. • Therefore, relative placement of pages Therefore, relative placement of pages

on disk has major impact on DBMS on disk has major impact on DBMS performance!performance!

Page 7: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 77

Components of a Disk Components of a Disk

Platters

The platters spin (say, 100rps).

Spindle

The arm assembly is moved in or out to position a head on a desired track. Tracks under heads make a cylinder (imaginary!).

Disk head

Arm movement

Arm assembly

Only one head reads/writes at any one time.

Tracks

Sector

Block size is a multiple of sector size (which is fixed).

Page 8: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 88

Accessing a Disk PageAccessing a Disk Page

Time to access (read/write) a disk Time to access (read/write) a disk block:block:• seek time seek time ((moving arms to position disk moving arms to position disk

head on trackhead on track))• rotational delay rotational delay ((waiting for block to rotate waiting for block to rotate

under headunder head)) often called “rotational latency”often called “rotational latency”

• transfer time transfer time ((actually moving data to/from actually moving data to/from disk surfacedisk surface))

Page 9: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 99

Seek time and rotational delay dominate.Seek time and rotational delay dominate.• Seek time varies from about 1 to 20msecSeek time varies from about 1 to 20msec• Rotational delay varies from 0 to 10msecRotational delay varies from 0 to 10msec• Transfer rate is about 1msec per 4KB pageTransfer rate is about 1msec per 4KB page

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

Page 10: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1010

Arranging Pages on DiskArranging Pages on Disk

`̀NextNext’ ’ block concept: block concept: • blocks on same track, followed byblocks on same track, followed by• blocks on same cylinder, followed byblocks on same cylinder, followed by• blocks on adjacent cylinderblocks on adjacent cylinder

Blocks in a file should be arranged Blocks in a file should be arranged sequentially on disk (by `next’), to sequentially on disk (by `next’), to minimize seek and rotational minimize seek and rotational delay.delay.

Page 11: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1111

For a For a sequential scansequential scan, , pre-fetchingpre-fetching several pages at a time is a big win!several pages at a time is a big win!

Page 12: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1212

Disk Space ManagementDisk Space Management

Lowest layer of DBMS software Lowest layer of DBMS software manages space on disk.manages space on disk.

Higher levels call upon this layer to:Higher levels call upon this layer to:• allocate/de-allocate a pageallocate/de-allocate a page• read/write a pageread/write a page

Page 13: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1313

One such “higher level” is the buffer One such “higher level” is the buffer manager, which receives a request manager, which receives a request to bring a page into memory and to bring a page into memory and then, if needed, requests the disk then, if needed, requests the disk space layer to read the page into the space layer to read the page into the buffer pool.buffer pool.

Page 14: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1414

Buffer Management in a DBMSBuffer Management in a DBMS

DB

MAIN MEMORY

DISK

disk page

free frame

Page Requests from Higher Levels

BUFFER POOL

choice of frame dictatedby replacement policy

Page 15: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1515

Data must be in RAM for DBMS to Data must be in RAM for DBMS to operate on it!operate on it!

Table of <frame#, pageid> pairs is Table of <frame#, pageid> pairs is maintained.maintained.

Page 16: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1616

When a Page is Requested ...When a Page is Requested ...

If requested page is not in pool:If requested page is not in pool:• Choose an unpinned frame for Choose an unpinned frame for

replacementreplacement; pin_count := 1; pin_count := 1• If frame is dirty, write it to diskIf frame is dirty, write it to disk• Read requested page into chosen frameRead requested page into chosen frame

Else:Else:• increment pin_countincrement pin_count

Page 17: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1717

Return its address. Return its address.

If requests can be predicted (e.g., sequential scans) pages can be pre-fetched several pages at a time!

Page 18: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1818

More on Buffer ManagementMore on Buffer Management

Requestor of page must unpin it, and Requestor of page must unpin it, and indicate whether page has been indicate whether page has been modified: modified: • dirtydirty bit is used for this.bit is used for this.

Page in pool may be requested many Page in pool may be requested many times: times: • a a pin count pin count is used. A page is a is used. A page is a

candidate for replacement iff candidate for replacement iff pin count pin count = 0.= 0.

Page 19: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 1919

CC & recovery may entail additional CC & recovery may entail additional I/O when a frame is chosen for I/O when a frame is chosen for replacement. (replacement. (Write-Ahead Log Write-Ahead Log protocol; more later.)protocol; more later.)

Page 20: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 2020

Buffer Replacement PolicyBuffer Replacement Policy

Frame is chosen for replacement by Frame is chosen for replacement by a a replacement policy:replacement policy:• Least-recently-used (LRU), Clock, MRU, Least-recently-used (LRU), Clock, MRU,

etc.etc. Policy can have big impact on # of Policy can have big impact on # of

I/O’s; depends on the I/O’s; depends on the access patternaccess pattern..

Page 21: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 2121

Sequential floodingSequential flooding: : Nasty situation Nasty situation caused by LRU + repeated caused by LRU + repeated sequential scans.sequential scans.• # buffer frames < # pages in file # buffer frames < # pages in file means means

each page request causes an I/O. MRU each page request causes an I/O. MRU much better in this situation (but not in much better in this situation (but not in all situations, of course).all situations, of course).

Page 22: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 2222

Files of RecordsFiles of Records

Page or block is OK when doing I/O, Page or block is OK when doing I/O, but higher levels of DBMS operate on but higher levels of DBMS operate on recordsrecords, and , and files of recordsfiles of records..

Page 23: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 2323

FILEFILE: A collection of pages, each : A collection of pages, each containing a collection of records. containing a collection of records. Must support:Must support:• insert/delete/modify recordinsert/delete/modify record• read a particular record (specified using read a particular record (specified using

record idrecord id))• scan all records (possibly with some scan all records (possibly with some

conditions on the records to be conditions on the records to be retrieved)retrieved)

Page 24: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 2424

Disk and File SummaryDisk and File Summary

Disks provide cheap, non-volatile Disks provide cheap, non-volatile storage.storage.• Random access, but cost depends on Random access, but cost depends on

location of page on disk; important to location of page on disk; important to arrange data sequentially to minimize arrange data sequentially to minimize seekseek and and rotation rotation delays.delays.

Page 25: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 2525

Buffer manager brings pages into RAM.Buffer manager brings pages into RAM.• Page stays in RAM until released by Page stays in RAM until released by

requestor(s).requestor(s).• Written to disk when frame chosen for Written to disk when frame chosen for

replacement (which is after all requestors replacement (which is after all requestors release the page), or earlier.release the page), or earlier.

• Choice of frame to replace based on Choice of frame to replace based on replacement policy.replacement policy.

File layer keeps track of pages in a file, File layer keeps track of pages in a file, and supports abstraction of a collection of and supports abstraction of a collection of records.records.

Page 26: Storing data : Disk Organization and I/O

VIJAY KUMAR SHARMAVIJAY KUMAR SHARMA 2626

End End