MySql-locks and things

lock

Introduction to locks

Why do I need a lock?

When you buy a product on Taobao, there is only one product in stock. If another person buys it at this time, how to solve the problem of whether you bought it or another person bought it?

The concept of lock

  • A lock is a mechanism for a computer to coordinate multiple processes or threads to concurrently access a certain resource.
  • In a database, data is also a resource shared by many users. How to ensure the consistency and effectiveness of concurrent data access is a problem that all databases must solve, and lock conflicts are also an important factor affecting the performance of concurrent database access.
  • Locks are especially important for databases, and they are more complicated.

Locks in MySQL

  • Table-level locks: low overhead and fast locking; no deadlocks; large locking granularity, the highest probability of lock conflicts, and the lowest concurrency.
  • Row-level locks: high overhead and slow locking; deadlocks will occur; locking granularity is the smallest, the probability of lock conflicts is the lowest, and the concurrency is the highest.
  • Page locks (gap locks, gap locks): The overhead and lock time are between table locks and row locks; deadlocks will occur; locking granularity is between table locks and row locks, and the degree of concurrency is average.

Table lock and row lock usage scenarios

Table-level locks are more suitable for applications that focus on queries and only a small amount of data is updated according to index conditions. For example, OLAP system
row-level locks are more suitable for applications that have a large number of concurrent updates of a small amount of different data according to index conditions and concurrent queries. Such as some online transaction processing (OLTP) systems.
It is difficult to say which lock is better in general, only which lock is more suitable in terms of the characteristics of specific applications

MyISAM lock

MySQL's table-level lock has two modes:
table shared read lock (Table Read Lock)
table exclusive write lock (Table Write Lock)
Insert picture description here

Shared read lock

Syntax: lock table table name read

  1. lock table testmysam READStart another session select * from testmysamto query
  2. insert into testmysam value(2); update testmysam set id=2 where id=1;Given
    3. In another session of
    insert into testmysam value(2);waiting
    4. At the same session of a
    insert into account value(4,'aa',123);given
    select * from account ;error
    5. In another session in
    insert into account value(4,'aa',123);successful
    6. In the same session lock select s.* from testmysam serror
    lock table alias table as Read;

Exclusive photo chain

1. lock table testmysam WRITE
In the same session

insert testmysam value(3);
delete from testmysam where id = 3 
select * from testmysam 

2. Operate on different tables (error report)

select s.* from testmysam s 
insert into account value(4,'aa',123); 

3. In other sessions (waiting)

select * from testmysam

to sum up:

  • Read locks, read operations on MyISAM tables, will not block other users' read requests to the same table, but will block write requests to the same table
  • Read lock, read operations on MyISAM tables, will not block the current session to read the table, when the table is modified, an error will be reported
  • Read lock, a session uses the LOCK TABLE command to add a read lock to table f. This session can query the records in the locked table, but updates or access to other tables will prompt errors;
  • Write lock, write operation to MyISAM table, it will block other users from reading and writing the same table;
  • Write lock, write operations to MyISAM tables, the current session can do CRUD on this table, but operations on other tables will report errors

InnoDB lock

InnoDB engine in mysql supports row lock

  • Shared locks are also called: read locks. When a transaction locks a read lock on certain rows, other transactions are allowed to read these rows, but they are not allowed to write operations, nor are other transactions allowed to lock these rows exclusively, but read locks are allowed .
  • Exclusive lock is also called: write lock. When a transaction locks a certain number of writes, other transactions are not allowed to write, but are allowed to read. It is not allowed for other transactions to place any locks on these rows. Including write locks.

grammar

The wording of the shared lock: lock in share mode
For example: The wording select * from 表 where 条件 lock in share mode;
of the exclusive lock: for update
For example:select * from 表 where 条件 for update;

note:

