1. innodb在不同隔離級別下的一致性讀及鎖的差異 不同的隔離級別下,innodb處理sql 時採用的一致性讀策略和需要的鎖是不同的,同時,數據恢復和複製機制的特點,也對一些sql的一致性讀策略和鎖策略有很大影響。對於許多sql, 隔離級別越高,innodb給記錄集的鎖就越嚴格(龍其是使用範圍條 ...
1. innodb在不同隔離級別下的一致性讀及鎖的差異
不同的隔離級別下,innodb處理sql 時採用的一致性讀策略和需要的鎖是不同的,同時,數據恢復和複製機制的特點,也對一些sql的一致性讀策略和鎖策略有很大影響。對於許多sql, 隔離級別越高,innodb給記錄集的鎖就越嚴格(龍其是使用範圍條件的時候),產生的鎖衝突的可能性也就越高,對併發性事務處理性能的影響也就越大。因此,在應用中,應該儘量使用較低的隔離級別,減少鎖爭用。通常使用Read Commited隔離級別就足夠了, 對於一些確實需要更高隔離級別的事務,可能在程式中執行 SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ 或SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE 動態來改變隔離級別。
下麵重點看下REPEATABLE READ與Read commited 鎖申請的不同區別,在增刪改查上申請的鎖都是一致的,但在事務中鎖釋放的時間是不一樣的這點需要註意。
SQL |
條件 |
Read uncommited |
Read commited |
Repeatable read |
serializable |
Select
|
= |
None locks |
Consisten read/ None locks |
Consisten read/ None locks |
Share locks |
範圍 |
None locks |
Consisten read/ None locks |
Consisten read/ None locks |
Share next-key |
|
Update |
= |
X(排它鎖) |
X |
X |
X |
範圍 |
X next-key |
X next-key |
X next-key |
X next-key |
|
Insert |
|
X |
X |
X |
X |
REPLACE |
無鍵衝突 |
X |
X |
X |
X |
鍵衝突 |
X next-key |
X next-key |
X next-key |
X next-key |
|
Delete
|
= |
X |
X |
X |
X |
範圍 |
X next-key |
X next-key |
X next-key |
X next-key |
|
Select ..from Lock in share mode |
= |
Share locks |
Share locks |
Share locks |
Share locks |
範圍 |
Share locks |
Share locks |
Share next-key |
Share next-key |
|
Select ..from For update |
= |
X |
X |
X |
X |
範圍 |
X |
Share locks |
X next-key |
X next-key |
|
Insert into.. Select .. |
Innodb_locks_unsafe _for_binlog=off |
Share next-key |
Share next-key |
Share next-key |
Share next-key |
Innodb_locks_unsafe _for_binlog=on |
None locks |
Consisten read/ None locks |
Consisten read/ None locks |
Share next-key |
|
Create table.. Select .. |
Innodb_locks_unsafe _for_binlog=off |
Share next-key |
Share next-key |
Share next-key |
Share next-key |
Innodb_locks_unsafe _for_binlog=on |
None locks |
Consisten read/ None locks |
Consisten read/ None locks |
Share next-key |