Understanding MySQL-- lock, transaction, isolation levels, concurrency control (to be continued)

1.MySQL server logic architecture

      Source: MySQL official website
      Understanding MySQL-- lock, transaction, isolation levels, concurrency control (to be continued)
      for each connection will be at the MySQL server generates a thread (internal thread by thread pool management). For example: to enter a select statement, MySQL will first find out whether the cache select this result set in the query cache , if not continue to perform analytical → → perform optimization was the process; otherwise it will get the result set directly from the cache.

2.MySQL lock

2.1 share lock and exclusive lock (Shared and Exclusive Locks)

      Shared locks and exclusive locks are standard row-level locking .
      1) shared locks S : standard read locks , read locks to allow a plurality of connectors may be the same timing concurrently to read the same resources , without disturbing each other.
      2) exclusive lock the X- : Also known as a write lock , a write lock block other write locks or read locks to ensure the same time only one connection can write data , while preventing other users of the data read and write .

Lock Type English name Also known as Same time Common
Shared locks S Shared Locks Read lock The same time, allowing multiple concurrent connections to read the same resource, interference. Both are strategies lock mechanism itself, makes a distinction between these two strategies by lock
Exclusive lock X Exclusive Locks  Write lock The same time data can be written only one connection, while preventing other users to read and write the data. Ditto

2.2 intent lock (Intention Locks)

      Background: coexistence of multi-granularity locks scene.
      InnoDB supports multiple granularity locking ( lock granularity : can be divided into row locks and table locks ), are allowed to coexist and table locks. In order to achieve multi-level granularity of locking , InnoDB uses intent locks.
      Intent locks : table-level locks. First advance declaration of a intent , and get the table level intent lock (shared or exclusive intent lock IS intent lock IX), if succeed , it will be later, or is (are allowed) , the table of some lines plus locks (S or X) a.
      Note : In addition LOCK TABLES ... WRITE, will lock all the rows in the table, the other intent locks actual scene not lock any rows.
      An agreement of intent locks : in before the transaction can acquire a shared lock on a row in a table , he must first obtain a lock on the table IS or stronger lock . In before the transaction can acquire an exclusive lock on the rows in the table , it must first acquire an IX lock on the table .
      Intention locks for the multi-granularity locking background scene coexist, compatibility follows:

X IX S IS
X Conflict Conflict Conflict Conflict
IX Conflict Compatible Conflict Compatible
S Conflict Conflict Compatible Compatible
IS Conflict Compatible Compatible Compatible

      Conflict: exclusive
      Compatible: compatibility
      intent lock only indication of intention, a weak lock , compatible parallel (the IS, compatible parallel between IX) between intent locks. X and IS, IX mutually exclusive, S and IX are mutually exclusive. Intent lock is weaker than the X / S locks , there is an anticipation significance. First get weaker IS, IX lock, if the acquisition fails do not have to get stronger S, X lock.

2.3 Index row lock (Record Locks)

        eg: SELECT c1 FROM t WHERE c1 = 10 FOR UPDATE, if the index is used to c1. Prevent another transaction changes the line c1 = 10's.
        record lock always lock on the index line. Even if a table has not set any index, this time innoDB would create an implicit clustered index (primary Key), and then lock on the clustered index.
        When not indexed query field, eg: update t setcolumn = " A" where column B = "B", if columnB field index (or is not a combination of the prefix index) does not exist, this statement will lock the lock table all records i.e. . If the execution of the statement is capable of performing a columnB index field, then only the rows where the meet will be locked.

2.4 Lock a gap (Gap locks)

        Lock gaps: the gap between the index record locking, or locking a gap index before recording, or after a locking gap index record.
        For example: SELECT uid FROM t1 WHERE uid BETWEEN 1and 4 FOR UPDATE, blocking another transaction uid values into the column, since the gap existing between all values within the range are locked.
        For statements using a unique index to search for unique rows select a from, no gap is locked . (It does not comprise a combination of unique index, namely: gapLock does not act on single unique index )
        , for example: SELECT * FROM t WHERE id = 100, if no index id or a non-unique index, the statement in front of the locking gap. If the displacement index id column, the above statement is only to lines 100 using the id index record locks, whether other sessions to insert a row in the front space is not important.
        A single index can span the gap value and a plurality of index values, or even empty.
        The gap between the lock is a tradeoff in performance and concurrency lock compromise weight, for certain transaction isolation level, such as RC level.
        In the overlap gap (or overlapping rows) gap allowed to coexist.
        The same gap, allowing a transaction holds gap X-Lock (gap write lock / exclusive lock), while in another transaction in the holding gap (gap write lock / exclusive lock)

