06-Mysql lock mechanism

1. The locking mechanism of action

1  to ensure isolation between transactions. But also to ensure data consistency.
2  to ensure that resources are not contention. Lock is part of a resource, not a feature of the transaction.
3 times each transaction requires resources, it is necessary to apply a lock to hold resources.

2. Types of locks

Memory Lock: mutex, latch, to ensure that resources are not data page memory contention, not to be replaced.
Object lock:        
The MDL (metadata locks): when modifying metadata. DDL -> alter, backup
Table_lock: lock table, DDL, the backup (FTWRL global lock table), lock tables t1 read, can also be upgraded to a table lock.
record (row) lock: line lock, index lock, lock clustered index.
GAP: gap lock, RR level, ordinary index gap auxiliary lock.
Next-lock: next key locks, GAP + record lock, the lock range of the ordinary secondary indexes


Object lock granularity: 
        The MDL (metadata locks): when modifying metadata. DDL -> alter, backup
        Table_lock: lock table, DDL, the backup (FTWRL global lock table), lock tables t1 read, can also be upgraded to a table lock.
        record (row) lock: line lock, index lock, lock clustered index.
        GAP: gap lock, RR level, ordinary index gap auxiliary lock.
        Next-lock: next key locks, GAP + record lock, the lock range of the ordinary secondary indexes

Functional classification:
    IS: intent shared lock table level
    S: shared lock to read lock, row level
    IX: intention exclusive lock, the table level
    X: exclusive lock, write lock, row level

C 3. ACID properties of consistency matters

A: atomicity, UNDO, REDO
D: persistence, Redo (WAL)
I: isolation, ISOLATION Level, Lock, MVCC (UNDO)
C: to ensure the work before, during and after the state of the data is complete and consistent.
So all the features characteristic of C is above all to ensure consistency.
Write Consistency: UNDO, REDO, LOCK 
Read consistency: ISOLATION Level, MVCC (UNDO) 
Consistency of data pages: 
double write buffer (disk area)
doublewrite buffer on the InnoDB tablespace pages 128 (zone 2) size is 2MB. 
In order to solve the problem of partial page write, when MySQL will flush dirty data to the data file when the first use memcopy copy dirty data to doublewrite buffer memory,
after doublewrite buffer again by 2 times, each time to write 1MB shared table space, and then immediately call fsync function,
synchronized to disk, to avoid problems caused by a buffer, in the process, doublewrite written order, the overhead is not large, after the completion of doublewrite write,
and then double write buffer is written each table space file, then discrete written. So under normal circumstances, MySQL write data page, write to disk twice, the first pass is written doublewrite buffer,
the second time from doublewrite buffer is written to the actual data files. If the extreme case (power failure) occurs after InnoDB start again,
found a page of data has been damaged, then you can recover the data from the doublewrite buffer.

4. Storage Engine core parameters

1. innodb_flush_log_at_trx_commit=1/0/2 
One of a double standard: redo log flashed parameters
2. innodb_flush_method=fsync/O_DIRECT/O_DIRECT/O_SYNC
Role: MySQL brush control disk write, whether to use the OS Cache
fsync mode:
    buffer pool data when writing to disk, you need to go through os cache, and then written to disk
    redo buffer data when writing to disk, you need to go through os cache, and then written to disk
O_DSYNC: 
    buffer pool data when writing to disk, you need to go through os cache, and then written to disk
    redo buffer data writing to disk when written directly to disk, across OS Cache 

O_DIRECT: 
    buffer pool of data writing to disk when written directly to disk, across OS Cache
    redo buffer data when writing to disk, you need to go through os cache, and then written to disk
Production recommended O_DIRECT, preferably with a solid state disk.
3.innodb_buffer_pool_size
Role: The total size of the data buffer. Buffering data pages and index pages. MySQL is the largest area of ​​memory.
Default: 128M
Official recommendations: 80-90% of physical memory
Production recommendations: 75% or less, on-demand deployment
SET GLOBAL innodb_buffer_pool_size=402653184;
mysql> show engine innodb status \G

 

Guess you like

Origin www.cnblogs.com/metoyou/p/12363678.html