2015q4_innercourse_presentation
TRANSCRIPT
Relational Database2015,10 [email protected]
OutletIntroduction
Index
cluster index
non-cluster index
Concurrency Control
optimistic locking
Introduction
IntroductionDatabase I/O
page, 4KB, 8KB, or 16KB
db I/O Unit
Read / Write, Disk <—> Memory
I/O >>>> CPU
Index
Index
Index File
Data file
Index entries
Direct search fordata entries
Data entries
DataRecords
IndexIndex entries
B+ tree, non-leaf node, search key
Data entries
B+ tree, leaf node
Data records
record id (rid)
IndexFan-out
non-leaf node, # of children (denoted as F)
Search, (logFN + 1) I/O -> page data
In practice, F is at least 100
log100106 = 3,
3, or 4 I/O <=> 106 records
IndexData entries
K*: Search key, and other fields.
Clustered index
<K, rid>: Search key, and record id
<K, rid-list>: Search key, and record id list.
IndexCluster index vs non-cluster index
Clustered index ( Primary key)
K*
Non-clustered index
<K, rid>
<K, rid-list>
Clustered IndexIndex entries
Direct search fordata entries
Data entries
DataRecords
Non-clustered IndexIndex entries
Direct search fordata entries
Data entries
DataRecords
Index Data TypeInt, Long, Guid
Sequential vs non-sequential
Int, Long => Sequential
Guid => ?
Composite index
Direct search fordata entries
Data entries
idx_1
idx_2
Composite IndexIncrease the tree-depth
Search key
〇 idx_1 only
〇 <idx_1, idx_2>
✕ idx_2 only
The order of indexes
Concurrency Control
Optimistic lockingOptimistic vs Pessimistic
Optimistic Pessimistic
Conflict Conflict detect Conflict avoid
效率 快 慢
實作 很容易 較麻煩
Lock 無 有
考慮因素 1. 不常衝突 2. 衝突代價低
1. 常常衝突 2. 衝突代價⾼
Optimistic locking參考資料, Java Hibernate實作
概念
有個欄位為rowVersion
int vs timestamp
每次db update時,由db檢查該值,並且負責更新這個欄位
判斷db update時, rowVersion是否被別⼈修改了。
Optimistic lockingUpdating
rowVersion == original rowVersion ?
If no, reject it.
rowVersion += 1
Pessimistic lockingBuilding lock manager
Types:
exclusive write lock
exclusive read lock
read/write lock
Lock Table
Dead lock
Timeout, for a victim
參考資料Clustered vs non-clustered index
Microsoft, Clustered and Nonclustered Indexes Described
Wiki, database index