7 how to reduce the impact on the performance of the line lock?

7 how to reduce the impact on the performance of the line lock?

MySQL 's row lock is achieved by each engine tier engines themselves, not all engines support line lock, such as myisam engine does not support row lock, line lock means that do not support concurrency control can only use table locks, for which kind of engine the table, the same table can have only one update at any time in the implementation, which will affect the business concurrency.

This chapter talk innodb row locks, as well as how to improve concurrency by reducing lock conflicts business degree.

Speaking from the two-phase locking

Session a

Session b

begin;

update t set k=k+1 where id=1;

update t set k=k+1 where id=2;

 

 

begin;

update t set k=k+1 where id=1;

commit;

 

Transaction b of the update statement execution time will be what the phenomenon, assuming that id is t of pk

Conclusions this question depends on the transaction A finishing the execution of two update after the statement holds those lock and release at what time, in fact, the transaction b of the update statement will block until the transaction is a statement commit or ROLLBACK , the transaction b able continue.

 

In innodb transaction, the lock is in line only when needed plus, but not not necessary to release immediately, but to wait until the release until the end of the transaction, this is the two-phase locking protocol .

 

If your transaction requires more than one row lock, the lock should most likely to cause conflict, most likely to affect the degree of concurrency lock put back as much as possible .

 

Purported Responsible realize a movie ticket online trading business, customer A to be in the theater B buy movie tickets, to simplify it, this business need to involve the following:

 

--1 from the customer A deduction ticket prices account balance

 

--2 to the theater B account balance increase this movie fare

 

--3 record a transaction log.

 

Also said to be complete this transaction, we need to update two records and a record insert record. Of course, in order to ensure atomic transaction, we need these three operations in one transaction, then, how would you arrange the order of these three statements in a transaction it?

 

Imagine if there is a customer while another C to be in the theater B tickets, then part of the conflict is that both transactions sentence 2 , because they want to update the same theater account balance, you need to modify the same rows of data.

 

According to the two-phase locking protocol, no matter how you arrange the order of statements, all operations required row locks are only released when the transaction is committed, so if the statement 2 on the last, for example, in accordance with 3,1,2 this order ,

So theater account balance lock time row would at least , which minimizes the lock wait between transactions, enhance the degree of concurrency.

 

Now that the right design, the balance of the theater row locks in a transaction will not stay for a long time, but this has not been fully resolved.

 

If the promotional activities, will lead to a large number of concurrent mysql service hang, deadlock detection and deadlock say here

 

Deadlock and deadlock detection

 

When different threads appear circular dependency concurrent systems, threads involved are waiting for the other thread to release resources, which will lead to several threads are waiting to enter the state radio, called deadlock

 

Case

Session a

Session b

begin;

update t set k=k+1 where id=1;

begin;

 

 

update t set k=k+1 where id=2;

update t set k=k+1 where id=2;

 

 

update t set k=k+1 where id=1;

Transaction a waiting affairs b release id = 2 row lock, and the transaction b waiting affairs a release id = 1 line lock, waiting for each other to enter a deadlock state, there has been a deadlock situation, there are two strategies:

--1 direct access to wait until the timeout parameter innodb_lock_wait_timeout

--2 initiate deadlock detection, active roll back a transaction to die in chains, so that other transactions proceed, the parameters innodb_deadlock_detect = ON , indicate on this logic

In innodb in, innodb_lock_wait_timeout default 50s , if this strategy, when the deadlock, the first thread to be locked 50s appear timeout, then other threads can continue to perform, for the online service, this waiting time is unacceptable.

But this innodb_lock_wait_timeout can not set a small value, such as 1S , there has been a deadlock will soon be solved, but if it is not a deadlock, the lock is waiting for it, there will be a lot of friendly fire.

So normally adopt strategy 2 , active surveillance deadlock parameters innodb_deadlock_detect default ON .

However, if a high degree of concurrency, all transactions update the same row, each new thread is blocked will not be judged because of their added led to a deadlock, the time complexity of O (n) operation, assuming there 1000 concurrent threads to simultaneously update the same row,

Then the deadlock detection operation is 100w this magnitude, although the final test result is no deadlock, but it consumes a lot of cpu resources.

Another concurrency control, if we can do concurrency control on the client side, but may be due to many clients, there will be a high concurrency, make the database server, if the middleware, the middleware can be considered achieved if can modify the mysql source,

Also in mysql which do, the basic idea is to update relative to peers, line up before entering the engine, so that innodb there would be a lot of internal work deadlock detection

However, this problem can not optimize the design come from it

Can be considered a line into a plurality of rows of logic to reduce lock conflict to theater account, for example, can be considered on a plurality of records, such as10records, account total is equal to this cinema10value of the sum accounts records so that each modification amount of time to the theater accounts,

Wherein a randomly selected record is modified so that each time the collision probability becomes the original 1/10 , can reduce the number of wait lock, thus reducing the deadlock detection cpu consumption.

This program needs to do the detailed design based on business logic, if there is a refund logic.

Questions :

If you want to delete a table in front of 10,000 rows of data, the following three methods:

--1 executed directly delete from t limit 10000;

--2 in a connection loop is executed 20 times delete from t limit 500;

--3 at 20 while performing the connections delete from t limit 500;

 

Guess you like

Origin www.cnblogs.com/yhq1314/p/11103996.html