mysql performance optimization (three) - Transaction Control

1, Innodb degree of engine supports isolation levels

Transaction isolation level Dirty read Non-repeatable read Magic Reading
Uncommitted Read (Read Uncommited) may may may
Read Committed (Read Commited) impossible may may
Repeatable Read (Repeated Read) impossible impossible Impossible to Innodb
Serialization (the Serializable) impossible impossible impossible

 

 

 

 

 

The isolation level in the end how to achieve it? Lock, MVCC

2, the table lock appreciated, row locks

Lock for concurrent access to shared resources to manage different affairs

Table difference locks and row locks:

Lock particle size: Table lock> row lock

Locking efficiency: Table Lock> Lock row

The probability of conflict: Table Lock> Lock row

Concurrency: Table lock <row lock

InnoDB storage engine supports row and table locks (alternative row locks)

3, MySql InnoDB lock type

Shared lock (line lock): Shared Locks

Exclusive lock (line lock): Exclusive Locks

Shared intent lock lock (table lock): Intention Shared Locks

Intent lock exclusive lock (table lock): Intention Exclusive Locks

Increase self-locking: AUTO-INC Locks

a, row lock algorithm

Record lock Record Locks

Gap lock Gap Locks

Pro Keylock Next-key Locks

b, a shared lock (Shared Locks) VS exclusive lock (Exclusive Locks)

Shared lock: also known as a read lock, referred to as the S lock, as the name implies, a shared lock is multiple transactions for the same data can be shared with a lock, you can access the data, but can only be read can not be modified;

Unlocking locking mode: select * from users WHERE id = 1 LOCK IN SHARE MODE; commit / rollback;

Exclusive lock: also known as a write lock, referred to as X lock, exclusive lock can not coexist with other locks, such as a transaction acquires an exclusive lock a data line, other transactions can no longer acquire the lock of the line (shared lock, exclusive lock) only the exclusive lock acquired transaction can read and modify rows of data, (other transactional data can be read from a snapshot)

Locking unlocking way:

delete / update / insert default lock plus X

SELECT * FROM table_name WHERE ... FOR UPDATE commit/rollback

c, innodb- row locks in the end to do what?

InnoDB row lock is achieved through the lock on the index entry to the index.

Only through the index data retrieval condition, InnoDB only row-level locking. Otherwise, InnoDB uses (all records locked index) table lock

表锁:lock tables xx read/write;

d, intent share lock locks (IS) & intent exclusive lock latch (IX)

Intention shared lock (IS)

Represents the transaction is ready to join a shared lock rows of data, a data line that is shared locks must first obtain a lock on the table before IS is compatible with each other intent shared lock

Intent exclusive locks (IX)

It represents the transaction is ready to join exclusive lock rows of data, namely data lines plus a row must first obtain a lock IX lock the table before him, intent exclusive locks are compatible with each other between the

Intention locks (IS, IX) is added prior to the automatic operation InnoDB data, without user intervention

significance:

When the transaction were to go lock table, you can determine whether there is intent locks, you can quickly return to the table can not be enabled table lock when there is

e, by the self-locking AUTO-INC Locks

For from a special table-level locking additional growth from

 

show variables like 'innodb_autoinc_lock_mode';

The default value of 1, represents the continuous, permanent loss of uncommitted transaction ID

f, the lock record (Record) & locking gap (Gap) & temporary key lock (Next-key)

Record locks:

Lock specific index entry

Sql performed when retrieving data according uniqueness (Primary key, Unique key) index, query and the query matches the contour data is present, then the SQL statement that is recorded together with the lock latch Record locks, locks particularly index entry

Gap locks:

Interval lock the data does not exist (left open right open)

When sql executed to retrieve data according to the index, data query condition does not exist, then the SQL statement with lock is the Gap locks, lock the index does not exist an interval (open left right open)

Next-key locks:

+ Record locking section (right-left opening and closing)

当sql执行按照索引进行数据的检索时,查询条件为范围查找(between and、<、>等)并有数 据命中则此时SQL语句加上的锁为Next-key locks,锁住索引的记录+区间(左开右闭)

g、临键锁(Next-key)

 

 

 

 

 为什么innodb选择临键锁Next-key作为行锁的默认算法?

 

 h、间隙锁(Gap)

 

 

 

 

Gap只在RR事务隔离级别存在

i、记录锁(Record)

 

 

 

 

4、利用锁怎么解决脏读

 

 

 

 

 使用排它锁

5、利用锁怎么解决不可重复读

 

 

 使用共享锁

6、利用锁怎么解决幻读

 

 

 

使用临键锁

7、死锁介绍

 多个并发事务(2个或者以上);

   每个事务都持有锁(或者是已经在等待锁);

   每个事务都需要再继续持有锁;

 事务之间产生加锁的循环等待,形成死锁。

死锁的避免

1)类似的业务逻辑以固定的顺序访问表和行。

2)大事务拆小。大事务更倾向于死锁,如果业务允许,将大事务拆小。

3)在同一个事务中,尽可能做到一次锁定所需要的所有资源,减少死锁概 率。

4)降低隔离级别,如果业务允许,将隔离级别调低也是较好的选择

5)为表添加合理的索引。可以看到如果不走索引将会为表的每一行记录添 加上锁(或者说是表锁)

8、MVCC

MVCC:

Multiversion concurrency control (多版本并发控制)

并发访问(读或写)数据库时,对正在事务内处理的数据做 多版本的管理。以达到用来避免写操作的堵塞,从而引发读操 作的并发问题。

Guess you like

Origin www.cnblogs.com/nangonghui/p/11454703.html