Ch7 storing data disks & files(笔记+习题)

笔记:

7.1 the memory hierarchy(内存层次)
primary storage(由cache高速缓存和main memory主存组成,提供对数据的快速访问)
secondary storage(由更慢的设备如magnetic disks磁盘组成)
Tertiary storage(是存储设备中最慢的,如optical disks光盘 and tapes磁带)
主存的速度是磁盘的100倍,磁带比磁盘更便宜
nonvolatile非易失性
主存通常是易失的,即使可能通过添加备用电池来增加非易失性
第二存储和第三存储是非易失的
磁带相对便宜,能存储巨量的数据,因此是档案存储的好选择
磁带的主要缺点是它们是顺序访问设备。 实际上,我们必须按顺序浏览所有数据,并且不能直接访问磁带上的给定位置。 例如,要访问磁带上的最后一个字节,我们必须首先遍历整个磁带。 这使磁带不适合存储操作数据或经常访问的数据。 磁带通常用于定期备份操作数据。
磁盘块是连续的字节序列,是将数据写入磁盘并从磁盘读取数据的单位。 块排列在一个或多个盘片上的同心圆环(称为轨道)中。 轨道可以记录在盘子的一个或两个表面上。 我们将盘片相应地称为单面或双面。直径相同的所有轨道的集合称为圆柱体,因为这些轨道所占据的空间的形状像圆柱体;圆柱体每个盘片表面包含一个轨道。 每个磁道分为称为扇区的弧形,其大小是磁盘的特征,无法更改。 当磁盘初始化为扇区大小的倍数时,可以设置磁盘块的大小。
access time = seek time + rotational delay + transfer time

7.2 RAID(磁盘阵列)
磁盘阵列是几个磁盘的排列,组织起来可以提高性能并提高所得存储系统的可靠性。 通过数据条带化可以提高性能。 数据条带化将数据分布在多个磁盘上,给人的印象是只有一个很大的非常快的磁盘。 通过冗余来提高可靠性。 除了保留数据的单个副本之外,还可以保留冗余信息。 冗余信息经过精心组织,以便在磁盘发生故障的情况下,可以将其用于重建发生故障的磁盘的内容。 结合了数据条带化和冗余的磁盘阵列称为独立磁盘的冗余阵列,或者简称为RAID。1已经提出了几种RAID组织,称为RAID级别。 每个RAID级别代表可靠性和性能之间的不同权衡。
data striping:数据被分割到大小相等的分区,每个分区叫做一个条单元,记作disk i mod D(D个磁盘)
redundant information:即可分布在少量的检查磁盘,也可均匀分布在所有磁盘
RAID级别0:Nonredundant(写性能最佳,空间利用率为100%,因为没有冗余信息,不需要更新冗余信息。但写性能不是最好的)
RAID级别1:Mirrored(最昂贵的方案,空间利用率为50%,在2个不同磁盘上保留2份相同的数据备份,不把数据分条)
RAID级别0+1: Striping and Mirroring(空间利用率为50%,对磁盘的读可调度到磁盘或其镜像,写则与级别1相同)
RAID级别2: Error-Correcting Codes(冗余信息使用汉明码,4个数据磁盘,3个校验磁盘,空间利用率为57%,适用于请求量大的工作,同时对请求量小的不利。写入时读出D块,修改D+C块并将D+C块写入磁盘,称为读出-修改-写入周期)
RAID级别3: Bit-Interleaved Parity(校验盘不需要包含用于标识故障磁盘的信息,只使用一个带有奇偶校验信息的校验盘,开销最低,性能与级别2相似)
RAID级别4: Block-Interleaved Parity(读请求仍可利用聚合带宽,写请求仍需要读出-修改-写入周期,但只涉及一个数据磁盘和校验盘。可以在不读取所有D个盘块的情况下更新校验盘的奇偶校验,利用旧数据块和新数据块之间的差异,将差异应用于校验磁盘上,NewParity = (OldData XOR NewData) XOR OldParity。只需要一个校验盘,空间利用率为80%)
RAID级别5: Block-Interleaved Distributed Parity(优点1:消除了唯一校验盘的瓶颈,可并行处理多个写请求。优点2:读请求有更高的并行度。性能最佳。有针对大小写请求的冗余性。小写操作仍需要读-修改-写周期,因此写效率低于级别1,空间利用率与级别3、4相同)
RAID级别6: P+Q Redundancy(动机:用来替换故障磁盘的磁盘也可能发生故障,或者恢复故障期间再次发生故障。使用Reed-Solomon代码,能从最多2个同时发生的磁盘故障中恢复。大小读取请求和大型写入请求的性能类似级别5。读-改-写过程涉及6个磁盘,空间利用率为66%)
7.2.4 Choice of RAID Levels
1、如果数据丢失不是问题,级别0在最小花费下提供了良好性能。
2、级别0+1比级别1更优,主要应用于小型存储子系统,有时应用于工作负载中具有较高写入百分比的应用程序。
3、级别2、4始终低于级别3、5。
4、级别3适用于主要由几个连续块的大型传输请求组成的工作负载,不利於单个磁盘块中有许多小请求的工作负载。
5、级别5是一个很好的通用方案,为大小型请求都提供了高性能。
6、级别6适有更高级别的可靠性。
7.3.1 Keeping Track of Free Blocks
方法1:维护自由块的列表。
方法2:为每一个磁盘维护一个位图。
7.4 BUFFER MANAGER
pid_count:当前给定的帧中页面被请求但不释放的次数,初始值为0。
dirty:布尔值,表示页面进入缓冲池以来是否被修改过,初始值为off。
向缓冲区管理器请求页面的过程:
1、检查缓冲池是否包含请求页面,增加该页面的pid_count。如果页面不在池中,操作如下:
(a)根据置换策略选择一帧做置换,增加此页面的pid_count。
(b)如果被置换的帧的dirty是on,把它写入硬盘。
(c)读置换的请求页面。
2、返回包含请求页面的帧地址给请求者。
缓冲区置换策略:LRU、Clock、MRU、FIFO、random
页面预取的好处:
1、请求页面在缓冲池中被访问。
2、读取连续块的页面比在不同时间读取相同页面要快得多。