1. Two transactions cannot lock the same index.
2. Insert, delete, and update will automatically add exclusive locks by default in the transaction.
3. Row lock must have an index to achieve, otherwise it will automatically lock the entire table, then it is not a row lock.

  CREATE TABLE testdemo ( 
  `id` int(255) NOT NULL , 
  `c1` varchar(300) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , 
  `c2` int(50) NULL DEFAULT NULL , 
  PRIMARY KEY (`id`), INDEX `idx_c2` (`c2`) USING BTREE )ENGINE=InnoDB; 
  insert into testdemo VALUES(1,'1',1),(2,'2',2);
  1. BEGIN select * from testdemo where id =1 for update
    In another session in update testdemo set c1 = '1' where id = 2the success of
    update testdemo set c1 = '1' where id = 1waiting
  2. BEGIN update testdemo set c1 = '1' where id = 1
    In another session of update testdemo set c1 = '1' where id = 1waiting
  3. BEGIN update testdemo set c1 = '1' where c1 = '1'
    In another session of update testdemo set c1 = '2' where c1 = '2'waiting
  4. The first session of the select * from testdemo where id =1 for update
    second session select * from testdemo where id =1 lock in share mode
    back to the first session UNLOCK TABLES will not unlock
    the commit or begin or ROLLBACK will unlock
  5. Look at the following table. Lock
    table testdemo WRITE
    use commit, ROLLBACK will not unlock.
    Use UNLOCK TABLES or begin will unlock

Lock wait problem

Well, after mastering the above, your understanding of locks has surpassed that of many classmates. Now, a practical problem is that one piece of data is often locked during work, causing other operations to be completely impossible to proceed.

You must have encountered this problem. Some programmers often lock part of the database data when debugging a program. At this time, you also need to debug this part of the function, but you find that the code always runs overtime. Have you ever encountered it? This is a problem. In fact, I believe you also know the root cause of this problem.
For example, there are two sessions.
Programmer A, the integrity debugging code
BEGIN
SELECT * FROM testdemo WHERE id = 1 FOR UPDATE
The function you have completed with integrity must also pass through that part of the code, you have to read the lock
BEGIN
SELECT * FROM testdemo WHERE id = 1 lock in share mode

Unfortunately at this time, you don’t know what happened, and it will always be a timeout exception during your debugging process, and this kind of problem may be encountered no matter in development or in actual project operation, so how to troubleshoot What about this problem? This is actually a small trick.

select * from information_schema.INNODB_LOCKS;

Insert picture description here
It’s great. I at least found that there are two locks for the same data in the same table. One of them is X (write lock) and the other is S (read lock). I can skip this piece of data. Use other data for debugging.
Maybe if I can't get around, I must use this data? Give a shout (Which wicked person is on the debug table, please don’t lock it), well, this is a joke, there is actually a better way to look at it

select * from sys.innodb_lock_waits

Insert picture description here
The sql statement I executed now has it, and look at the bottom, the kill command. You can completely kill the blocked sql statement through the kill bar in your work, and you can continue to run it, but you should pay attention to the use of this command. However, if a colleague is doing more important debugging and you kill him, he may be beaten up if he finds out.

The above solution is good, but what if your MySQL is not version 5.7? It's 5.6. You don't have a sys library at all. It's difficult to do this at this time, but there are ways.

SELECT r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
r.trx_query waiting_query, 
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread 
FROM 
information_schema.innodb_lock_waits w 
INNER JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id 
INNER JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id;

Insert picture description here
See if you haven't seen it, can you also perform a big move like kill 29?

Affairs

Many software nowadays are multi-user, multi-program, multi-threaded, and there may be many people using the same table at the same time. In order to maintain data consistency, the concept of transaction is proposed.

A has to transfer money to B, A’s account is -1000 yuan, and B’s account is +1000 yuan. These two update statements must be executed as a whole, otherwise it is difficult for A to deduct money and B does not add money. deal with.

What storage engine supports transactions

  1. Check whether transactions are supported under the database (InnoDB support)?
show engines;
  1. Check the current default storage engine of mysql?
 show variables like '%storage_engine%'; 
  1. Check the storage engine of a table?
  show create table 表名 ; 
  1. For the modification of the storage structure of the table?
    Create an InnoDB table: Create table… type=InnoDB; Alter table table_name type=InnoDB;

Transaction characteristics

Transactions should have 4 attributes: atomicity, consistency, isolation, and durability. These four attributes are commonly referred to as ACID characteristics.

  • Atomicity
  • Consistency
  • Isolation
  • Durability

