Ch8 file organizations & indexes(筆記+習題)

Review:

·文件組織是排列文件記錄的方式。在我們對不同文件組織的討論中,我們使用了簡單的成本模型,該模型使用磁盤頁面I / O的數量作爲成本指標。 (第8.1節)
·我們比較了3種基本文件組織(heap files, sorted files, and hashed files)的以下操作:掃描、等值查詢、範圍查詢、插入和刪除。選擇不同的文件組織有不同的性能影響。
·索引是一種能夠加速對文件的特定操作的數據結構。操作涉及搜索鍵,即一系列記錄字段(大部分情況下是一個字段)。索引的元素叫做數據條目。數據條目可以是實際數據記錄,<search-key,rid>對,或者<search-key,rid-list>對。一個數據記錄的文件可以有多個索引,每個索引有不同的搜索鍵。 (Section 8.3)
·在聚集索引中,文件中記錄的順序與索引中的數據條目相匹配。一個索引被稱爲稠密的,當且僅當每個搜索鍵至少對應一個數據條目;否則稱爲稀疏的。一個索引被稱爲主索引,當且僅當搜索鍵包含主鍵;否則稱爲輔助索引。一個包含多個字段的搜索鍵叫做合成鍵。 (Section 8.4)
·SQL-92不包括對索引結構設置的描述,所以在不同的DBMS中索引關係有不同之處。 (Section 8.5)

Exercises:

Exercise 8.1 What are the main conclusions that you can draw from the discussion of the three file organizations?
三種文件組織各有優缺點,具體使用哪一種要根據實際情況。

Exercise 8.2 Consider a delete specified using an equality condition. What is the cost if no record qualifies? What is the cost if the condition is not on a key?
對於heap files將掃描整個文件,成本是B(D+RC);
對於sorted files只需要找到第一個記錄,成本是Dlog2B+Clog2B;
對於hashed files成本是H+D。
如果沒有等值記錄,hashed files成本將變成H+D+RC。

Exercise 8.3 Which of the three basic file organizations would you choose for a file where the most frequent operations are as follows?
1. Search for records based on a range of field values.
選擇sorted files最佳。
2. Perform inserts and scans where the order of records does not matter.
選擇heap files最佳。
3. Search for a record based on a particular field value.
選擇hashed files最佳。

Exercise 8.4 Explain the difference between each of the following:
1. Primary versus secondary indexes.
主索引是按照索引字段值進行排序的一個有序文件,通常建立在有序文件的基於主碼的排序字段上。
輔助索引通常對字段(該字段非排序)的每一個不同值有一個索引項,字段值不唯一,引入中間桶保存指針列表。
主索引是稀疏索引,輔助索引是稠密索引。
一個主文件僅有一個主索引,但可以有多個輔助索引。
可以利用主索引重新組織主文件數據,輔助索引不可以。
2. Dense versus sparse indexes.
在稠密索引中,文件中的每個搜索碼值都對應一個索引值。
在稀疏索引中,只爲搜索碼的某些值建立索引項。
稠密索引比稀疏索引更快的定位一條記錄。 
稀疏索引所佔空間小,並且插入和刪除時所需維護的開銷也小。
3. Clustered versus unclustered indexes.
聚集索引:數據行的物理順序與列值(一般是主鍵的那一列)的邏輯順序相同。
非聚集索引:該索引中索引的邏輯順序與磁盤上行的物理存儲順序不同。
一個表中只能擁有一個聚集索引,但可以擁有多個非聚集索引。
If you were about to create an index on a relation, what considerations would guide your choice with respect to each pair of properties listed above?
注意事項有字段值是否唯一、空間和時間上的要求、哪種操作類型需求更大。

Exercise 8.5 Consider a relation stored as a randomly ordered file for which the only index is an unclustered index on a field called sal. If you want to retrieve all records with sal > 20,is using the index always the best alternative? Explain.
使用索引不是最好的選擇。在這個例子中,索引是非聚集的,每一個數據條目可以包含一個指向數據頁面的rid,將導致與匹配範圍查詢的數據條目一樣多的數據頁面I/O。所以使用文件掃描更佳。

Exercise 8.6 If an index contains data records as `data entries', is it clustered or unclustered? Dense or sparse?
根據定義,它是聚集的,稠密的。

Exercise 8.7 Consider Alternatives (1), (2) and (3) for `data entries' in an index, as discussed in Section 8.3.1. Are they all suitable for secondary indexes? Explain.
並不都適合。alternatives (1)的索引把實際數據記錄作爲條目。這必須有一個主索引,並且沒有重複的。它並不適合輔助索引,因爲我們不想要數據記錄的副本。

Exercise 8.8 Consider the instance of the Students relation shown in Figure 8.7, sorted by age: For the purposes of this question, assume that these tuples are stored in a sorted file in the order shown; the first tuple is in page 1, slot 1; the second tuple is in page 1, slot 2; and so on. Each page can store up to three data records. You can use hpage-id, sloti to identify a tuple.
List the data entries in each of the following indexes. If the order of entries is significant, say so and explain why. If such an index cannot be constructed, say so and explain why.


1. A dense index on age using Alternative (1).
11,12,18,19,19.條目的順序很重要,因爲條目順序與數據記錄的順序相同。
2. A dense index on age using Alternative (2).
<11,(1,1)>,<12,(1,2)>,<18,(1,3)>,<19,(2,1)>,<19,(2,2)>
3. A dense index on age using Alternative (3).
<11,(1,1)>,<12,(1,2)>,<18,(1,3)>,<19,(2,1),(2,2)>
4. A sparse index on age using Alternative (1).
11,19.
5. A sparse index on age using Alternative (2).
<11,(1,1)>,<19,(2,1)>
6. A sparse index on age using Alternative (3).
<11,(1,1)><19,(2,1),(2,2)>
7. A dense index on gpa using Alternative (1).
1.8,2.0,3.4,3.2,3.8.
8. A dense index on gpa using Alternative (2).
<1.8,(1,1)>,<2.0,(1,2)>,<3.4,(1,3)>,<3.2,(1,4)>,<3.8,(1,5)>
9. A dense index on gpa using Alternative (3).
<1.8,(1,1)>,<2.0,(1,2)>,<3.4,(1,3)>,<3.2,(1,4)>,<3.8,(1,5)>
10. A sparse index on gpa using Alternative (1).
1.8,3.8.
11. A sparse index on gpa using Alternative (2).
<1.8,(1,1)>,<3.8,(1,2)>
12. A sparse index on gpa using Alternative (3).
<1.8,(1,1)>,<3.8,(1,2)>

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