Review:

·计算机系统中的内存分为主存储(高速缓存和主内存),辅助存储(磁盘)和三级存储(光盘和磁带)。 持久存储数据的存储设备称为非易失性。(第7.1节)
·磁盘提供廉价的非易失性存储。 从磁盘到主存储器的传输单位称为块或页面。 块被安排在几个盘子上的轨道上。 访问页面的时间取决于其在磁盘上的位置。 访问时间包括三个部分:将磁盘臂移至所需磁道的时间(查找时间),等待所需块在磁盘磁头下旋转的时间(旋转延迟)以及传输数据的时间( 传输时间)(第7.1.1节)
·仔细地将页面放置在磁盘上以利用磁盘的几何形状可以最大程度地减少顺序读取页面时的查找时间和旋转延迟。(第7.1.2节)
·磁盘阵列是连接到计算机的几个磁盘的排列。 磁盘阵列的性能可以通过数据分条来提高,而可靠性可以通过冗余来提高。 称为RAID级别的不同RAID组织代表了可靠性和性能之间的不同权衡。 (第7.2节)
·在DBMS中,磁盘空间管理器通过跟踪可用磁盘块和已用磁盘块来管理磁盘上的空间。 它还提供作为磁盘页面集合的数据的抽象。 由于性能,功能和可移植性的原因,DBMS很少使用OS文件。 (第7.3节)
·在DBMS中,所有页面请求都由客户经理集中处理。 缓冲区管理器在磁盘和主内存的特殊区域(称为缓冲池)之间传输页面,该区域被划分为称为页面的页面大小的块。 对于缓冲池中的每个页面,缓冲区管理器维护一个pin计数和一个脏标志,pin计数指示当前页面的用户数,dirty标志指示页面是否已被修改。 请求的页面将保留在缓冲池中,直到所有用户将其释放(取消固定)为止。 随后,当选择包含页面的帧进行替换时,会将页面写回到磁盘(如果已在缓冲池中对其进行了修改)。 (第7.4节)
·替换帧的选择基于缓冲区管理器的替换策略,例如LRU或clock。 如果使用LRU,则对文件的重复扫描会导致顺序泛洪。 (第7.4.1节)
·DBMS缓冲区管理器通常可以预测磁盘页面的访问模式。 通过向磁盘发出请求以一次预取几个页面,可以利用这种机会。 此技术最大程度地减少了磁盘臂的移动并减少了I / O时间。 DBMS还需要能够将页面强制到磁盘以确保崩溃恢复。 (第7.4.2节)
·数据库页面被组织为文件,更高级别的DBMS代码将数据视为记录的集合。 (第7.5节)
·最简单的文件结构是堆文件,它是无序的记录集合。堆文件被组织为数据页面的链接列表或目录页面的列表,这些目录页面引用具有数据的实际页面。 (第7.5.1节)
·页面包含插槽的集合,每个插槽都标识一条记录。 带槽的页面允许在页面上四处移动记录,而无需更改记录标识符或页ID。 插槽编号对。 对于固定长度记录(空闲插槽的位图)或可变长度记录(插槽目录),存在有效的页面组织。 (第7.6节)
·对于定长记录,可以连续存储字段,并且可以轻松计算字段的地址。 可变长度记录可以在记录的开头存储偏移量数组,也可以使用分隔符将各个字段分开。 具有偏移量数组的组织可以直接访问字段(如果记录很长并且包含很多字段,这可能很重要),并且支持空值。 (第7.7节)

