SQL 光速入門 + RDBMS Isolation levels & locks
Who am I
- @fateyan / @fateyan_tw / 肥燕
- 無業遊民
- 東華資工休學
- 在台北找工作中
- cakeresume.com/bo-syuan-fu
- 賞我飯吃 QQ
- 開源社群志工
- ex-區塊鏈打工仔
我是誰、我從哪裡來、我該往何處去
Adulthood hit me hard
Before we start
- 入門的部分
- 與其給魚不如給釣竿和告訴你釣魚點
- 淺嚐即止
- 定位 RDBMS
- 一句直白的解釋給 不是 dev 的朋友們
- Isolation levels & locking
- 就分享最近讀的東西 XD
- 知道處理 Concurrency 要讀什麼文件
- 每個段落都會留一些時間給大家問問題
What's RDBMS
Relational Database Management System
For manage your data
- Why to choose RDBMS over Files
- Who uses it/When to use it
- How to use it
- 暫時可以先想像它像 Excel
Why to choose RDBMS over files
- Identity and Access management
- Access over Network
- Performance
- Maintainability
It provides more management power over files
Who uses it,
When to use it
How to use it
- 選一個資料庫 & 安裝它 & 設定它
- 創建資料表(你需要先學會如何設計表以及 SQL 語法)
- 使用 SQL 語法對它做任何你想做的事情
- 等等會有 demo
So far, any questions?
SQL: A Quick Start
But never ends
Topics I didn't mentioned
- http://web.csie.ndhu.edu.tw/showyang/DB2019s/index.html
- Fundamental 綱要整理得很好,推薦拿來自學(?)
- 如果你是資工系學生,除非你手上有需求,不然我比較推薦把資結演算法和作業系統讀好再來看
- 像是 Indexing 的部分
- 當然你不用了解所有事情也能把 DBMS 用得很好
- 解決 real world problems 讀手冊應該還是比較快 xD
關於 Relational Algebra
Computer Science make Logic Great Again
CRUD Live demo
- Create, Read, Update, Delete
- MariaDB Knowledge Base: Transactions
- https://www.codecademy.com/learn/learn-sql
範例資料表+一些操作把玩+教你怎麼看SQL文件裡的文法
Transactions
What's Transaction?
- For Concurrency Control
- Finite sequence of SQL statements(or operations)
- ACID
- 不是所有資料庫都支援 Transaction
- MariaDB:InnoDB 支援
- MariaDB:MyISAM 不支援
- 你需要手動 Lock Table 來模擬
Concurrency Control
Race Condition
// Thread 1
read_shared_variableA;
write_shared_variableA;
// Thread 2
read_shared_variableA;
write_shared_variableA;
任意排列組合,結果都不一樣 xD
Locks coming out to save the world!
// Thread 1
acquire_lock_variableA;
// {
read_shared_variableA;
write_shared_variableA;
// }
release_lock_variableA;
// Thread 2
acquire_lock_variableA;
// {
read_shared_variableA;
write_shared_variableA;
// }
release_lock_variableA;
沒時間解釋太多,先當作問題解決了
就我所知,只有 Mutex 和 SpinLock,其它都是變種(多個鎖或記狀態)
What Transaction looks like
# MariaDB
START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;ACID of Transactions
- Atomicity:Transaction 裡的 SQL Statements 只會全部執行或全部不執行(Rollback)
- Consistency:執行 Transaction 前 Consistent 的話執行後也會是 Consistent 的(constraints, cascades, triggers)
- Isolation:Transactions 對其它用戶、系統甚至 Transaction 擁有資料完整性,且可設定不同 isolation level
- Durability:Committed Transaction 是永久的,即便 System Failure 發生
So far, any questions?
Isolation levels
Isolation levels
- 就像剛才講的,Isolation 是保證資料的完整性
- 但我們仍考慮拿一些完整性去換取效能
- 或者反過來說,我們需要犧牲多少效能來換得完整性
- 在講這些 Isolation levels 前,
我們先來認知一些各 levels 試圖解決的 Race Condition 問題- Dirty Read
- Non-repeatable reads
- Phantom reads
特別需要筆記的點:Range Lock 是 Index-record lock(B-Tree),在沒加 index 的情況可能會造成 full-table lock
總結來說
- Concurrency Control 可以靠不同 Granularity 的 Lock 來解
- A field of a database record (an attribute of a tuple)
- A database record (a tupleor a relation)
- A disk block
- An entire file
- The entire database
而 DBMS 提供了這樣的解決方案,算是呼應了前面說的 Why DBMS
結語
- 乖乖讀文件 XD
References
Q&A
感謝大家
SQL 光速入門 + RDBMS Isolation levels & locks
By Bo Syuan Fu (fateyan)
SQL 光速入門 + RDBMS Isolation levels & locks
- 115