Storage, Data Organization, and Buffering Walid G. Aref
Memory Hierarchy Archival Storage Higher (e.g., tapes) Capacity Network Storage Block-oriented access Non- Disk Storage Volatile Solid-State Disks Main Memory Byte-oriented Volatile Cache Memory access CPU Registers Higher Higher Price Speed Walid G. Aref
Cache Memory • L1 Cache (Level 1 Cache): • Memory built inside the microprocessor chip • Stores the CPU's recently accessed data • Also, termed system cache, primary cache, internal cache • Fastest ( ≈ 100 𝑢𝑗𝑛𝑓𝑡 𝑔𝑏𝑡𝑢𝑓𝑠 𝑢ℎ𝑏𝑜 𝑛𝑏𝑗𝑜 𝑛𝑓𝑛𝑝𝑠𝑧) • For a multi-core CPU, every core will have a separate L1 case • Not shared with the other cores • Relatively small in size (~ 32K/core) • L2 Cache (Level 2 Cache): • For a multi-core CPU, L2 Cache Is in-chip memory that is shared among the multiple cores • Larger in capacity than L1 cache • ~4 MB per 4 cores (Intel Anon) • 𝑇𝑢𝑗𝑚𝑚 𝐺𝑏𝑡𝑢𝑓𝑠 𝑢ℎ𝑏𝑜 𝑛𝑏𝑗𝑜 𝑛𝑓𝑛𝑝𝑠𝑧 (≈ 25 𝑢𝑗𝑛𝑓𝑡) • L3 Cache (Level 3 Cache) or Level 4: • Memory outside of the microprocessor chip in a separate chip • 𝑇𝑚𝑝𝑥𝑓𝑠 𝑢ℎ𝑏𝑜 𝑀1 𝑏𝑜𝑒 𝑀2 𝑑𝑏𝑑ℎ𝑓𝑡 • Larger in capacity than L1 and L2 cache Walid G. Aref
Disk Storage Disk-based Storage Main Memory • The focus of this course Buffers • Assumption: • Data cannot entirely fit in main memory Processor • Use memory as buffers Rotational Delay • Access: Block-based Seek Time • Disk Access: • Seek time (~10 ms): • Time to move the heads IN/Out • Rotational Delay (~10 ms): Data Transfer • Time to rotate around the spindle Time (measured in RPM) • Transmission delay (~1ms): • Time to transfer a block of data Walid G. Aref
Disk Interface • Each disk page has a disk block id • Block Read(disk block id): • Transmit the corresponding disk page from the disk to the memory buffers • Write(Block, disk block id): • Transmit the data Block from the memory buffers into the disk page corresponding to the given disk block id Walid G. Aref
Data Retrieval From Disk • Each disk page has a Disk Block identifier • Disk Block Identifier gets mapped into: • (Cylinder (or Track) Number, Platter Number, Sector Number) • Time needed is dependent on: • The current location of the disk head, and • The location of the requested page. Track • Involves mechanical movement, and hence is relatively slow • How to retrieve the page? 1. Seek: • Head moves to the corresponding cylinder number (uses an elevator algorithm) 2. Rotation: 1. Seek to • Wait until head is on top of the correct Sector Cylinder Number 3. Data Number 3. Transmission: Transfer Time 2. Sector Number • Once head is at the right cylinder, right sector, direct and give the signal to the head responsible for the block’s Platter Number to start reading the block and transmit the bits to the disk driver buffers 3. Platter Number Walid G. Aref • Same is true for writing a page to disk
How to Place Multiple Pages on Disk? • Data Placement Problem • Given a table with multiple data blocks (d 1 , d 2 , d 3 , …) • How to place the data blocks on disk to minimize file read time? • Assume we have p platters, s sectors per track • Store d 1 in Cylinder c 1 , Platter p 1 , Sector s 1 • Where to store d 2 Track • d 2 à c 1 , p 2 , s 1 (Seek Time = 0, Rotational Delay = 0) • d 3 à c 1 , p 3 , s 1 (Seek Time = 0, Rotational Delay = 0) • … • d p à c 1 , p p , s 1 (Seek Time = 0, Rotational Delay = 0) • d p+1 à c 1 , p 1 , s 2 (Seek Time = 0, Rotational Delay = 1 sector) • d p+2 à c 1 , p 2 , s 2 (Seek Time = 0, Rotational Delay = 0) 1. Cylinder • Number 3. Data ... • Transfer Time --- 2. Sector Number • d ps à c 1 , p p , s s (Seek Time = 0, Rotational Delay = 0) • d ps+1 à c 2 , p p , s s (Seek Time = 1, Rotational Delay = 0) 3. Platter Number • Walid G. Aref etc.
Data Placement and Data Prefetching • When Page 1 of the table is requested, Pre-retrieve Pages 2, 3, and 4 before being requested ( Prefetching ) • May result in saving time • Then, when actually requesting Page 2, will find it in the memory buffers. • Can also be wasted effort if Page 2 is not requested • But will benefit from good data placement, and hence retrieval cost of Page 2 is small • What else can make Data Placement work wasted? • Multi-tenant databases or concurrent transactions accessing the disk at the same time and each requesting to retrieve a different file at the same time. • Proper and careful placement of data of one file will not help in this case. • The reason is that requests for pages from multiple files will be interleaved. Walid G. Aref
How to Access Pages of a Table? • Assume for now that no indexes are available. • Given a table name, how to know the list of disk pages that comprise the table? • Directory: Table name à Head of linked list of pages Head Page 1 Page 2 Page n • Each page has forward and backward pointers (disk page ids) • Sequential access Walid G. Aref
How to Access Pages of a Table? (2) • To reduce the sequential access overhead for large tables, maintain a two-level hierarchy (directory of pages) 1 2 3 4 • Directory Pages à n 5 • Data Pages à Page 1 Page 2 Page 3 Page n Walid G. Aref
How to Store Tuples Inside a Disk Page? • Tuples of a table can be of two types: • Tuples in the table are all of the same length --> Fixed-length records • Each tuple in the table can be of a different length than the other tuples à Variable-length Records Walid G. Aref
Tuple Identifiers: How to Refer to a Tuple in a Table? • Using a Tuple Identifier or a Record Identifier (TID or RID, for short) • Tuple Identifier: Used to physically locate a tuple • What composes a TID? • Disk page id of the page that contains the tuple • The index (or offset) of the tuple inside the page (more details to follow) • Where can a TID be used? • In the leaf level of an index, e.g., B+-tree, to point to the tuple (key-value, TID) pairs • In a foreign-key/primary-key relationship, the foreign key side can contain the TID of the primary key side • An alternative to storing the key value • Helps avoid performing a join • Important property of a TID: • TID uniquely identifies a tuple • Potentially should not change if the tuple changes its location • Or at least, do not change it unless we have to • Walid G. Aref
How to Store Tuples Inside a Disk Page? • Tuples of a table can be of two types: • Tuples in the table are all of the same length --> Fixed-length records • Each tuple in the table can be of a different length than the other tuples à Variable-length Records • For each type, need to state what the TID is in each case • Demonstrate how to enforce the TID properties Walid G. Aref
Disk Page Layout Containing Fixed-length Tuples Alternative 3: Attempt 1: Alternative 2: • • • Keep track of the locations of the empty slots • Will need to keep track of the locations of Will need to keep track of the locations of • • the empty slots the empty slots Add a linked list of free space tuples in the page • TID: (page id, slot id) Search for empty slot and insert new • • Know slot-id from previous pointer • tuples Can we recompact after tuple deletion? • Or can compute the offset given the fixed length • Tuples guaranteed to fit • tuples Not a good idea as TIDs will keep changing • TID: (page id, slot id) does not change • Prev. Pg. Prev. Pg. Next Pg. Next Pg. Prev. Pg. Next Pg. 0 1 1 0 1 1 1 Free Space Head of Free Space Free Space Free Space Free Space TUPLE TUPLE TUPLE TUPLE TUPLE TUPLE Free Space Free Space Free Space Linked List T3 T3 T3 of T2 T2 T2 Walid G. Aref Free Space T1 T1 T1
Disk Page Layout Containing Fixed-length Tuples: Comments • Each page can have pointers to the previous and next pages (based on chosen directory structure) • Need to search for next empty slot (linear search for bit array) • Alternative 3 is better but consumes more space • How to know that page is full? • Add field: Number of tuples in page • May need other meta data per page, e.g., for logging and concurrency control purposes Walid G. Aref
Disk Page Layout Containing Variable-length Tuples Tuple Identifier (page id, slot id) • Slot-id: An index to an in-page directory entry that • Prev. Pg. Ptr. Free Space Ptr. Next Pg. Ptr. contains offset of the tuple from the beginning of page Free Space Size If Tuple moves inside the page, slot-id does not change • but the offset value in the entry changes Records can be moved around within a page to keep • them contiguous with no empty space between them; entry in the header must be updated (Expensive – Free Space Should be avoided) In-page directory entries (Expandable array of size Num. • of Slots) ( Free Space bit , size, offset to tuple) • 0 means free space • 1 means busy • No. of Slots In-page Directory Walid G. Aref
Recommend
More recommend