innodb-非鎖定一致性讀

非鎖定一致性讀

1.在RR和RC隔離級別下一致性讀是innodb默認的模式,一致性讀不會加任何鎖,不影響其他事務來修改相關數據

Consistent read is the default mode in which InnoDB processes SELECT statements in READ COMMITTED and REPEATABLE READ isolation levels. A consistent read does not set any locks on the tables it accesses, and therefore other sessions are free to modify those tables at the same time a consistent read is being performed on the table.

2.在RR級別下,一個事務建立快照是以第一次讀取操作時作爲時間點。如果想更新快照,需要提交當前事務然後再開啓一個事務執行查詢操作。

If the transaction isolation level is REPEATABLE READ (the default level), all consistent reads within the same transaction read the snapshot established by the first such read in that transaction. You can get a fresher snapshot for your queries by committing the current transaction and after that issuing new queries.

3.可以提前建立快照的兩種方式:1.提交當前事務然後再執行select 2.以"START TRANSACTION WITH CONSISTENT SNAPSHOT"的方式開啓一個事務

You can advance your timepoint by committing your transaction and then doing another SELECT or START TRANSACTION WITH CONSISTENT SNAPSHOT.

4.可以看到最新的數據庫狀態的兩種方式:1.使用RC隔離級別 2.使用鎖定讀方式

If you want to see the “freshest” state of the database, use either the READ COMMITTED isolation level or a locking read

這裏解釋下爲什麼使用鎖定讀方式可以看到數據庫最新狀態:
使用鎖定讀,可以防止數據被修改或者範圍內插入新的數據,因爲鎖定讀有兩種方式:select for update(排他鎖)、select lock in share mode(共享鎖),不管是哪種方式都是和排他鎖互斥的。

5.在特殊場景下一致性讀是不能工作的:1.drop table 2.alter table

1.Consistent read does not work over DROP TABLE, because MySQL cannot use a table that has been dropped and InnoDB destroys the table.
2.Consistent read does not work over ALTER TABLE, because that statement makes a temporary copy of the original table and deletes the original table when the temporary copy is built. When you reissue a consistent read within a transaction, rows in the new table are not visible because those rows did not exist when the transaction’s snapshot was taken. In this case, the transaction returns an error: ER_TABLE_DEF_CHANGED, “Table definition has changed, please retry transaction”.

6.在一些場景下不需要鎖定讀也會刷新快照,例如:insert…select、update…(select)、create…select

The type of read varies for selects in clauses like INSERT INTO … SELECT, UPDATE … (SELECT), and CREATE TABLE … SELECT that do not specify FOR UPDATE or LOCK IN SHARE MOD

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章