2.5next-key lock

        next-key lock is a combination of the record lock and gap lock.
        For example: the presence of a query row match b = 6 (with a non-unique index on b), NextLock is: B in ( "before" = rows added RecordLock lock 6, and b = before use GapLock lock 6 : Sort index) of all rows.
        MySQL query execution when row-level locking strategy, will scan the rows in the lock (X or S), which is coupled Record Lock, simultaneously on all lines before the record plus the Gap Lock.
        Next Key Lock is assumed that one index contains the index value Ken 10,11,13 and 20. The locking the following sections:
        Understanding MySQL-- lock, transaction, isolation levels, concurrency control (to be continued)
        Note: innoDB default isolation level (RR), next key lock automatically. (The reason: gap acts on the RR, if it is RC, gap Lock does not take effect, then the next key lock nature will not take effect).

2.6 intent lock insert (Insert Intention Locks)

        insert intention lock insert is a place in the insert statement gap lock when the gap locks , locks all the rows before inserting rows .
        Insert intent lock in such a way to indicate that insertion of intent, if inserted into multiple transactions in the same index in the gap is not plugged into the same position of the gap, they do not need to wait for the other.
        For example: the presence of index values 5 and 8 of the recording, are inserted into separate transaction attempt 6 and 7, prior to obtaining an exclusive lock on the inserted line, for each transaction using the gap between the locking insert intention lock 4 and 7, but not block each other, because these actions do not conflict.

Understanding MySQL-- lock, transaction, isolation levels, concurrency control (to be continued)
Understanding MySQL-- lock, transaction, isolation levels, concurrency control (to be continued)

2.7AUTO-INC Locks

        AUTO-INC lock is a special table lock, produced in this scenario: insert transaction (inserting into) the table having AUTO_INCREMENT column.
        In the simplest case, if the value of a transaction is inserted into the table, then any other transaction must wait inserted into the table values themselves, so that a transaction is inserted by the first row receiving a continuous primary key value.
 

3. Transaction

        Transaction is a set of atomic sql, or a separate unit of work. That transaction, or MySQL engines will all perform in this group sql statement, or all not execute (where such a statement fails).

3.1 auto-commit (AutoCommit, MySQL default)

   Understanding MySQL-- lock, transaction, isolation levels, concurrency control (to be continued)
        MySQL default to AutoCommit mode, that is, each is a sql transaction, you do not need an explicit enforcement branch. If autoCommit closed, then each sql are enabled by default a transaction, the transaction will be submitted only after an explicit implementation of "commit".

3.2 Display Transaction (START TRANSACTION ... COMMIT)

        Example: Great give small Y L 100 transfers money, what needs to perform three operations:
        1) checking account balance is greater than the small Y 100;
        2) small Y account reduction 100;
        3) a large increase in L account 100.
        These three operations is a transaction, you must be packaged execution, either all succeed or does not perform, any failure will result in a operation of all three operations, "no execute" - rollback.

ACID 3.3 transactions

        A (atomiciy) Atomicity: a transaction must ensure that either all operations which executed or rolled back, there can be performed only a portion of this situation.
        C (consistency) Consistency: data must ensure that conversion from one consistent state to another consistent state. Such a transaction is performed on the second step of a system crash, the data will not appear less account small Y 100, L case of large accounts but not changed. Either maintaining the original (rolled back), or less small Y 100 L large while the plurality 100, only two of coherency states.
        I (isolation) Isolation: When a transaction is not completed execution, usually ensure that other Session can not see the results of this transaction.
        D (durability) Persistence: Once the data commit, will be preserved. After the completion of timely data submission system crashes, data will not be lost.
 

4. isolation level

        

Guess you like

Origin blog.51cto.com/13678728/2479233