笔记分享: 墨尔本大学INFO20003数据库系统——04. 归一化与事务

文章目录


更多 资料笔记

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️⃣示例

  1. 标准化前
    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
  2. 标准化后
    学生表 \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} AB
查询一个表
查询两个表
插入/修改/删除容易困难

1.2.   Database   Anomalies \textbf{1.2. Database Anomalies} 1.2. Database Anomalies​异常

1️⃣概述:期末会给你表格,让你用文字描述举例三种 Anomaly \text{Anomaly} Anomaly

  1. 示例表格
    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
  2. 基本概念
    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: 不能根本解决

  1. 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/DescQty NULL \text{NULL} NULL
    • 不可以的示例:插入 Item/Desc → Qty \text{Item/Desc}\to{}\text{Qty} Item/DescQty NULL/Order \text{NULL/}\text{Order} NULL/Order不能为 NULL \text{NULL} NULL
  2. Deletion Anomaly: \text{Deletion Anomaly:} Deletion Anomaly: 同理可能可以,当删除的数据不是 PK \text{PK} PK时,可考虑设为 NULL \text{NULL} NULL
  3. 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️⃣概述

  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
  2. 符号: X → Y \text{X}\to{}\text{Y} XY表示如果知道 X \text{X} X就知道 Y \text{Y} Y,比如 Stu-Name → Stu-FirstName \text{Stu-Name}\to{}\text{Stu-FirstName} Stu-NameStu-FirstName
  3. 性质:
    • 依赖恒等式: X → X \text{X}\to{}\text{X} XX PK → All Column \text{PK}\to{}\text{All Column} PKAll 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{}只有右侧可拆分 : ABCD 可以拆分为: ABC / ABD不可拆分为: ACD / BCD只有右侧可拆分

2️⃣有关概念:以 A ( X ‾ , Y ‾ , Z , D ) \text{A}(\underline{\text{X}}, \underline{\text{Y}}, \text{Z}, \text{D}) A(X,Y,Z,D)为例

  1. Determinants: \text{Determinants:} Determinants:
    • 箭头左侧的属性,比如 X ‾ , Y ‾ → Z \underline{\text{X}} ,\underline{\text{Y}}\to{}\text{Z} X,YZ中的 XY \text{XY} XY
    • 至于这其中 Z \text{Z} Z叫什么就无具体说法
  2. 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
  3. 两种 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} YZ
    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} ZD

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} BA ⇒ \Rightarrow A → B \text{A} \rightarrow \text{B} AB或者说 AB → A \text{A}\text{B}\to{}\text{A} ABA
Augmentation \text{Augmentation} Augmentation(扩展) A → B \text{A}\rightarrow\text{B} AB ⇒ \Rightarrow AC → BC \text{AC} \rightarrow \text{BC} ACBC 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 BC ⇒ \Rightarrow A → C \text{A} \rightarrow \text{C} AC 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

  1. 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)
  2. 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
  3. 示例:注意区分 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(OrderItem, Desc, Qty)

2️⃣ 2NF \text{2NF} 2NF

  1. 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 依赖)
  2. 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
  3. 示例:注意区分 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(OrderItem, Desc, Qty))存在 Order,ItemQtyItemDesc (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(OrderItem, Desc, Qty))→ Item(Item, Desc)Order-Item(OrderItem, Qty)

3️⃣ 3NF \text{3NF} 3NF​​

  1. 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 依赖)
  2. 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
  3. 示例:注意区分 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} EmptEname, DeptDeptDname (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(课外,可不看)

  1. 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
  2. 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 MR SMR 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​​了
  3. 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定义:

  1. 定义:数据库操作序列( A logical unit of work \text{A logical unit of work} A logical unit of work​)
  2. 根本特性:要么一次性执行完 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引入的目的

  1. 定义 Unit of Work \text{Unit of Work} Unit of Work,适用于一个/多个用户
  2. Concurrent Access \text{Concurrent Access} Concurrent Access,允许多个 Command \text{Command} Command交织执行,适用于多个用户

3️⃣ Tansaction \text{Tansaction} Tansaction种类

  1. 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就是一个事务
    
  2. 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

image-20240526204243601
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️⃣示例

  1. 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)
  2. 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

  1. 含义:多个 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}$执行效果)
  2. 上例中:若 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​数据

image-20240526220018050
  • 理论上,可以合理安排 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️⃣总体思路:

  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)
  2. 物理上:在 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
  1. 理想情况 Field-Level \text{Field-Level} Field-Level最好,但其 Lock \text{Lock} Lock​过于复杂( High Overhead \text{High Overhead} High Overhead),反倒最不常用
  2. 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​​

  1. Binary Lock: \text{Binary Lock:} Binary Lock: Lock/Unlock \text{Lock/Unlock} Lock/Unlock两种状态,只要用到数据(不论读写)都是 Lock \text{Lock} Lock
  2. 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

  1. 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一个个解锁,当解锁开始时就不能再上锁了
  2. 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

  1. 用圈代表 TXN \text{TXN} TXN,用箭头表示等待( A → B \text{A}\to{}\text{B} AB表示 A \text{A} A在等待 B \text{B} B释放某个 Lock \text{Lock} Lock)
  2. Graph \text{Graph} Graph构成闭环 → \to{} 死锁,但注意成环不代表闭环,闭环是要形成环路(下图示例)
    image-20240527033215115

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{/} /
  1. 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{/} /
  2. 对于 T 1 , 2 , 3 \text{T}_{1,2,3} T1,2,3 Wait-For Graph → 不闭环 \text{Wait-For Graph}\xrightarrow{不闭环} Wait-For Graph不闭环 ​不构成死锁
    image-20240527033243392
  3. 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
  1. 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{/} /
  2. 对于 T 1 , 2 , 3 \text{T}_{1,2,3} T1,2,3 Wait-For Graph → 闭环 \text{Wait-For Graph}\xrightarrow{闭环} Wait-For Graph闭环 构成了死锁
    image-20240527035828981
  3. TXN \text{TXN} TXN顺序:只有 T 3 \text{T}_3 T3​可以完成

5️⃣一些课外补充

  1. 死锁的解决:选择一个死锁的 TXN \text{TXN} TXN去(部分)回退,至于是哪个可以根据 Age \text{Age} Age/负载等
  2. 死锁的预防: 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​​​​​解决

  1. 规则:
    操作已占用 Lock \textbf{Lock} Lock
    申请新的 Lock \text{Lock} Lock
  2. 执行
    阶段操作
    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)​​解决

  1. 规则
    操作已占用 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
  2. 执行
    阶段操作
    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} COMMITimage-20240527045640074    ⟹    \implies 死锁,谁都 COMMIT \text{COMMIT} COMMIT不了

2.4.   Transaction   Log \textbf{2.4. Transaction Log} 2.4. Transaction Log

1️⃣含义:记录数据库事务变化的日志,包括

  1. Transaction \text{Transaction} Transaction开始的记录
  2. 每个 SQL \text{SQL} SQL语句的详细信息
  3. Transaction COMMIT \text{Transaction COMMIT} Transaction COMMIT的记录

2️⃣作用:将所有更改恢复到最后一次 COMMIT \text{COMMIT} COMMIT的状态

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值