文章目录
- Normalization & Transaction \textbf{Normalization \& Transaction} Normalization & Transaction
- 1. Normalization \textbf{1. Normalization} 1. Normalization
- 1.1. Normalization \textbf{1.1. Normalization} 1.1. Normalization定义
- 1.2. Database Anomalies \textbf{1.2. Database Anomalies} 1.2. Database Anomalies异常
- 1.3. Functional Dependencies \textbf{1.3. Functional Dependencies} 1.3. Functional Dependencies
- 1.4. Normal Form \textbf{1.4. Normal Form} 1.4. Normal Form
- 2. Transaction \textbf{2. Transaction} 2. Transaction(事务)
- 2.1. \textbf{2.1. } 2.1. 概念与概述
- 2.2. Serializability \textbf{2.2. Serializability} 2.2. Serializability
- 2.3. Concurrent Execution \textbf{2.3. Concurrent Execution} 2.3. Concurrent Execution的问题与解决
- 2.3.1. Concurrent Access Conflict \textbf{2.3.1. Concurrent Access Conflict} 2.3.1. Concurrent Access Conflict
- 2.3.2. Concurrency Control Method: \textbf{2.3.2. Concurrency Control Method:} 2.3.2. Concurrency Control Method: 解决冲突
- 2.3.2.0. \textbf{2.3.2.0. } 2.3.2.0. 概述
- 2.3.2.1. Lock Method \textbf{2.3.2.1. Lock Method} 2.3.2.1. Lock Method
- 2.3.2.2. Unresolvable Problem For Lock: Deadlock \textbf{2.3.2.2. Unresolvable Problem For Lock: Deadlock} 2.3.2.2. Unresolvable Problem For Lock: Deadlock
- 2.3.2.3. Concurrency Control Method \textbf{2.3.2.3. Concurrency Control Method} 2.3.2.3. Concurrency Control Method
- 2.4. Transaction Log \textbf{2.4. Transaction Log} 2.4. Transaction Log
更多 资料与 笔记
Normalization & Transaction \textbf{Normalization \& Transaction} Normalization & Transaction
1. Normalization \textbf{1. Normalization} 1. Normalization
1.1. Normalization \textbf{1.1. Normalization} 1.1. Normalization定义
1️⃣定义
操作 目的 Normalization \text{Normalization} Normalization 一大表格 → 拆分 \xrightarrow{拆分} 拆分若干小表格 去除 Redundancies \text{Redundancies} Redundancies从而避免 Anomoly \text{Anomoly} Anomoly Denormalization \text{Denormalization} Denormalization 一大表格 ← 拼接 \xleftarrow{拼接} 拼接若干小表格 避免频繁的 Join \text{Join} Join ⚠️ Fully Normalized = Eliminate All Anomalies \text{Fully Normalized = Eliminate All Anomalies} Fully Normalized = Eliminate All Anomalies,一般到 4NF \text{4NF} 4NF才算完全归一化
2️⃣示例
- 标准化前
StuID \textbf{StuID} StuID SName \textbf{SName} SName CID \textbf{CID} CID CName \textbf{CName} CName 1 \text{1} 1 John \text{John} John 101 \text{101} 101 Math \text{Math} Math 2 \text{2} 2 Mary \text{Mary} Mary 102 \text{102} 102 English \text{English} English 1 \text{1} 1 John \text{John} John 103 \text{103} 103 Physics \text{Physics} Physics 3 \text{3} 3 Peter \text{Peter} Peter 101 \text{101} 101 Math \text{Math} Math 2 \text{2} 2 Mary \text{Mary} Mary 103 \text{103} 103 Physics \text{Physics} Physics - 标准化后
学生表 \small\textbf{学生表} 学生表 StuID \small\textbf{StuID} StuID SName \small\textbf{SName} SName 课程表 \small\textbf{课程表} 课程表 CID \small\textbf{CID} CID CName \small\textbf{CName} CName 学生-课程表 \small\textbf{学生-课程表} 学生-课程表 StuID \small\textbf{StuID} StuID CID \small\textbf{CID} CID / \text{/} / 1 \text{1} 1 John \small\text{John} John / \text{/} / 101 \text{101} 101 Math \small\text{Math} Math / \text{/} / 1 \text{1} 1 101 \text{101} 101 / \text{/} / 2 \text{2} 2 Mary \small\text{Mary} Mary / \text{/} / 102 \text{102} 102 English \small\text{English} English / \text{/} / 2 \text{2} 2 102 \text{102} 102 / \text{/} / 1 \text{1} 1 John \small\text{John} John / \text{/} / 103 \text{103} 103 Physics \small\text{Physics} Physics / \text{/} / 1 \text{1} 1 103 \text{103} 103 / \text{/} / 3 \text{3} 3 Peter \small\text{Peter} Peter / \text{/} / 101 \text{101} 101 Math \small\text{Math} Math / \text{/} / 3 \text{3} 3 101 \text{101} 101 / \text{/} / 2 \text{2} 2 Mary \small\text{Mary} Mary / \text{/} / 103 \text{103} 103 Physics \small\text{Physics} Physics / \text{/} / 2 \text{2} 2 103 \text{103} 103 3️⃣ Normalization/Denormalization \text{Normalization/Denormalization} Normalization/Denormalization对比
操作 Normalization \textbf{Normalization} Normalization,比如 A, B \textbf{A, B} A, B Denormalization \textbf{Denormalization} Denormalization,比如 A ⋈ B \textbf{A}\bowtie{}\textbf{B} A⋈B 查询一个表 快 慢 查询两个表 慢 快 插入/修改/删除 容易 困难 1.2. Database Anomalies \textbf{1.2. Database Anomalies} 1.2. Database Anomalies异常
1️⃣概述:期末会给你表格,让你用文字描述举例三种 Anomaly \text{Anomaly} Anomaly
- 示例表格
Order(PFK) \textbf{Order(PFK)} Order(PFK) Item(PFK) \textbf{Item(PFK)} Item(PFK) Desc \textbf{Desc} Desc Qty \textbf{Qty} Qty 27 \text{27} 27 873 \text{873} 873 nut \text{nut} nut 2 \text{2} 2 28 \text{28} 28 873 \text{873} 873 nut \text{nut} nut 10 \text{10} 10 30 \text{30} 30 495 \text{495} 495 washer \text{washer} washer 50 \text{50} 50 - 基本概念
Anomaly \textbf{Anomaly} Anomaly 含义 示例(下表) Insertion \text{Insertion} Insertion 插入数据 → \text{→} →插入其他数据 插入 Item/Desc → 还需 \text{Item/Desc}\xrightarrow{还需} Item/Desc还需插入 Order/Qty \text{Order/Qty} Order/Qty Deletion \text{Deletion} Deletion 删除数据 → \text{→} →删除其他数据 删除 Order30 → 引起 \text{Order30}\xrightarrow{引起} Order30引起整行删除 Update \text{Update} Update 更新数据 → \text{→} →更新其他数据 更新 Item873 \text{Item873} Item873的 Desc → 有两处Item873 引起 \text{Desc}\xrightarrow[有两处\text{Item873}]{引起} Desc引起有两处Item873两处 Desc \text{Desc} Desc更新 2️⃣用 NULL \text{NULL} NULL来解决 Anomaly: \text{Anomaly:} Anomaly: 不能根本解决
- Insertion Anomaly \text{Insertion Anomaly} Insertion Anomaly可能可以,原因在于 PK \text{PK} PK是不能 NULL \text{NULL} NULL的
- 可以的示例:插入 Order/Item/Desc → Qty \text{Order/Item/Desc}\to{}\text{Qty} Order/Item/Desc→Qty为 NULL \text{NULL} NULL
- 不可以的示例:插入 Item/Desc → Qty \text{Item/Desc}\to{}\text{Qty} Item/Desc→Qty为 NULL/Order \text{NULL/}\text{Order} NULL/Order不能为 NULL \text{NULL} NULL
- Deletion Anomaly: \text{Deletion Anomaly:} Deletion Anomaly: 同理可能可以,当删除的数据不是 PK \text{PK} PK时,可考虑设为 NULL \text{NULL} NULL
- Update Anomaly: \text{Update Anomaly:} Update Anomaly: 绝对没戏,不可能更新成 NULL \text{NULL} NULL
1.3. Functional Dependencies \textbf{1.3. Functional Dependencies} 1.3. Functional Dependencies
1️⃣概述
- 定义: A set of attributes X determines another set of attributes Y uniquely \text{A set of attributes X determines another set of attributes Y uniquely} A set of attributes X determines another set of attributes Y uniquely
- 符号: X → Y \text{X}\to{}\text{Y} X→Y表示如果知道 X \text{X} X就知道 Y \text{Y} Y,比如 Stu-Name → Stu-FirstName \text{Stu-Name}\to{}\text{Stu-FirstName} Stu-Name→Stu-FirstName
- 性质:
- 依赖恒等式: X → X \text{X}\to{}\text{X} X→X , PK → All Column \text{PK}\to{}\text{All Column} PK→All Column
- 拆分原则 : AB → CD { 可以拆分为: AB → C / AB → D 不可拆分为: A → CD / B → CD → 只有右侧可拆分 \text{: AB}\to{}\text{CD}\begin{cases}可以拆分为\text{: AB}\to{}\text{C / }\text{AB}\to{}\text{D}\\\\不可拆分为\text{: A}\to{}\text{CD / }\text{B}\to{}\text{CD}\end{cases}\to{}只有右侧可拆分 : AB→CD⎩ ⎨ ⎧可以拆分为: AB→C / AB→D不可拆分为: A→CD / B→CD→只有右侧可拆分
2️⃣有关概念:以 A ( X ‾ , Y ‾ , Z , D ) \text{A}(\underline{\text{X}}, \underline{\text{Y}}, \text{Z}, \text{D}) A(X,Y,Z,D)为例
- Determinants: \text{Determinants:} Determinants:
- 箭头左侧的属性,比如 X ‾ , Y ‾ → Z \underline{\text{X}} ,\underline{\text{Y}}\to{}\text{Z} X,Y→Z中的 XY \text{XY} XY
- 至于这其中 Z \text{Z} Z叫什么就无具体说法
- Key/Non-Key \text{Key/Non-Key} Key/Non-Key属性:包含/不包含 PK \text{PK} PK的属性,比如
属性类型 含义 示例 Key \text{Key} Key属性 包含 PK \text{PK} PK的属性,或者说是 PK \text{PK} PK的一部分 X ‾ , Y ‾ \underline{\text{X}} ,\underline{\text{Y}} X,Y Non-Key \text{Non-Key} Non-Key属性 其余属性 Z , D \text{Z} ,\text{D} Z,D - 两种 Dependency \text{Dependency} Dependency
依赖类型 含义 示例 Partial Functional Dependency \text{Partial Functional Dependency} Partial Functional Dependency Key \text{Key} Key属性( PK \text{PK} PK的一部分) → 决定 Non-Key \xrightarrow{决定}\text{Non-Key} 决定Non-Key属性 Y ‾ → Z \underline{\text{Y}} \to{}\text{Z} Y→Z Transitive Dependency \text{Transitive Dependency} Transitive Dependency(传递性) Non-Key \text{Non-Key} Non-Key属性 → 决定 Non-Key \xrightarrow{决定}\text{Non-Key} 决定Non-Key属性 Z → D \text{Z} \to{}\text{D} Z→D 3️⃣ Armstrong’s Axioms \text{Armstrong’s Axioms} Armstrong’s Axioms(公理)
Axioms \textbf{Axioms} Axioms 条件 ⇒ \Rightarrow ⇒ 结果 备注 Reflexivity \text{Reflexivity} Reflexivity B ⊆ A \text{B}\subseteq\text{A} B⊆A ⇒ \Rightarrow ⇒ A → B \text{A} \rightarrow \text{B} A→B 或者说 AB → A \text{A}\text{B}\to{}\text{A} AB→A Augmentation \text{Augmentation} Augmentation(扩展) A → B \text{A}\rightarrow\text{B} A→B ⇒ \Rightarrow ⇒ AC → BC \text{AC} \rightarrow \text{BC} AC→BC N/A \text{N/A} N/A Transitive \text{Transitive} Transitive A → B ∧ B → C \text{A} \rightarrow \text{B }\land{}\text{ B}\rightarrow \text{C} A→B ∧ B→C ⇒ \Rightarrow ⇒ A → C \text{A} \rightarrow \text{C} A→C N/A \text{N/A} N/A 1.4. Normal Form \textbf{1.4. Normal Form} 1.4. Normal Form
0️⃣ Overview \text{Overview} Overview
Normal Form \textbf{Normal Form} Normal Form Description \textbf{Description} Description 0NF \text{0NF} 0NF 混沌之物 (Non-Relational) \text{ (Non-Relational)} (Non-Relational) 1NF \text{1NF} 1NF 无 Repeating Groups \text{Repeating Groups} Repeating Groups 2NF \text{2NF} 2NF 无 Repeating Groups+Partial Dependency \text{Repeating Groups+}\text{Partial Dependency} Repeating Groups+Partial Dependency 3NF \text{3NF} 3NF 无 Repeating Groups+Partial Dependency+Transitive Dependency \text{Repeating Groups+}\text{Partial Dependency+}\text{Transitive Dependency} Repeating Groups+Partial Dependency+Transitive Dependency 1️⃣ 1NF \text{1NF} 1NF
- 1NF \text{1NF} 1NF 的基本特征, ( \text{(} (不满足以下特征的就是 Non-Relational Model) \text{Non-Relational Model)} Non-Relational Model)
- 不能有重复的列
- 不能有 Repeating Groups \text{Repeating Groups} Repeating Groups,即表中一个单元( Fill \text{Fill} Fill)只能有一个数据
- (不同单元的数据用逗号隔开,但注意有些数据单元内本就有逗号如 Address \text{Address} Address)
- Non-Relational Model→1NF: \text{Non-Relational Model→1NF:} Non-Relational Model→1NF:
- 将 Repeating Groups \text{Repeating Groups} Repeating Groups从原表中拆分成新表
- 将原表的 PK { 一份保留在原关系 → 作PK 一份复制到新关系 → 作PFK \text{PK}\begin{cases}一份保留在原关系\to{}作\text{PK}\\\\一份复制到新关系\to{}作\text{PFK}\end{cases} PK⎩ ⎨ ⎧一份保留在原关系→作PK一份复制到新关系→作PFK
- 示例:注意区分 PK ‾ \underline{\text{PK}} PK, FK ‾ \overline{{\text{FK}}} FK, PFK ‾ ‾ \overline{\underline{\text{PFK}}} PFK的表示方法
Order-Item( Order ‾ , Cust, ( Item ‾ , Desc, Qty))→ { Order( Order ‾ , Cust) Order-Item( Order ‾ ‾ , Item ‾ , Desc, Qty) \text{Order-Item(}\underline{\color{red}{\text{Order}}}\text{, Cust, (}\underline{\color{green}{\text{Item}}}\text{, Desc, Qty))}\text{→}\begin{cases} \text{Order(}{\underline{\color{red}{\text{Order}}}}\text{, Cust)} \\\\ \text{Order-Item(}\overline{\underline{\color{red}{\text{Order}}}}\text{, }\underline{\color{green}{\text{Item}}}\text{, Desc, Qty)} \end{cases} Order-Item(Order, Cust, (Item, Desc, Qty))→⎩ ⎨ ⎧Order(Order, Cust)Order-Item(Order, Item, Desc, Qty)2️⃣ 2NF \text{2NF} 2NF
- 2NF \text{2NF} 2NF基本特征: Part of Composite Key → ❌ Identify Non-key Attribut (NO Partial 依赖) \text{Part of Composite Key}\xrightarrow[❌]{\text{Identify}}\text{Non-key Attribut (NO Partial 依赖)} Part of Composite KeyIdentify❌Non-key Attribut (NO Partial 依赖)
- 1NF→2NF: \text{1NF→2NF:} 1NF→2NF: 移除 Partial Dependencies \text{Partial Dependencies} Partial Dependencies
- 把 Partial Dependencies \text{Partial Dependencies} Partial Dependencies从原关系中剥离出来
- Partial Dependencies \text{Partial Dependencies} Partial Dependencies的 Determinant { 一份保留在原关系 → 作PFK 一份剥离到新关系 → 作PK \text{Determinant}\begin{cases}一份保留在原关系\to{}作\text{PFK}\\\\一份剥离到新关系\to{}作\text{PK}\end{cases} Determinant⎩ ⎨ ⎧一份保留在原关系→作PFK一份剥离到新关系→作PK
- 示例:注意区分 PK ‾ \underline{\text{PK}} PK, FK ‾ \overline{{\text{FK}}} FK, PFK ‾ ‾ \overline{\underline{\text{PFK}}} PFK的表示方法
- 假设Order-Item( Order ‾ ‾ , Item ‾ , Desc, Qty))存在 { Order ‾ ‾ , Item ‾ → Qty Item ‾ → Desc (Partial Dependency) 假设\text{Order-Item(}\overline{\underline{\text{Order}}}\text{, }\underline{\text{Item}}\text{, Desc, Qty))存在}\begin{cases}\overline{\underline{\text{Order}}},\underline{\text{Item}}\to{}\text{Qty}\\\\\underline{\text{Item}}\to{}\text{Desc (Partial Dependency)}\end{cases} 假设Order-Item(Order, Item, Desc, Qty))存在⎩ ⎨ ⎧Order,Item→QtyItem→Desc (Partial Dependency)
- Order-Item→ { Order( Order ‾ , Cust) Order-Item( Order ‾ ‾ , Item ‾ , Desc, Qty))→ { Item( Item ‾ , Desc) Order-Item( Order ‾ ‾ , Item ‾ ‾ , Qty) \text{Order-Item}\text{→} \begin{cases} \text{Order(}\underline{\color{red}{\text{Order}}}\text{, Cust)} \\\\ \text{Order-Item(}\overline{\underline{\color{red}{\text{Order}}}}\text{, }\underline{\color{green}{\text{Item}}}\text{, Desc, Qty))→} \begin{cases} \text{Item(}\underline{\color{green}{\text{Item}}}\text{, Desc)} \\\\ \text{Order-Item(}\overline{\underline{\color{red}{\text{Order}}}}\text{, }\overline{\underline{\color{green}{\text{Item}}}}\text{, Qty)} \end{cases} \end{cases} Order-Item→⎩ ⎨ ⎧Order(Order, Cust)Order-Item(Order, Item, Desc, Qty))→⎩ ⎨ ⎧Item(Item, Desc)Order-Item(Order, Item, Qty)
3️⃣ 3NF \text{3NF} 3NF
- 3NF \text{3NF} 3NF基本特征: Non-key Attribut → ❌ Identify Non-key Attribut (NO Transitive 依赖) \text{Non-key Attribut}\xrightarrow[❌]{\text{Identify}}\text{Non-key Attribut (NO Transitive 依赖)} Non-key AttributIdentify❌Non-key Attribut (NO Transitive 依赖)
- 2NF→3NF: \text{2NF→3NF:} 2NF→3NF: 移除 Transitive Dependencies \text{Transitive Dependencies} Transitive Dependencies
- 把 Transitive Dependency \text{Transitive Dependency} Transitive Dependency从原关系剥离出来
- Transitive Dependency \text{Transitive Dependency} Transitive Dependency的 Determinant { 一份保留在原关系 → 作FK 一份剥离到新关系 → 作PK \text{Determinant}\begin{cases}一份保留在原关系\to{}作\text{FK}\\\\一份剥离到新关系\to{}作\text{PK}\end{cases} Determinant⎩ ⎨ ⎧一份保留在原关系→作FK一份剥离到新关系→作PK
- 示例:注意区分 PK ‾ \underline{\text{PK}} PK, FK ‾ \overline{{\text{FK}}} FK, PFK ‾ ‾ \overline{\underline{\text{PFK}}} PFK的表示方法
- 假设 { Empt ‾ → Ename, Dept Dept → Dname (Transitive Dependency) \begin{cases}\underline{\text{Empt}}\to{}\text{Ename, Dept}\\\\{\text{Dept}}\to{}\text{Dname (Transitive Dependency)}\end{cases} ⎩ ⎨ ⎧Empt→Ename, DeptDept→Dname (Transitive Dependency)
- Emp( Emp ‾ , Ename, Dept, Dname) → { Emp( Emp ‾ , Ename, Dept ) ‾ Dept( Dept ‾ , Dname) \text{Emp(}\underline{\color{red}{\text{Emp}}}\text{, Ename, Dept, Dname)}\to{} \begin{cases} \text{Emp(}\underline{\color{red}{\text{Emp}}}\text{, Ename, }\overline{\color{green}{\text{Dept}})} \\\\ \text{Dept(}\underline{\color{green}{\text{Dept}}}\text{, Dname)} \end{cases} Emp(Emp, Ename, Dept, Dname)→⎩ ⎨ ⎧Emp(Emp, Ename, Dept)Dept(Dept, Dname)
4️⃣其他级别的 NF \text{NF} NF(课外,可不看)
- BCNF: \text{BCNF:} BCNF: Non-key Attribut → ❌ Identify Part of Composite Key \text{Non-key Attribut}\xrightarrow[❌]{\text{Identify}}\text{Part of Composite Key} Non-key AttributIdentify❌Part of Composite Key
- 4NF: \text{4NF:} 4NF:解决 Multivalue Dependency \text{Multivalue Dependency} Multivalue Dependency
- 多值依赖:比如若下列 { M → ✔ ® S M → ✔ ® C C ↔ ❌ S → \begin{cases}\text{M}\xrightarrow{✔️}\text{S}\\\text{M}\xrightarrow{✔️}\text{C}\\\text{C}\xleftrightarrow{❌}\text{S}\end{cases}\to ⎩ ⎨ ⎧M✔R◯SM✔R◯CC❌ S→则存在多值依赖,且记为 { M→→S M→→C \begin{cases}\text{M→→S}\\\\\text{M→→C}\end{cases} ⎩ ⎨ ⎧M→→SM→→C
M \textbf{M} M S \textbf{S} S C \textbf{C} C M1 \text{M1} M1 S1 \text{S1} S1 C1 \text{C1} C1 M1 \text{M1} M1 S1 \text{S1} S1 C2 \text{C2} C2 M1 \text{M1} M1 S2 \text{S2} S2 C1 \text{C1} C1 - 当关系达到 4NF \text{4NF} 4NF时就认为完成 Normalization \text{Normalization} Normalization了,已经没有 Duplicates \text{Duplicates} Duplicates了
- 5NF: \text{5NF:} 5NF:解决 Loss Dependency \text{Loss Dependency} Loss Dependency
- 若表格拆开又拼回去不改变原表格( Lossless-Join \text{Lossless-Join} Lossless-Join分解),则表格就有 Loss Dependency \text{Loss Dependency} Loss Dependency
- 但实际上不会如此吹毛求疵了
2. Transaction \textbf{2. Transaction} 2. Transaction(事务)
2.1. \textbf{2.1. } 2.1. 概念与概述
1️⃣ Tansaction \text{Tansaction} Tansaction定义:
- 定义:数据库操作序列( A logical unit of work \text{A logical unit of work} A logical unit of work)
- 根本特性:要么一次性执行完 Entirely Completed \text{Entirely Completed} Entirely Completed要么 Aborted \text{Aborted} Aborted,是不可分割的工作单位
状态 操作 COMMIT \text{COMMIT} COMMIT 事务正常结束,完成所有操作(读取数据,更新写回硬盘) ROLL BACK \text{ROLL BACK} ROLL BACK 事务异常结束,中途不再继续运行,已完成的操作也全部撤回 2️⃣ Tansaction \text{Tansaction} Tansaction引入的目的
- 定义 Unit of Work \text{Unit of Work} Unit of Work,适用于一个/多个用户
- Concurrent Access \text{Concurrent Access} Concurrent Access,允许多个 Command \text{Command} Command交织执行,适用于多个用户
3️⃣ Tansaction \text{Tansaction} Tansaction种类
- Implicit Transaction: \text{Implicit Transaction:} Implicit Transaction: 单独一句 DML/DDL \text{DML/DDL} DML/DDL就是 Transaction \text{Transaction} Transaction
SELECT / INSERT / DELETE / CREATE / ALTER / DROP -- 比如一个SELECT就是一个事务
- User-Defined Transaction: \text{User-Defined Transaction:} User-Defined Transaction: 需要关键字
BEGIN/COMMIMT/RILLBACK
-- 从SATRT到COMMIT不论中间多少语句,都只算一个Transaction START TRANSACTION; -- 开始事务,也可以用BEGIN TRANSACTION -- SQL 语句1 -- SQL 语句2 COMMIT; -- 提交事务 ROLLBACK; -- 或者如果发生错误,回滚事务
4️⃣ Transaction \text{Transaction} Transaction性质 : ACID \text{: ACID} : ACID
性质 描述 Atomicity \text{Atomicity} Atomicity Transaction \text{Transaction} Transaction是不可分割的逻辑工作单位(执行完 Or \text{Or} Or回退) Consistancy \text{Consistancy} Consistancy Transaction \text{Transaction} Transaction执行前后数据库都是一致状态 + \text{+} +多用户读取数据时应看到相同值 Isolation \text{Isolation} Isolation 运行结束前一个 Transaction \text{Transaction} Transaction的改变对其他 Transaction \text{Transaction} Transaction不可见 Durability \text{Durability} Durability 一旦 Transaction \text{Transaction} Transaction执行完其对数据的改变永久有效(即使系统崩溃) 2.2. Serializability \textbf{2.2. Serializability} 2.2. Serializability
1️⃣ Transaction \text{Transaction} Transaction的交叉执行( Interleaved \text{Interleaved} Interleaved):注意 Transaction → 缩写 TXN \text{Transaction}\xrightarrow{缩写}\text{TXN} Transaction缩写TXN
![]()
Execution \textbf{Execution} Execution种类 含义 备注 Concurrent/Interleaved \text{Concurrent/Interleaved} Concurrent/Interleaved 不同 TXN \text{TXN} TXN交叉并行执行 实际 TXN \text{TXN} TXN执行方式 Serial Execution \text{Serial Execution} Serial Execution 不同 TXN \text{TXN} TXN一个个执行 最安全但最低效的执行方式 2️⃣示例
- Serial Execution \text{Serial Execution} Serial Execution的读写操作
TXN \textbf{TXN} TXN Stage1 \textbf{Stage1} Stage1 Stage2 \textbf{Stage2} Stage2 Stage3 \textbf{Stage3} Stage3 Stage4 \textbf{Stage4} Stage4 TXN1 \text{TXN1} TXN1 R(A) \text{R(A)} R(A) W(A) \text{W(A)} W(A) / \text{/} / / \text{/} / TXN2 \text{TXN2} TXN2 / \text{/} / / \text{/} / R(B) \text{R(B)} R(B) W(B) \text{W(B)} W(B) - Concurrent Execution \text{Concurrent Execution} Concurrent Execution的读写操作:黄标为 Concurrent Execution \text{Concurrent Execution} Concurrent Execution部分
TXN \textbf{TXN} TXN Stage1 \textbf{Stage1} Stage1 Stage2 \textbf{Stage2} Stage2 Stage3 \textbf{Stage3} Stage3 Stage4 \textbf{Stage4} Stage4 TXN1 \text{TXN1} TXN1 R(A) \text{R(A)} R(A) R(A) \text{R(A)} R(A) W(A) \text{W(A)} W(A) W(A) \text{W(A)} W(A) TXN2 \text{TXN2} TXN2 / \text{/} / R(B) \text{R(B)} R(B) / \text{/} / W(B) \text{W(B)} W(B) 3️⃣ Serializable \text{Serializable} Serializable
- 含义:多个 TXN \text{TXN} TXN是 Serializable ⇔ \text{Serializable}\xLeftrightarrow{\text{ }\text{ }\text{ }\text{ }\text{ }\text{ }\text{ }\text{ }\text{ }} Serializable (多个 TXN \text{TXN} TXN并行执行效果 $ \equiv\text{Serial Execution}$执行效果)
- 上例中:若 AB \text{AB} AB的读写互不干扰,则两表最终结果一样,则 TXN1/2 \text{TXN1/2} TXN1/2是 Serializable \text{Serializable} Serializable
2.3. Concurrent Execution \textbf{2.3. Concurrent Execution} 2.3. Concurrent Execution的问题与解决
2.3.1. Concurrent Access Conflict \textbf{2.3.1. Concurrent Access Conflict} 2.3.1. Concurrent Access Conflict
🤔出现问题的根本原因:多 TXN \text{TXN} TXN操作同一 Object \text{Object} Object,并且其中至少一个操作是 Write \text{Write} Write
1️⃣ Loss Update Problem: \text{Loss Update Problem:} Loss Update Problem: 多次针对一个 Object \text{Object} Object写, Object \text{Object} Object为最后一次写的内容
TXN \textbf{TXN} TXN Stage1 \textbf{Stage1} Stage1 Stage2 \textbf{Stage2} Stage2 Stage3 \textbf{Stage3} Stage3 Stage4 \textbf{Stage4} Stage4 Stage5 \textbf{Stage5} Stage5 Bob \text{Bob} Bob R(A) \text{R(A)} R(A) / \text{/} / W(A) \text{W(A)} W(A) / \text{/} / COMMIT \text{COMMIT} COMMIT Alice \text{Alice} Alice / \text{/} / R(A) \text{R(A)} R(A) / \text{/} / W(A) \text{W(A)} W(A) COMMIT \text{COMMIT} COMMIT
- 最后结果是 Alice \text{Alice} Alice写入内容
2️⃣ Uncommitted Data/Dirty Read Problem: \text{Uncommitted Data/Dirty Read Problem:} Uncommitted Data/Dirty Read Problem: 某 TXN \text{TXN} TXN回退导致未交付数据被后面 TXN \text{TXN} TXN使用
TXN \textbf{TXN} TXN Stage1 \textbf{Stage1} Stage1 Stage2 \textbf{Stage2} Stage2 Stage3 \textbf{Stage3} Stage3 Stage4 \textbf{Stage4} Stage4 Stage5 \textbf{Stage5} Stage5 Bob \text{Bob} Bob R(A) \text{R(A)} R(A) W(A) \text{W(A)} W(A) / \text{/} / / \text{/} / ROLL BACK \text{ROLL BACK} ROLL BACK Alice \text{Alice} Alice / \text{/} / / \text{/} / R(A) \text{R(A)} R(A) W(A) \text{W(A)} W(A) COMMIT \text{COMMIT} COMMIT
- W(A) \text{W(A)} W(A)数据并未 COMMIT \text{COMMIT} COMMIT但却被 Alice \text{Alice} Alice使用
3️⃣ Inconsistent Retrieval Problem: \text{Inconsistent Retrieval Problem:} Inconsistent Retrieval Problem: 一个 TXN \text{TXN} TXN在聚合函数操作 + \text{+} +另一个 TXN \text{TXN} TXN在 Update \text{Update} Update数据
![]()
- 理论上,可以合理安排 Interleaved \text{Interleaved} Interleaved来避免冲突
2.3.2. Concurrency Control Method: \textbf{2.3.2. Concurrency Control Method:} 2.3.2. Concurrency Control Method: 解决冲突
2.3.2.0. \textbf{2.3.2.0. } 2.3.2.0. 概述
1️⃣总体思路:合理安排读写操作的顺序
2️⃣基本思路
基本思路 操作 备注 Pesimistic \text{Pesimistic} Pesimistic 先检查有无问题,以选择不执行/更正后执行 比如 Lock \text{Lock} Lock Optimistic \text{Optimistic} Optimistic 先一股脑执行,执行完后有问题再回来更正 此课程不涉及
- Optimistic \text{Optimistic} Optimistic方法: Timestamping \text{Timestamping} Timestamping和 Optimistic Concurrency Control \text{Optimistic Concurrency Control} Optimistic Concurrency Control
2.3.2.1. Lock Method \textbf{2.3.2.1. Lock Method} 2.3.2.1. Lock Method
1️⃣总体思路:
- 逻辑上:通过让 TXN \text{TXN} TXN持有 Lock \text{Lock} Lock,让当前 TXN \text{TXN} TXN霸占某个数据项( Exclusive Use of Data \text{Exclusive Use of Data} Exclusive Use of Data)
- 物理上:在 RAM \text{RAM} RAM上设置 Lock Manager \text{Lock Manager} Lock Manager来管理 Lock \text{Lock} Lock的数据
2️⃣ Level/Gradularity \text{Level/Gradularity} Level/Gradularity(精细度) of Lock \text{of Lock} of Lock
Gradularity \textbf{Gradularity} Gradularity 含义 使用频率 Database-Level \text{Database-Level} Database-Level 锁住整个数据库 Slightly Rare \text{Slightly Rare} Slightly Rare Table-Level \text{Table-Level} Table-Level 锁住一张表格 Very Common \text{Very Common} Very Common Page-Level \text{Page-Level} Page-Level 锁住一页(一张表存储在多页中) Common \text{Common} Common Row-Level \text{Row-Level} Row-Level 锁住表中的一行 Most Common \text{Most Common} Most Common Field-Level \text{Field-Level} Field-Level 锁住表中的某个值 Rare \text{Rare} Rare
- 理想情况 Field-Level \text{Field-Level} Field-Level最好,但其 Lock \text{Lock} Lock过于复杂( High Overhead \text{High Overhead} High Overhead),反倒最不常用
- 在 MySQL \text{MySQL} MySQL中,如果多层级 Lock \text{Lock} Lock被应用,则需要 Intention Lock \text{Intention Lock} Intention Lock来辅助
3️⃣ Types of Lock \text{Types of Lock} Types of Lock
- Binary Lock: \text{Binary Lock:} Binary Lock: 仅 Lock/Unlock \text{Lock/Unlock} Lock/Unlock两种状态,只要用到数据(不论读写)都是 Lock \text{Lock} Lock
- Shared and Exclusive Locks(Read and Write Locks) \text{Shared and Exclusive Locks(Read and Write Locks)} Shared and Exclusive Locks(Read and Write Locks)
- 描述
Type \textbf{Type} Type 适用情况 要求 Share/Read Lock \text{Share/Read Lock} Share/Read Lock Read Table Only \text{Read Table Only} Read Table Only 无 Write Lock \text{Write Lock} Write Lock Exclusive/Write Lock \text{Exclusive/Write Lock} Exclusive/Write Lock Update(Write) Records in Table \text{Update(Write) Records in Table} Update(Write) Records in Table 无其他 Lock \text{Lock} Lock - 要求表,这张表很重要
操作 已有 S-Lock \textbf{S-Lock} S-Lock 已有 X-Lock \textbf{X-Lock} X-Lock 申请新的 S-Lock \text{S-Lock} S-Lock ✔️ ❌ 申请新的 X-Lock \text{X-Lock} X-Lock ❌ ❌ 4️⃣上锁与解锁 Two Phase Policy \text{Two Phase Policy} Two Phase Policy
- Two Phase Policy: \text{Two Phase Policy:} Two Phase Policy:
Phase \textbf{Phase} Phase 描述 Grow Phase \text{Grow Phase} Grow Phase 给 Object \text{Object} Object一个个上锁 Shrink Phase \text{Shrink Phase} Shrink Phase 给 Object \text{Object} Object一个个解锁,当解锁开始时就不能再上锁了 - Strict Two Phase Policy: \text{Strict Two Phase Policy:} Strict Two Phase Policy: 相比于非 Strict \text{Strict} Strict有如下改变
- 只有在 Transaction COMMIT \text{Transaction COMMIT} Transaction COMMIT后才能解锁
- 解锁不是一个个解锁,而是一次性全部解锁
2.3.2.2. Unresolvable Problem For Lock: Deadlock \textbf{2.3.2.2. Unresolvable Problem For Lock: Deadlock} 2.3.2.2. Unresolvable Problem For Lock: Deadlock
1️⃣基本思路
基本思路 操作 备注 Pesimistic \text{Pesimistic} Pesimistic 执行前先检查是否死锁,如果死锁就不执行/更正后执行 N/A \text{N/A} N/A Optimistic \text{Optimistic} Optimistic 先一股脑执行,每隔一段时间检测是否锁死并加以解决 此处不涉及 2️⃣死锁 Detection \text{Detection} Detection的方法: Wait-For Graph \text{Wait-For Graph} Wait-For Graph
- 用圈代表 TXN \text{TXN} TXN,用箭头表示等待( A → B \text{A}\to{}\text{B} A→B表示 A \text{A} A在等待 B \text{B} B释放某个 Lock \text{Lock} Lock)
- 当 Graph \text{Graph} Graph构成闭环 → \to{} →死锁,但注意成环不代表闭环,闭环是要形成环路(下图示例)
3️⃣死锁检测例题 1: \text{1:} 1: 序列 1 \text{1} 1如下, C=COMMIT \text{C=COMMIT} C=COMMIT,其次不必在意一个 Object \text{Object} Object可以不读就写
序列: TXN \textbf{TXN} TXN 1 \textbf{1} 1 2 \textbf{2} 2 3 \textbf{3} 3 4 \textbf{4} 4 5 \textbf{5} 5 6 \textbf{6} 6 7 \textbf{7} 7 8 \textbf{8} 8 9 \textbf{9} 9 序列 1: T1 \text{1: T1} 1: T1 R(Z) \small\text{R(Z)} R(Z) / \text{/} / / \text{/} / / \text{/} / W(Y) \small\text{W(Y)} W(Y) C \small\text{C} C / \text{/} / / \text{/} / / \text{/} / 序列 1: T2 \text{1: T2} 1: T2 / \text{/} / W(X) \small\text{W(X)} W(X) W(Y) \small\text{W(Y)} W(Y) / \text{/} / / \text{/} / / \text{/} / C \small\text{C} C / \text{/} / / \text{/} / 序列 1: T3 \text{1: T3} 1: T3 / \text{/} / / \text{/} / / \text{/} / W(Y) \small\text{W(Y)} W(Y) / \text{/} / / \text{/} / / \text{/} / C \small\text{C} C / \text{/} /
- Lock \text{Lock} Lock请求时序: T 1 , 2 , 3 \text{T}_{1,2,3} T1,2,3对 X-Lock(Y) \text{X-Lock(Y)} X-Lock(Y)的请求构成等待
T1 \textbf{T1} T1 T2 \textbf{T2} T2 T3 \textbf{T3} T3 R(Z) → 获得 S-Lock(Z) \text{R(Z)}\xrightarrow{获得}\text{S-Lock(Z)} R(Z)获得S-Lock(Z) / \text{/} / / \text{/} / / \text{/} / W(X) → 获得 X-Lock(X) \text{W(X)}\xrightarrow{获得}\text{X-Lock(X)} W(X)获得X-Lock(X) / \text{/} / / \text{/} / W(Y) → 获得 X-Lock(Y) \text{W(Y)}\xrightarrow{获得}\text{X-Lock(Y)} W(Y)获得X-Lock(Y) / \text{/} / / \text{/} / / \text{/} / W(Y) → T 2 释放 等待 X-Lock(Y) \text{W(Y)}\xrightarrow[\text{T}_{2}释放]{等待}\text{X-Lock(Y)} W(Y)等待T2释放X-Lock(Y) W(Y) → T 2 , 3 释放 等待 X-Lock(Y) \text{W(Y)}\xrightarrow[\text{T}_{2,3}释放]{等待}\text{X-Lock(Y)} W(Y)等待T2,3释放X-Lock(Y) / \text{/} / / \text{/} / - 对于 T 1 , 2 , 3 \text{T}_{1,2,3} T1,2,3的 Wait-For Graph → 不闭环 \text{Wait-For Graph}\xrightarrow{不闭环} Wait-For Graph不闭环不构成死锁
- TXN \text{TXN} TXN顺序: T2 \text{T2} T2完成 → T3获得X-Lock(Y) T 2 COMMIT释放X-Lock(Y) T3 \xrightarrow[\text{T3}获得\text{X-Lock(Y)}]{\text{T}_2\text{ COMMIT}释放\text{X-Lock(Y)}}\text{T3} T2 COMMIT释放X-Lock(Y)T3获得X-Lock(Y)T3完成 → T1获得X-Lock(Y) T 3 COMMIT释放X-Lock(Y) T1 \xrightarrow[\text{T1}获得\text{X-Lock(Y)}]{\text{T}_3\text{ COMMIT}释放\text{X-Lock(Y)}}\text{T1} T3 COMMIT释放X-Lock(Y)T1获得X-Lock(Y)T1完成
4️⃣死锁检测例题 2 \text{2} 2
序列: TXN \textbf{TXN} TXN 1 \textbf{1} 1 2 \textbf{2} 2 3 \textbf{3} 3 4 \textbf{4} 4 5 \textbf{5} 5 6 \textbf{6} 6 7 \textbf{7} 7 8 \textbf{8} 8 9 \textbf{9} 9 序列 2: T1 \text{2: T1} 2: T1 / \text{/} / W(Y) \small\text{W(Y)} W(Y) / \text{/} / / \text{/} / / \text{/} / W(X) \small\text{W(X)} W(X) C \small\text{C} C / \text{/} / / \text{/} / 序列 2: T2 \text{2: T2} 2: T2 W(X) \small\text{W(X)} W(X) / \text{/} / / \text{/} / / \text{/} / W(Y) \small\text{W(Y)} W(Y) / \text{/} / / \text{/} / C \small\text{C} C / \text{/} / 序列 2: T3 \text{2: T3} 2: T3 / \text{/} / / \text{/} / R(Z) \small\text{R(Z)} R(Z) W(Z) \small\text{W(Z)} W(Z) / \text{/} / / \text{/} / / \text{/} / / \text{/} / C \small\text{C} C
- Lock \text{Lock} Lock请求时序: T 1 , 2 , 3 \text{T}_{1,2,3} T1,2,3对 X-Lock(Y) \text{X-Lock(Y)} X-Lock(Y)的
T1 \textbf{T1} T1 T2 \textbf{T2} T2 T3 \textbf{T3} T3 / \text{/} / W(X) → 获得 X-Lock(X) \text{W(X)}\xrightarrow{获得}\text{X-Lock(X)} W(X)获得X-Lock(X) / \text{/} / W(Y) → 获得 X-Lock(Y) \text{W(Y)}\xrightarrow{获得}\text{X-Lock(Y)} W(Y)获得X-Lock(Y) / \text{/} / / \text{/} / / \text{/} / / \text{/} / R(Z) → 获得 S-Lock(Z) \text{R(Z)}\xrightarrow{获得}\text{S-Lock(Z)} R(Z)获得S-Lock(Z) / \text{/} / / \text{/} / W(Z) → 获得 X-Lock(Z) \text{W(Z)}\xrightarrow{获得}\text{X-Lock(Z)} W(Z)获得X-Lock(Z) / \text{/} / W(Y) → T 1 释放 等待 X-Lock(Y) \text{W(Y)}\xrightarrow[\text{T}_{1}释放]{等待}\text{X-Lock(Y)} W(Y)等待T1释放X-Lock(Y) / \text{/} / W(X) → T 2 释放 等待 X-Lock(X) \text{W(X)}\xrightarrow[\text{T}_{2}释放]{等待}\text{X-Lock(X)} W(X)等待T2释放X-Lock(X) / \text{/} / / \text{/} / - 对于 T 1 , 2 , 3 \text{T}_{1,2,3} T1,2,3的 Wait-For Graph → 闭环 \text{Wait-For Graph}\xrightarrow{闭环} Wait-For Graph闭环构成了死锁
- TXN \text{TXN} TXN顺序:只有 T 3 \text{T}_3 T3可以完成
5️⃣一些课外补充
- 死锁的解决:选择一个死锁的 TXN \text{TXN} TXN去(部分)回退,至于是哪个可以根据 Age \text{Age} Age/负载等
- 死锁的预防: Wait-die and Wound-die Policy \text{Wait-die and Wound-die Policy} Wait-die and Wound-die Policy
2.3.2.3. Concurrency Control Method \textbf{2.3.2.3. Concurrency Control Method} 2.3.2.3. Concurrency Control Method
1️⃣ TXN \text{TXN} TXN序列:是一个 Loss Update Problem \text{Loss Update Problem} Loss Update Problem问题
Time \textbf{Time} Time t=0 \textbf{t=0} t=0 t=1 \textbf{t=1} t=1 t=2 \textbf{t=2} t=2 t=3 \textbf{t=3} t=3 Final \textbf{Final} Final T1 \text{T1} T1 R(A) \text{R(A)} R(A) / \text{/} / / \text{/} / W(A) \text{W(A)} W(A) COMMIT \text{COMMIT} COMMIT T2 \text{T2} T2 / \text{/} / R(A) \text{R(A)} R(A) W(A) \text{W(A)} W(A) / \text{/} / COMMIT \text{COMMIT} COMMIT 2️⃣用 Binary Lock \text{Binary Lock} Binary Lock解决
- 规则:
操作 已占用 Lock \textbf{Lock} Lock 申请新的 Lock \text{Lock} Lock ❌ - 执行
阶段 操作 T1-R(A) \text{T1-R(A)} T1-R(A) 获取 Lock(A) \text{Lock(A)} Lock(A)并执行 T2-R(A) \text{T2-R(A)} T2-R(A)和 T2-W(A) \text{T2-W(A)} T2-W(A) 等待 T1 \text{T1} T1释放 Lock(A) \text{Lock(A)} Lock(A) T1-W(A) \text{T1-W(A)} T1-W(A) 已有 Lock(A) \text{Lock(A)} Lock(A)并执行 COMMIT \text{COMMIT} COMMIT 只有 W(A) \text{W(A)} W(A)完成执行 3️⃣ Shared and Exclusive Locks(Read and Write Locks) \text{Shared and Exclusive Locks(Read and Write Locks)} Shared and Exclusive Locks(Read and Write Locks)解决
- 规则
操作 已占用 S-Lock \textbf{S-Lock} S-Lock 已占用 X-Lock \textbf{X-Lock} X-Lock 申请新的 S-Lock \text{S-Lock} S-Lock ✔️ ❌ 申请新的 X-Lock \text{X-Lock} X-Lock ❌ ❌ - 执行
![]()
阶段 操作 T1-R(A) \text{T1-R(A)} T1-R(A) 获取 S-Lock(A)-1 \text{S-Lock(A)-1} S-Lock(A)-1 T2-R(A) \text{T2-R(A)} T2-R(A) 获取 S-Lock(A)-2 \text{S-Lock(A)-2} S-Lock(A)-2 (在已有 S-Lock \text{S-Lock} S-Lock情况下在获取一个 S-Lock \text{S-Lock} S-Lock是 OK \text{OK} OK的) T2-W(A) \text{T2-W(A)} T2-W(A) 等待 T1 \text{T1} T1释放 X-Lock(A)-1 \text{X-Lock(A)-1} X-Lock(A)-1 T1-W(A) \text{T1-W(A)} T1-W(A) 等待 T2 \text{T2} T2释放 X-Lock(A)-2 \text{X-Lock(A)-2} X-Lock(A)-2 COMMIT \text{COMMIT} COMMIT 死锁,谁都 COMMIT \text{COMMIT} COMMIT不了 2.4. Transaction Log \textbf{2.4. Transaction Log} 2.4. Transaction Log
1️⃣含义:记录数据库事务变化的日志,包括
- Transaction \text{Transaction} Transaction开始的记录
- 每个 SQL \text{SQL} SQL语句的详细信息
- Transaction COMMIT \text{Transaction COMMIT} Transaction COMMIT的记录
2️⃣作用:将所有更改恢复到最后一次 COMMIT \text{COMMIT} COMMIT的状态