Exercises:


Exercise 7.1 What is the most important difference between a disk and a tape?
磁带相对便宜,能存储巨量的数据,因此是档案存储的好选择。
磁带的主要缺点是它们是顺序访问设备。
Exercise 7.2 Explain the terms seek time, rotational delay, and transfer time.
seek time:寻道时间,移动读写头至指定磁道所用时间。
rotational delay:旋转延迟,磁盘转动所用时间。
transfer time:传输数据,传输数据所用时间。

Exercise 7.3 Both disks and main memory support direct access to any desired location(page). On average, main memory accesses are faster, of course. What is the other important difference (from the perspective of the time required to access a desired page)?
访问硬盘数据的时间是不确定的,取决于数据所在的位置。而访问主存的时间是一样的。

Exercise 7.4 If you have a large file that is frequently scanned sequentially, explain how you would store the pages in the file on a disk.
顺序存放,以加快访问速度。

Exercise 7.5 Consider a disk with a sector size of 512 bytes, 2,000 tracks per surface, 50 sectors per track, 5 double-sided platters, average seek time of 10 msec.
1. What is the capacity of a track in bytes? What is the capacity of each surface? What is the capacity of the disk?
512×50=25K。512×50×2000=50000K。512×50×2000×10=500000K。
2. How many cylinders does the disk have?
2000。
3. Give examples of valid block sizes. Is 256 bytes a valid block size? 2,048? 51,200?
2048字节大小比较合适。
4. If the disk platters rotate at 5,400 rpm (revolutions per minute), what is the maximum rotational delay?
(1/5400)×60=0.011s。
5. Assuming that one track of data can be transferred per revolution, what is the transfer rate?
25/0.011=2250 Kbytes/second。

