鎖粒度
- 表級(jí)鎖:MYSQL中最基本的鎖策略,擁有最小的開銷烛亦,鎖定整張表诈泼。當(dāng)客戶端對(duì)表進(jìn)行寫操作時(shí),將獲取寫鎖煤禽,同時(shí)阻塞其他的讀與寫操作铐达;當(dāng)客戶端進(jìn)行讀操作時(shí),將獲取讀鎖檬果,不影響其他的讀獲取鎖瓮孙。
- 行級(jí)鎖:提供最優(yōu)秀的并發(fā)能力,同時(shí)花費(fèi)最大的開銷选脊。InnoDB 和 XtraDB 存儲(chǔ)引擎實(shí)現(xiàn)的是行級(jí)鎖杭抠。
事務(wù)
ACID
- Atomicity(原子性):事務(wù)必須作為一個(gè)單個(gè)的不可分割的工作單元,事務(wù)中的操作或者都成功恳啥,或者回滾祈争。
- Consistency(一致性):數(shù)據(jù)庫必須總是從一個(gè)一致性狀態(tài)到下一個(gè)一致性狀態(tài)。
- Isolation(隔離性):一個(gè)事務(wù)的結(jié)果通常是只有這個(gè)事務(wù)完成時(shí)才對(duì)其他事務(wù)可見角寸。
- Durability(持久性):一旦事務(wù)被提交,其做的改變必須持久化忿墅。
隔離等級(jí)
- READ UNCOMMITTED(未提交讀):事務(wù)可以看到其他未提交事務(wù)的結(jié)果扁藕,也成為臟讀。性能并不比其他隔離級(jí)別好很多疚脐。
- READ COMMITTED(提交讀):也叫不可重復(fù)讀亿柑。大多數(shù)數(shù)據(jù)庫的默認(rèn)隔離級(jí)別(MYSQL不是)。一個(gè)事務(wù)所做的改變只有提交完才對(duì)其他事務(wù)可見棍弄。
- REPEATABLE READ(可重復(fù)讀):保證了同一個(gè)事務(wù)中讀取同一行時(shí)望薄,讀取兩次結(jié)果是一致的『艋可能會(huì)出現(xiàn)幻讀痕支。
- SERIALIZABLE(串行化):最高隔離級(jí)別。強(qiáng)制事務(wù)串行化執(zhí)行蛮原,確保了事物間不互相影響卧须。解決了幻讀問題。
隔離等級(jí)優(yōu)缺點(diǎn)
Isolation level | Dirty reads possible | Nonrepeatable reads possible | Phantom reads possible | Locking reads |
---|---|---|---|---|
READ UNCOMMITTED | YES | YES | YES | NO |
READ COMMITTED | NO | YES | YES | NO |
REPEATABLE READ | NO | NO | YES | NO |
SERIALIZABLE | NO | NO | NO | YES |
MVCC(multiversion concurrency control)
工作原理:保存數(shù)據(jù)在某個(gè)時(shí)間點(diǎn)的快照。
InnoDB的MVCC實(shí)現(xiàn)
在每一行存了兩個(gè)額外的隱藏值:行的創(chuàng)建事務(wù)版本號(hào)和行的刪除事務(wù)版本號(hào)花嘶。
例:
- SELECT:InnoDB must examine each row to ensure that it meets two criteria:
- InnoDB must find a version of the row that is at least as old as the transaction
(i.e., its version must be less than or equal to the transaction’s version). This
ensures that either the row existed before the transaction began, or the transaction
created or altered the row. - The row’s deletion version must be undefined or greater than the transaction’s
version. This ensures that the row wasn’t deleted before the transaction began.
Rows that pass both tests may be returned as the query’s result.
- InnoDB must find a version of the row that is at least as old as the transaction
- INSERT:
InnoDB records the current system version number with the new row. - DELETE:
InnoDB records the current system version number as the row’s deletion ID. - UPDATE:
InnoDB writes a new copy of the row, using the system version number for the new
row’s version. It also writes the system version number as the old row’s deletion
version.
MYSQL存儲(chǔ)引擎
InnoDB
- 事務(wù)
- 支持表級(jí)笋籽、行級(jí)鎖
MYISAM
- 非事務(wù)
- 只支持表級(jí)鎖
- 可對(duì)BLOB、TEXT列的前五百個(gè)字符進(jìn)行索引
- 支持全文索引
Archive
- 只支持INSERT與SELECT
- 到MYSQL5.1才支持索引
XtraDB
- 第三方引擎
- InnoDB的修改版
改變表的存儲(chǔ)引擎
- ALTER TABLE mytable ENGINE = InnoDB;
- 最簡單的方式
- 花費(fèi)時(shí)間長:MYSQL會(huì)一行一行的拷數(shù)據(jù)到一個(gè)新表中
- 可能會(huì)占滿磁盤的I/O
- 在轉(zhuǎn)換過程中椭员,原始表會(huì)被讀鎖定车海,阻塞對(duì)標(biāo)數(shù)據(jù)的讀取
- Dump and import
- 使用mysqldump先備份表
- 編輯dump文件,修改表名字和引擎類型
- mysqldump默認(rèn)在CREATE TABLE前會(huì)寫一個(gè)DROP TABLE語句隘击,所以執(zhí)行前要小心
- CREATE and SELECT
- 前兩種方式的中和
- 創(chuàng)建新表侍芝,并使用INSERT ... SELECT向新表中填充數(shù)據(jù),適合數(shù)據(jù)不是很多時(shí)
mysql> CREATE TABLE innodb_table LIKE myisam_table; mysql> ALTER TABLE innodb_table ENGINE=InnoDB; mysql> INSERT INTO innodb_table SELECT * FROM myisam_table;
- 當(dāng)數(shù)據(jù)較多時(shí)闸度,使用增量插入更好一些
mysql> START TRANSACTION; mysql> INSERT INTO innodb_table SELECT * FROM myisam_table WHERE id BETWEEN x AND y; mysql> COMMIT;