45 MySQL combat stress study notes: Q & A article (B): see lock (Lecture 30) with a dynamic view

First, review the locking rules

20 and 21 in the first article, I introduce you to the gap and the lock InnoDB, next-key lock, and locking rules. In these two articles the comments section, there have been many high-quality message. I think these issues through the analysis, can
help you deepen the understanding of locked rules.

So, I pick and choose a few representative questions form the subject of this Q & A article today, namely: Look locked dynamic point of view.

In order to facilitate your understanding, we'll go over it with locking rules. This rule contains two "principles", two "optimization" and a "bug":

Principle 1: The basic unit of locks is the next-key lock. I hope you remember, next-key lock is opened and closed before and after the interval.
Principle 2: Find Objects process will have access to the lock.

Optimization 1: equivalence query on the index, the only index to lock when, next-key lock degenerate into row lock.
Optimization 2: equivalence query on the index, while traversing right and the last condition does not meet the equivalent value of time, next-key lock lock degenerate into space.

A bug: the scope of the inquiry first unique index value does not satisfy the conditions of access to so far.

Next, our discussion is based on the following table t:

CREATE TABLE `t` (
  `id` int(11) NOT NULL,
  `c` int(11) DEFAULT NULL,
  `d` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `c` (`c`)
) ENGINE=InnoDB;

insert into t values(0,0,0),(5,5,5),
(10,10,10),(15,15,15),(20,20,20),(25,25,25);

Second, inequality in conditions equivalent query

Some students on the "equivalence query" raised questions: the equivalent query and "walk" What is the difference? Why examples of our articles inside, where conditions are unequal numbers, this process also has the equivalent query?

Together we look at this example, analyze the scope of this lock of query:

begin;
select * from t where id>9 and id<12 order by id desc for update;

Use locking rules above, we know that this statement is locked range (0,5], (5, 10] and (10, 15). In other words, id = 15 This line on the primary key index, and no is added row lock. Why?

We said locking unit is next-key lock, both before and after the opening and closing zone, but there used to optimize 2, that is the equivalent query on the index, right traversal when id = 15 is not satisfied, the next-key In order to lock the gap degradation lock (10, 15).

However, our query statement where conditions are greater-than and less-than here, "the equivalent of inquiry" has come of it?

You know, locking action takes place in the course of execution of sentence, so when you lock behavior analysis, the data structure on the index from the start. Here, I then look at the dismantling process.

As shown in FIG. 1 is a schematic view of the index table id.

1 a schematic view of FIG index id

1. First, the semantics of this query is the order by id desc, to get all the rows meet the conditions, the optimizer must first find the "first id <12 value."
2. This process is obtained by the process of searching the index tree, inside the engine, in fact, it is to find the value id = 12, but ultimately did not find, but found (10, 15) this gap.
3. Then left traverse, during traversal, a query is not equivalent, the id = 5 scans the line, it will add a next-key lock (0,5].

In other words, in the implementation process, locate records by way of tree search when using the word "equivalent query" approach.

Third, the process of equivalence queries

This corresponds with the above example, is @ Clockwork Orange students raised questions: What locking range following statement is?

begin;
select id from t where c in(5,20,10) lock in share mode;

This query is in use in, explain the results of our first look at this statement.

explain the results of Figure 2 in statement

It can be seen in this statement uses the index and c rows = 3, the three values ​​are described by a B + tree search target.

In the Find c = 5 when the first locked (0,5]. But because c is not the only index, in order to confirm that there is no other record c = 5, you must traverse to the right, find c = 10 was not confirmed , this optimization process satisfies 2, so the addition of between
gap lock (5,10).

Similarly, when performing this logic c = 10, the locking range is (5, 10] and (10, 15); c = 20 when performing this logic, the locking range (15, 20] and ( 20, 25).

Through this analysis, we can know that this statement on the index c plus three record locking order is: first record lock plus c = 5, plus record locking c = 10, and finally add record c = 20 lock.

You might say that this lock range, is not removed from the (5,25) c = row locks 15 do? Why bother segmented say?

Because I'll tell you emphasize this process: These locks are "in the process of executing a plus in a" rather than a one-time additions.

Once you understand this locking procedure, since we can analyze the deadlock problem in the following example.

If there is another statement at the same time, it is so written:

select id from t where c in(5,20,10) order by c desc for update;

