innodb-四種隔離級別

四種隔離級別

1.innodb有四種標準的隔離級別:READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE.默認隔離級別是REPEATABLE READ

InnoDB offers all four transaction isolation levels described by the SQL:1992 standard: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE. The default isolation level for InnoDB is REPEATABLE READ.

2.用戶可以爲一個session中或所有的connection設置事務隔離級別屬性

A user can change the isolation level for a single session or for all subsequent connections with the SET TRANSACTION statement.

3.innodb在不同的隔離級別下使用不同的鎖策略,使用默認的RR級別對ACID要求合規的重要數據可以基本滿足高度一致性。

InnoDB supports each of the transaction isolation levels described here using different locking strategies. You can enforce a high degree of consistency with the default REPEATABLE READ level, for operations on crucial data where ACID compliance is important.

4.如果對數據的一致性要求不高,可以使用較爲鬆式的策略,例如:RC、RU

Or you can relax the consistency rules with READ COMMITTED or even READ UNCOMMITTED, in situations such as bulk reporting where precise consistency and repeatable results are less important than minimizing the amount of overhead for locking

5.innodb-SERIALIZABLE隔離級別是一種最嚴格的規則,一般用於XA事務或解決併發死鎖故障

SERIALIZABLE enforces even stricter rules than REPEATABLE READ, and is used mainly in specialized situations, such as with XA transactions and for troubleshooting issues with concurrency and deadlocks.

RR級別:

特性1.一致性讀的方式是在第一次讀時建立快照

Consistent reads within the same transaction read the snapshot established by the first read.

特性2.在鎖定讀、update、delete時,加鎖方式依賴於查詢的方式,即是否是根據唯一索引查詢, 如果是則鎖定行,如果不是則可能會產生間隙鎖

For a unique index with a unique search condition, InnoDB locks only the index record found, not the gap before it.
For other search conditions, InnoDB locks the index range scanned, using gap locks or next-key locks to block insertions by other sessions into the gaps covered by the range. For information about gap locks and next-key locks, see Section 14.7.1, “InnoDB Locking”.

RC級別:

特性1.一致性讀的方式是每次讀取和更新都會刷新快照

sets and reads its own fresh snapshot

特性2.在鎖定讀、update、delete時,加鎖方式是隻鎖定索引記錄,不鎖間隙

For locking reads (SELECT with FOR UPDATE or LOCK IN SHARE MODE), UPDATE statements, and DELETE statements, InnoDB locks only index records, not the gaps before them, and thus permits the free insertion of new records next to locked records

特性3.在RC級別下binlog模式只支持row-based,即使配置了binlog_format=MIXED,實際也是採用row-based

Only row-based binary logging is supported with the READ COMMITTED isolation level. If you use READ COMMITTED with binlog_format=MIXED, the server automatically uses row-based logging.

特性4.在RC級別下,根據非索引字段更新,只會鎖定where條件下對應的行,不會鎖定全錶行記錄

If READ COMMITTED is used instead, the first UPDATE acquires an x-lock on each row that it reads and releases those for rows that it does not modify

在RC級別下,因爲沒有間隙鎖的保護,這時會出現幽靈問題,也可以叫做幻讀

Because gap locking is disabled, phantom problems may occur, as other sessions can insert new rows into the gaps. For information about phantoms

RC級別作用和啓用innodb_locks_unsafe_for_binlog配置類似,但兩者也有區別:
1.innodb_locks_unsafe_for_binlog可以被設置成全局/所有session,但RC級別可以設置爲所有session/單個session

Enabling innodb_locks_unsafe_for_binlog is a global setting and affects all sessions, whereas the isolation level can be set globally for all sessions, or individually per session.

2.innodb_locks_unsafe_for_binlog只能在mysql服務啓動前設置,RC級別可以在服務運行時設置

innodb_locks_unsafe_for_binlog can be set only at server startup, whereas the isolation level can be set at startup or changed at runtime.

RU級別:

特性1.讀取不具有一致性,存在髒讀問題

SELECT statements are performed in a nonlocking fashion, but a possible earlier version of a row might be used. Thus, using this isolation level, such reads are not consistent. This is also called a dirty read.

SERIALIZABLE級別:

特性1.在atuocommit關閉的情況下,所有的select操作都會加共享鎖

This level is like REPEATABLE READ, but InnoDB implicitly converts all plain SELECT statements to SELECT … LOCK IN SHARE MODE if autocommit is disabled.

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