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代價。

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