At this point the lock range, what is it?

We now know that the gap is not interlocked lock, but these two statements will c on the index c = 5,10 plus record locking on this three rows.

Here you need to look at, because the statement which is order by c desc, lock the order of these three record locking, and is the first lock c = 20, then c = 10, and finally c = 5.

In other words, these two statements the same resources to be locked, but the lock reverse order. When the concurrent execution of these two statements, a deadlock may occur.

Information about the deadlock, MySQL only preserves the deadlock of the last scene, but the scene is still incomplete.

Some students in the comments area to leave a message, I hope I can expand it to see how the deadlock. Now, I have a simple analysis of the deadlock in the field above example.

Fourth, how to see the deadlock?

FIG 3 is in the deadlock, the operative part show engine innodb status output command obtained. This command
will output a lot of information, there is a LATESTDETECTED DEADLOCK, is the last letter of deadlock recorded
information.

3 Deadlock site

Let's look at a few key information in this figure.

1. The result is divided into three parts:

1, TRANSACTION, is the first transaction information;

2, TRANSACTION, second transaction information;

3, WE ROLL BACK TRANSACTION (1), is the final processing result indicating the first transaction is rolled back.

2. Information of a transaction in which:

  • WAITING FOR THIS LOCK TO BE GRANTED, indicate that this transaction lock information in waiting;
  • index c of table `test`.`t`, etc., are described in the index table t c above the lock;
  • lock mode S waiting to add their own statements indicate that a read lock, the current state is waiting;
  • Record lock Description This is a record locking;
  • n_fields 2 indicates that this record is two, i.e. primary key field and c field ID;
  • 0: len 4; hex 0000000a; asc ;; the first field, i.e. c. Hexadecimal value a, which is 10;
  • 1: len 4; hex 0000000a; asc ;; a second field, which is the primary key id, value is 10;
  • These two lines inside asc indicated that the next step is to print out the value inside the "printable characters", but 10 is not a printable character, and therefore display space.
  • The first transaction information to only show the status of the lock, waiting (c = 10, id = 10) in this line lock.
  • Of course, you know, since the deadlock appeared to be good, this transaction also has other locks, but did not show up. Do not worry, we deduce from the information in the second transaction

3. The second transaction information displayed to be more:

  • "HOLDS THE LOCK (S)" to display what locks held by the transaction;
  • index c of table `test`.`t` lock is represented in the table index t C;
  • hex 0000000a and indicates that the transaction holds hex 00000014 c = 10 c = 20 and lock the two records;
  • WAITING FOR THIS LOCK TO BE GRANTED, represents the like (c = 5, id = 5) the lock record.

From the information above, we know:

1. "lock in share mode" of this statement, record locks held c = 5, c = the lock 10 and the like;
this statement holds records c 20 and c = 2. "for update" = 10 to locks, record locks waiting c = 5.

Thus leading to a deadlock. Here, we can get two conclusions:

1. Since the lock is a plus, to avoid deadlock, the same set of resources, according to try to access the same order;
2. deadlock at the moment, for update this statement occupy more resources, rollback greater cost, so choose the InnoDB to roll back the cost of a smaller lock in share mode statement to be rolled back.

Fifth, look at how the lock wait?

After reading a deadlock, we will look at an example of a lock wait.

In the first 21 articles of the comments area, @ Geek_9ca34e students made an interesting verification, I reproduce the steps listed:

4 delete gap variation results in FIG.

It can be seen that, since the session A and c = 10 does not lock the records, session B to delete the line id = 10 is possible. But then, session B insert id = 10 think this line to go back to die.

Now let's look at the results show engine innodb status at this time, and see if you can give us some tips. Lock information is the output of this command TRANSACTIONS in this section. You can see this picture in the presentation

FIG lock wait information 5

We look at a few key messages.

1. index PRIMARY of table `test`.`t`, indicates that the statement is locked because of a lock on a primary key table t.

2. lock_mode X locks gap before rec insert intention waiting here are a few information:

  • insert intention indicates that the current thread is ready to insert a record, which is inserted into a lock intention. For ease of understanding, you can think of it is this insertion operation itself.
  • gap before rec indicates that this is a lock space, instead of record locking.

3. Well, this gap which is before recording it? The next five lines 0-4 of the contents of this information is recorded.

