当前位置:网站首页>Tell you about mvcc sequel

Tell you about mvcc sequel

2022-06-25 02:46:00 InfoQ

The last one talked about mysql  Of  innodb  Based on the engine  read view  Realized  mvcc  And transaction isolation level , Some careful friends may find some problems , The first is in  RC  Visibility after transaction submission at level , Three parameters are involved here ,m_low_limit_id,m_up_limit_id,m_ids, I saw a very good article written by Zhihu before , But there seems to be some doubt on this point , Here we will explain this problem based on the source code and comments

/**
Opens a read view where exactly the transactions serialized before this
point in time are seen in the view.
@param id Creator transaction id */

void ReadView::prepare(trx_id_t id) {
 ut_ad(mutex_own(&trx_sys->mutex));

 m_creator_trx_id = id;

 m_low_limit_no = m_low_limit_id = m_up_limit_id = trx_sys->max_trx_id;

m_low_limit_id The assigned value is trx_sys->max_trx_id, Represents the smallest unallocated transaction in the current system  id, So , for instance , There are currently three active transactions , Business  id  Namely  100,200,300, and  m_up_limit_id = 100, m_low_limit_id = 301, When a transaction  id  yes  200  After the submission of , Its update is that it can be  100  and  300  notice , Rather than say  m_ids  It's gone  200, also  200  Than  100  Big should be invisible

Fantasy reading

There is also a problem of unreal reading , This seems to be a high-frequency interview question , What do you mean , Or the dirty reading that is most often compared with it , Dirty reading refers to reading uncommitted data from other transactions , Because it was not submitted , Strictly speaking , It doesn't have to end up in the library , It may roll back , That is to say  read uncommitted  Problems that will occur at level , But unreal reading is different , Unreal reading means that the number of results of two queries is different , For example, the first time I checked was  
select * from table1 where id < 10 for update
, I found a result  id  yes  5, Then I checked again and found one  id  yes  5, One  id  yes  7, Is that a bit awkward , In fact, I think dirty reading and phantom reading are also named from the principle level , It's normal if the first contact students find that they don't understand , Because logically speaking, the data does not meet expectations , But based on the source code level, it is actually different , Unreal reading is in the original  read view  Cannot be completely solved , How to solve it , It's simply a lock , We know innodb  Is based on  record lock  Row lock , But there seems to be no way to solve this problem , that  innodb  It's introduced  gap lock  Clearance lock , For example, in the case mentioned above ,id  Less than  10  Under the circumstances , Should be locked ,gap lock  In fact, the lock is based on the index structure , Because the index tree has a tree structure , One more next record  The pointer to ,gap lock  It's also based on this to lock  mysql  Documents

SELECT ... FOR UPDATE sets an exclusive next-key lock on every record the search encounters. However, only an index record lock is required for statements that lock rows using a unique index to search for a unique row.

For one  for update  Inquire about , stay  RR  Below grade , It will set up a  next-key lock On every record queried ,next-lock  What is it , In fact, that is  gap  Lock and  record  A combination of locks , For example, I have in the database  id  yes  1,3,5,7,10, For the above query , The result is  1,3,5,7, Then follow the description in the document , For these records, we will add next-key lock, That is to say (-∞, 1], (1, 3], (3, 5], (5, 7], (7, 10)  These intervals and records will be locked , Do not let insert , Tell me more , In fact, if it is a read-only transaction , light  read view  Consistent reading is enough , If there is a write operation , You need a lock .

This article USES the
A signature  4.0  The international  (CC BY 4.0)
license agreement , Welcome to reprint 、 Or modify the use of , But you need to indicate the source . 
The author of this article : 
Nicksxs
 
Creation time : 2020-05-02 
Link to this article : 
I'll explain it to you  MVCC  sequel
 
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242318166787.html