Exercise 7.6 Consider again the disk specifications from Exercise 7.5 and suppose that a block size of 1,024 bytes is chosen. Suppose that a file containing 100,000 records of 100 bytes each is to be stored on such a disk and that no record is allowed to span two blocks.
1. How many records fit onto a block?
1024/100=10 records。
2. How many blocks are required to store the entire file? If the file is arranged sequentially on disk, how many surfaces are needed?
10000 blocks。10 surfaces。
3. How many records of 100 bytes each can be stored using this disk?
500000 records。
4. If pages are stored sequentially on disk, with page 1 on block 1 of track 1, what is the page stored on block 1 of track 1 on the next disk surface? How would your answer change if the disk were capable of reading/writing from all heads in parallel?
第26块是下一个表面的轨道1的块1。如果读写性能与之前一致,把前10个页面分别放在每个表面的轨道1的块1,那么下一个表面的轨道1的块1是第2个页面。
5. What is the time required to read a file containing 100,000 records of 100 bytes each sequentially? Again, how would your answer change if the disk were capable of reading/writing from all heads in parallel (and the data was arranged optimally)?
100000个100字节的记录需要400个轨道存储。每个轨道的数据传输时间是0.011秒,所以需要400×0.011=4.4秒来传输400个轨道的数据。需要探寻40次。探寻数据是40×0.01=0.4秒。因此总的访问时间是4.4+0.4=4.8秒。
如果硬盘读写性能与之前一致,硬盘每秒能读10个轨道数据。传输10次,时间是4.4/10=0.44秒,总的访问时间是0.44+0.4=0.84秒。
6. What is the time required to read a file containing 100,000 records of 100 bytes each in some random order? Note that in order to read a record, the block containing the record has to be fetched from disk. Assume that each block request incurs the average seek time and rotational delay.
访问时间=seektime+rotationaldelay+transfertime
seektime=10ms
rotationaldelay=6ms
transfertime=1K/(2250K/s)=0.44ms
则平均访问时间是16.44秒。对于一个有100000个100字节大小的记录的文件,总的访问时间是164.4秒。

Exercise 7.7 Explain what the buffer manager must do to process a read request for a page.What happens if the requested page is in the pool but not pinned?
缓冲池检查是否包含请求页面。如果包含,执行第2步;否则如下顺序执行:
(a)通过置换策略选择一帧来置换。
(b)如果被选中的帧是被污染的,先更新它。
(c)请求页面写入到被选中的帧。
如果页面的pin值为0,即使主存需要此页面也可能被移出。如果pin值不为0则不会被移出。

Exercise 7.8 When does a buffer manager write a page to disk?
当缓冲池选择一帧来置换,而此帧是被污染的,这时需要先把此帧写入磁盘。

Exercise 7.9 What does it mean to say that a page is pinned in the buffer pool? Who is responsible for pinning pages? Who is responsible for unpinning pages?
页面被固定在缓冲池中意味着页面不会被移出。缓冲池。页面请求器。

Exercise 7.10 When a page in the buffer pool is modified, how does the DBMS ensure that this change is propagated to disk? (Explain the role of the buffer manager as well as the modifier of the page.)
页面修改器通过页面的dirty标志来告知缓冲池是否被修改。必要时缓冲池把页面写入磁盘。

Exercise 7.11 What happens if there is a page request when all pages in the buffer pool are dirty?
缓冲池选择某一页面,写入磁盘,再置换出去。如果所有页面都在被请求,需等待。

Exercise 7.12 What is sequential flooding of the buffer pool?
一些数据库操作需要对关系进行顺序扫描。假设缓冲池中,只有10帧大小,要扫描的文件有11或更多个页面。使用LRU,每次扫描这个文件会导致读取这个文件的所有页面。这种情况叫做sequential ?ooding。

Exercise 7.13 Name an important capability of a DBMS buffer manager that is not supported by a typical operating system's buffer manager.
pin一个页面能阻止其被置换;能够将一个页面强制写入磁盘。

Exercise 7.14 Explain the term prefetching. Why is it important?
因为DBMS中大部分页面的引用方式都是已知的,所以缓冲池可以预测下一个请求的页面,在请求前预先读取页面,这叫做prefetching。
好处在于:当页面被请求时可在缓冲池访问到;读取连续块的页面比不同时候读取同一页面要更快。