4. n_fields 5 also shows, this record has a 5:

  • insert intention indicates that the current thread is ready to insert a record, which is inserted into a lock intention. For ease of understanding, you can think of it is this insertion operation itself.
  • gap before rec indicates that this is a lock space, instead of record locking
  • 0: len 4; hex 0000000f; asc ;; first column is the primary key id field, f is the hexadecimal id = 15. So, then we will know, before this gap is id = 15, since id = 10 does not exist, it is indicated (5,15).
  • 1: len 6; hex 000000000513; asc ;; second column having a length of 6 bytes transaction id, this line indicates that the last modified trx id 1299 transaction.
  • 2: len 7; hex b0000001250134; asc% 4 ;; third column length of 7 bytes rollback information. It can be seen behind the acs where a display content (%, and 4), because it is exactly this byte is a printable character. Followed by two values ​​of c and d, are 15.

So, we know, because the delete operation to delete the line id = 10, the original two gaps (5, 10), (10, 15) into a (5, 15).

Here, you can join together and then think about the link between these two phenomena:

1. session A complete implementation of the select statement, did not do anything, but it's locked range suddenly "become big";
after-school 2. Article 21 of the article thinking questions, when we execute select * from t where c> = 15 and c <= 20order by c desc lock in share mode; when scanning left to c = 10, make (5, 10] lock up

In other words, the so-called "gap", in fact, simply a "right of that record this gap" definition.

Six, update examples

Seen lock insert and delete example, we look at a case statement update. @ Letter in the message area in the letter the students to do this test:

6 update the example of FIG.

You can analyze yourself, lock session A range is (5,10 on the index c], (10,15], (15,20], (20,25] and (25, supremum]. After the session B the first update statement should be changed c = 5 c = 1, you can be understood as two steps:

1. Insert (c = 1, id = 5 ) This record;
2. Delete (c = 5, id = 5 ) this record.

According to our last one to say, the index c (5,10) clearance by the right of the record gap, which is defined by c = 10. Therefore, by this operation, the lock range becomes like session A shown in Figure 7:
Note: The first record c> 5 found that c = 10, and therefore will not be added (0,5] The next -key lock.

Figure 7 session B modifications, in the lock range session A


Well, then session B to perform update t set c = 5 where c = 1 the statement, and as can be split into two steps:

1. Insert (c = 5, id = 5 ) This record;
2. Delete (c = 1, id = 5) this record.


The first step has been attempted to add a lock in the gap (1,10) into the data, it was blocked.

VII Summary

Today this article, I used a few questions before the first 20 and second 21 article comments area, once again review the rules with your lock. And you and I highlights, analysis locking range, we must cooperate to perform a logical statement.

In my opinion, each would like to seriously understand the principles of MySQL students should be able to do: through the results explain, the brain will be able to fill out the execution flow of a SQL statement. To such an extent, it is considered as an index-organized table, cable
lead, lock concepts have a more clear understanding. You can also use this method to validate your mastery of the knowledge points.

In analyzing these locking rules, the way I described how to see transaction information and deadlock information show engine innodb status output results with you and hope this site for later analysis you can help.

The old rules, even if the Q & A article, I still have to stay after class a question for you.

Above we mentioned a very important point: the so-called "gap", in fact, simply by the definition of "this and that record to the right of the gap."

Then, an empty table there is a gap it? This gap is defined by whom? How do you verify this conclusion?

You can put on your analysis and verification methods written in the comments section, I will end next article and you discuss this issue. Thank you for listening, you are welcome to send this share to more friends to read together.

Eight, the issue of time

I left the last question on the article is to share your experience with business process monitoring.

In the comments section of this article, many students have to share a good experience. Here, I chose a few typical messages, and you share it:

@ Lao Yang comrades to answer it in detail. His main idea is to monitor the status on service and quality of service. Wherein, the monitoring service state, can generally be achieved with an external system; and monitor the quality of service, it is necessary through the interface
to count the response time.

@Ryoma students mentioned services are used healthCheck to detect, in fact, similar to the select mode mentioned herein with us 1.

@ Strong brother classmate, in accordance with the object of surveillance, monitoring will be divided into the basic monitoring, service monitoring and service monitoring, and shared objects need to focus on each monitor.

These are all very good experience, you can also draw their own programs based on specific business scenarios.

Guess you like

Origin www.cnblogs.com/luoahong/p/11732124.html