Atomicity

A transaction must be regarded as an indivisible minimum unit. All operations in the entire transaction are either submitted successfully or failed. For a transaction, it is impossible to perform only a part of the operations.
Xiao Ming gives Xiao Hong money
1. Xiaoming bank card deducts 500 yuan
2 Xiaohong bank card increases by 500. The
entire transaction either all succeeds or all fails.

Consistency

Consistency means that the transaction transforms the database from one consistency to another consistency state. The integrity of the data in the database is not destroyed before the transaction starts and after the transaction ends.
Xiaohong gives Xiaoming money
1. Xiaohong bank card Deduct 500
2. Add 500 to
Xiao Ming's bank card 2. Add 1000 to Xiao Ming's bank card. The
deducted money (-500) and the added money (500) should add up to 0

Durability

Once the transaction is committed, the changes made will be permanently saved in the database. At this point, even if the system crashes, the modified data that has been submitted will not be lost. It is
not completely solved from the perspective of the database.

Isolation

The execution of a transaction cannot be interfered by other transactions. That is, the internal operations of a transaction and the data used are isolated from other concurrent transactions, and each transaction executed concurrently cannot interfere with each other. (Parallel execution of the database should be like serial execution)

  • Uncommitted read (READ UNCOMMITED) dirty read
  • READ COMMITED cannot be read repeatedly
  • Repeatable read (REPEATABLE READ)
  • Serializable (SERIALIZABLE) The
    default transaction isolation level of mysql is repeatable-read
show variables like '%tx_isolation%';

Transaction concurrency

  • Dirty read: Transaction A reads the updated data of transaction B, and then B rolls back the operation, then the data read by A is dirty data
  • Non-repeatable read: Transaction A reads the same data multiple times, and transaction B updates and commits the data during transaction A's multiple reads. As a result, when transaction A reads the same data multiple times, the results are inconsistent.
  • Phantom reading: System administrator A changed the scores of all students in the database from specific scores to ABCDE grades, but system administrator B inserted a record of specific scores at this time. When system administrator A finished the change, he found that there was still If a record is not changed, it is as if an illusion has occurred, which is called a phantom reading.

It is easy to confuse non-repeatable reading and phantom reading. Non-repeatable reading focuses on modification , and phantom reading focuses on adding or deleting. To solve the problem of non-repeatable read, you only need to lock the rows that meet the conditions, and to solve the phantom read, you need to lock the table

Uncommitted read (READ UNCOMMITED) dirty read

show variables like '%tx_isolation%'; 
set SESSION TRANSACTION ISOLATION LEVEL read UNCOMMITTED;

In a session

start TRANSACTION 
update account set balance = balance -50 where id = 1

Query in another session

select * from account

Roll back the transaction in the first session

ROLLBACK
In the second session

select * from account

The submitted data is read in another session, and this part of the data is dirty data

READ COMMITED cannot be read repeatedly

show variables like '%tx_isolation%';
 set SESSION TRANSACTION ISOLATION LEVEL read committed;

In a session

start TRANSACTION 
update account set balance = balance -50 where id = 1

Query in another session (data has not changed)

select * from account

Go back to the first session and roll back the transaction
commit
in the second session
select * from account(data has changed)

Repeatable read (REPEATABLE READ)

show variables like '%tx_isolation%';
 set SESSION TRANSACTION ISOLATION LEVEL repeatable read;

In a session

start TRANSACTION 
update account set balance = balance -50 where id = 1

Query in another session (data has not changed)

select * from account

Roll back the transaction in the first session

commit

In the second session
select * from account(data has not changed)

Serializable (SERIALIZABLE)

show variables like '%tx_isolation%'; 
set SESSION TRANSACTION ISOLATION LEVEL serializable;

1. Start a transaction

begin select * from account #发现 3 条记录

2. Start another transaction

begin select * from account #发现 3 条记录 也是 3 条记录
insert into account VALUES(4,'deer',500) #发现根本就不让插入
  1. Go back to the first transaction commit

Guess you like

Origin blog.csdn.net/weixin_42292697/article/details/114081064