Exercise 7.15 Modern disks often have their own main memory caches, typically about one MB, and use this to do prefetching of pages. The rationale for this technique is the empirical observation that if a disk page is requested by some (not necessarily database!) application,80 percent of the time the next page is requested as well. So the disk gambles by reading ahead.
1. Give a nontechnical reason that a DBMS may not want to rely on prefetching controlled by the disk.
人们希望它独立于特定的硬件支持,具备兼容性。
2. Explain the impact on the disk's cache of several queries running concurrently, each scanning a different file.
不同次查询的页面会被反复调入主存。最坏情况是一直缺页。
3. Can the above problem be addressed by the DBMS buffer manager doing its own prefetching?Explain.
可以。DBMS缓冲器自动预读页面可使请求不会起冲突。
4. Modern disks support segmented caches, with about four to six segments, each of which is used to cache pages from a different file. Does this technique help, with respect to the above problem? Given this technique, does it matter whether the DBMS buffer manager also does prefetching?
分段缓存有助于解决并行问题,但如果被访问文件数超过段数则无用。所以DBMS仍应该预读页面。

Exercise 7.16 Describe two possible record formats. What are the trade-offs between them?
定长记录格式容易实现,可以连续存储,快速存取。
不定长记录格式更加灵活。

Exercise 7.17 Describe two possible page formats. What are the trade-offs between them?
consecutive slots常用于定长记录格式,它使用位图或链表处理删除。
slot directory维护了每个页面的slot目录,每个slot对应一个<record offset,record length>对。这是一种间接获取偏移量的方式,删除变得容易,只要设置长度字段为0。记录也很容易移动,不需要改变额外的标识符。

Exercise 7.18 Consider the page format for variable-length records that uses a slot directory.
1. One approach to managing the slot directory is to use a maximum size (i.e., a maximum number of slots) and to allocate the directory array when the page is created. Discuss the pros and cons of this approach with respect to the approach discussed in the text.
方法简单,但不够灵活。记录长度可变会导致很难估量给定页面有多少记录。
2. Suggest a modification to this page format that would allow us to sort records (according to the value in some field) without moving records and without changing the record ids.
存储slot入口如<logical record number within page,offset>对。

Exercise 7.19 Consider the two internal organizations for heap files (using lists of pages and a directory of pages) discussed in the text.
1. Describe them briefly and explain the trade-offs. Which organization would you choose if records are variable in length?
链表方法比较简单,但很难高效寻找一个新纪录(特别是变长记录下)。我们必须扫描整个页面列表直到发现符合的页面。而目录方法只是简单地查看目录就能找到这样的页面,因此目录方法更佳,特别是变长记录下。
2. Can you suggest a single page format to implement both internal file organizations?
双向链表。

Exercise 7.20 Consider a list-based organization of the pages in a heap file in which two lists are maintained: a list of all pages in the file and a list of all pages with free space. In contrast, the list-based organization discussed in the text maintains a list of full pages and a list of pages with free space.
1. What are the trade-offs, if any? Is one of them clearly superior?
第一种方法(维护所有页面的列表和拥有空闲空间的页面列表)中,有空闲空间的页面属于两种列表。因此我们需要使用对每个页面,每个列表使用指针对<previous,next>。
第二种方法(维护完整的页面列表和拥有空闲空间的页面列表)中,每个页面只属于两个列表其中一个。因此每个页面有指针对<previous,next>。
两种方法很相似,第二种方法整体更优。
2. For each of these organizations, describe a page format that can be used to implement it.
<previous,next>。

Exercise 7.21 Modern disk drives store more sectors on the outer tracks than the inner tracks. Since the rotation speed is constant, the sequential data transfer rate is also higher on the outer tracks. The seek time and rotational delay are unchanged. Considering this information,explain good strategies for placing files with the following kinds of access patterns:
1. Frequent, random accesses to a small file (e.g., catalog relations).
存储在中间轨道。由于文件小,顺序访问速度不是问题。另外,seek time也小。
2. Sequential scans of a large file (e.g., selection from a relation with no index).
存储在外部轨道。顺序访问速度很重要,外部轨道的速度是最大的。
3. Random accesses to a large file via an index (e.g., selection from a relation via the index).
存储在内部轨道。DBMS将交替访问索引页面和文件页面,因此二者应紧邻放置以减少seek时间。同时,这样做节约了珍贵的外部高速轨道。
4. Sequential scans of a small file.
存储在磁盘内侧。顺序扫描的代价取决于最初文件起始处的seek代价。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章