当前位置:网站首页>MySQL transactions and locks (V)

MySQL transactions and locks (V)

2022-06-13 03:01:00 summer_ forty-five

Database notes

Transactions and locks

Business

Definition

The business is made up of a group of SQL A logical processing unit made up of statements , have ACID Four properties

attribute

  • A Atomicity In business all operation Agreement

  • C Uniformity : Operations within a transaction == As a result of Data modification Should agree ==

  • I Isolation, : During transaction processing ** The intermediate state is invisible to the outside ** Of ; External state changes are also invisible to transactions .

  • D persistence : After the transaction completes , Yes == The modification of data is permanent .==

Problems caused by concurrent transaction processing

  • Dirty write —— Last update covers Updates made by other firms

  • Dirty reading —— Business A Read the business B Modified but not submitted The data of 【 Does not satisfy the isolation

  • Don't reread —— Business A Internal same query SQL Inconsistent results 【 Does not satisfy the isolation

  • Fantasy reading —— Business A Read transaction B New data submitted 【 Does not satisfy the isolation

Transaction isolation level

Definition

Databases have different isolation levels , The default isolation level is Repeatable

  • View the transaction isolation level of the current database :

    show variables like 'tx_isolation';
    
  • Set the transaction isolation level :

    set tx_isolation='REPEATABLE-READ';
    

classification

  • Read uncommitted
  • Read submitted
  • Repeatable
  • Serializable

Read uncommitted

set tx_isolation='read-uncommitted';

technological process :

  1. client A With client B Each setting is Read uncommitted
  2. client B** Update table account,** but Do not commit transaction
  3. client A Query table account, Read B Modified data
  4. If at this time ,B Roll back ,A Read dirty data

Possible problems :

Dirty reading 、 Don't reread 、 Fantasy reading

Read submitted

set tx_isolation = 'read-committed';

technological process :

  1. client A With client B Each setting is Read submitted
  2. client B** Update table account,** Commit transaction
  3. client A Query table account, Read B Modified data

Possible problems :

Don't reread 、 Fantasy reading

Repeatable

set tx_isolation = 'repeatable-read';

adopt undo Log files for version control , Achieve isolation

technological process :

  1. client A With client B Each setting is Repeatable
  2. client B** To the table account Add a new one id by 100 The data of ,** Commit transaction
  3. client A Query table account, You can go to id by 100 The data of

Possible problems :

Fantasy reading

Serialization

set tx_isolation = 'serializable';

adopt Lock To achieve serialization

technological process :

  • Case one

    1. client A With client B Each setting is Serializable
    2. client A perform Inquire about id by 1 The sentence of 【 add Row lock 、 Read the lock 、 Clearance lock
    3. client B here to update id by 1 The statement of will Block waiting , to update id by 2 The statement can be executed normally
  • The second case

    1. client A With client B Each setting is Serializable
    2. client B here Insert id by 1 The sentence of 【 add Row lock 、 Write lock
    3. client A perform Inquire about id by 1 The statement of will Block waiting

Lock details

Definition :

Locks are used to control Critical section resources For who

The following lock categories , The database implements Row lock and table lock

Lock classification

  • performance —— Optimism lock and Pessimistic locking

  • Operation type —— Read the lock and Write lock

  • Lock granularity —— Row lock and Table locks and Clearance lock

Various types of locks can be crossed , Not necessarily mutually exclusive

Optimism lock

Definition :

Each thread uses the version number comparison to achieve the goal Critical section resources

Pessimistic locking

Definition :

Each thread vies for the lock Critical section resources

Read the lock

Definition :

Shared lock S

For the same data , If ** Threads A Get the read lock **

Multiple thread read operations can be performed simultaneously , Except for threads A Other thread writes will be blocked .

It's a pessimistic lock

Write lock

Definition :

Exclusive lock X

For the same data , If ** Threads A Get the write lock **

Only the thread A Be able to read and write data , The read and write operations of other threads will be blocked .

Row lock

Definition :

Lock one row of data per operation , Smaller particle size .

classification :

  • Row lock + Read the lock —— Lock the data in this row , Other threads' writes to this row are blocked

    select * from test_innodb_lock where a = 2 lock in share mode;
    
  • Row lock + Write lock —— Lock the data in this row , Other threads' read and write operations to this row are blocked

    select * from test_innodb_lock where a = 2 for update;
    

expenses :

because The row lock should be positioned to a certain row , So it locks slowly , The cost will increase , And there will be a deadlock .

Lock escalation :

InnoDB The row lock of is the lock added for the index , Instead of recording the lock added . If Update the non indexed field or the index becomes invalid , Row locks may be upgraded to table locks .

Line lock analysis :

#  Inquire about InnoDB Row lock contention 
show status like 'innodb_row_lock%';
  • Innodb_row_lock_time_avg ( Average waiting time )

  • Innodb_row_lock_waits ( Total waiting times )

  • Innodb_row_lock_time( The total waiting time )

Optimize according to the situation

Table locks

Definition :

Lock one row of data per operation , The particle size is large , Applicable to whole table data migration .

classification :

  • Table locks + Read the lock —— Lock the table data , Other threads' writes to the table are blocked

    lock table  The name of the table  read;
    
  • Table locks + Write lock —— Lock the table data , Other threads' read and write operations to the table are blocked

    lock table  The name of the table  write;
    

Clearance lock

Definition :

Clearance lock , Is to lock the gap between two lines 【 Section 】 Only in Repeatable To take effect

Example :

 Insert picture description here

Existing interval id by (3,10) (10,20) (20,+∞)

  • stay session1 In the implementation of

    #  UPDATE statement 
    update account set name = 'zhuge' where id > 8 and id <18;
    ## MySQL A clearance lock will be added   Lock the  (8,18)
    ##  because 8 Fall to the original (3,10) The range of ,18 Fall in the (10,20) The range of , So the locked interval is  (3,20]
    
  • Lock according to clearance , Lock the (3,20]

Add a write lock to the interval , other session This interval cannot be inserted 、 Delete 、 Modify the operating

View the related data table of system library lock

##  see INFORMATION_SCHEMA Data table related to system library lock 
#  View transactions 
select * from INFORMATION_SCHEMA.INNODB_TRX;
#  Check the lock 
select * from INFORMATION_SCHEMA.INNODB_LOCKS;
#  View lock wait 
select * from INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
#  Release the lock ,trx_mysql_thread_id It can be downloaded from INNODB_TRX See in the table 
kill trx_mysql_thread_id
#  View lock wait details 
show engine innodb status\G;

summary

  • MyISAM stay DQL front , Will give == The tables involved are read locked .== stay DML It will be given before ** The tables involved are locked .** 【MyISAM Table locks
  • InnoDB stay DQL front ,( Non serial isolation level ) No locks . stay DML It will be given before == The rows involved are read locked .==【InnoDB Row lock
  • When performing updates, try to operate with original values update Table name set Field 1 = Field 1 - 50 where Conditions
  • Data is retrieved by index , Avoid row lock escalation
  • Minimize the range of search conditions , Avoid gap locks
  • ** Control transaction size ,** It may be locked SQL Execute after the transaction
原网站

版权声明
本文为[summer_ forty-five]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280534510922.html