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

Tell you about mvcc

2022-06-24 03:48:00 InfoQ

Long ago , An interviewer asked , You know,  mysql  Transaction isolation level ,“ forehead  O__O …, Don't know much about it ”, After that, I went to the Internet to find relevant articles , Found this
MySQL  Description of four transaction isolation levels
,  The article is very well written , After reading this, you can understand the transaction isolation levels , However, after reading this, if you think more about it, you will still find problems , How is such a magical level of transaction isolation achieved

among  innodb  The transaction isolation of the uses the  mvcc,
Multiversion concurrency control
,  Multi version concurrency control , Let's not talk about what this is , Consider if pure speculation , How should this transaction isolation level be implemented , Stupid I thought , You can copy a table at the beginning of a transaction , This can support  RR  Level ,RC  Level is not supported , And if it's a very big watch , It is not feasible to think about it

He said with a shy face that although this was not feasible , But the idea is right , A series of measures should be taken to implement it ( Fat intestines ) The changes to the , First of all, according to my understanding , In fact, copying a table becomes copying a record , But if there are multiple transactions , You have to copy it many times , This problem can be explained with the help of version management system , Version management system in use ,git  And so on , Very primitive may be after developing a function , Just make a compressed package and name it with time and other information , Then, if you want to retrieve this later, you can directly use the compressed package , Then there was.  svn,git  Centralized and distributed version management system , One of its characteristics is that the granularity can be controlled to the file and code line level , Corresponding to our  mysql  Can transactions also be refined from the table level expected at the beginning to the row level , Maybe many people have known it before , In addition to our user-defined fields, a row of records in the database , There are some additional fields , To source code
data0type.h
Fish in

/* Precise data types for system columns and the length of those columns;
NOTE: the values must run from 0 up in the order given! All codes must
be less than 256 */
#define DATA_ROW_ID 0 /* row id: a 48-bit integer */
#define DATA_ROW_ID_LEN 6 /* stored length for row id */

/** Transaction id: 6 bytes */
constexpr size_t DATA_TRX_ID = 1;

/** Transaction ID type size in bytes. */
constexpr size_t DATA_TRX_ID_LEN = 6;

/** Rollback data pointer: 7 bytes */
constexpr size_t DATA_ROLL_PTR = 2;

/** Rollback data pointer type size in bytes. */
constexpr size_t DATA_ROLL_PTR_LEN = 7;

One is  
DATA_ROW_ID
, This is to generate a hidden when the data does not specify a primary key , If the user specifies a primary key, it is the primary key

One is  
DATA_TRX_ID
, This represents the transaction of this record  ID

The other one is  
DATA_ROLL_PTR
  Pointer to rollback segment

The rollback segment pointed to is actually what we often say  undo log, The specific structure is a linked list , stay  mvcc  This will be used in , And this is it  
DATA_TRX_ID
, Each record records this transaction  ID, It indicates which transaction modified the current value of this record , Let's get back to business , We know  
Read Uncommitted
,  In fact, isolation is not necessary , Just read the current value directly , here we are  
Read Committed
  Level , We want the transaction to read the committed value ,mysql  Used a call  
read view
  The gadget , It has these values that we should pay attention to ,

m_low_limit_id
,  This is  read view  The largest active transaction at creation  id

m_up_limit_id
,  This is  read view  The smallest active transaction at creation  id

m_ids
,  This is  read view  All active transactions at creation  id  Array

m_creator_trx_id  This is the creation transaction of the current record  id

The main logic for determining the visibility of transactions is as follows ,

  • When recorded transactions  
    id
      Less than the minimum active transaction  id, The description is visible ,
  • If the recorded transaction  
    id
      Equal to the current transaction  id, The description is your own change , so
  • If the recorded transaction  
    id
      Greater than the largest active transaction  
    id
    ,  invisible
  • If the recorded transaction  
    id
      Be situated between  
    m_low_limit_id
      and  
    m_up_limit_id
      Between , It is necessary to judge whether it is  
    m_ids
      in , If in , invisible , If not , Indicates that... Has been submitted , See the specific
    Code
    Take a look

/** Check whether the changes by id are visible.
 @param[in] id transaction id to check against the view
 @param[in] name table name
 @return whether the view sees the modifications of id. */
 bool changes_visible(trx_id_t id, const table_name_t &name) const
 MY_ATTRIBUTE((warn_unused_result)) {
 ut_ad(id > 0);

 if (id < m_up_limit_id || id == m_creator_trx_id) {
 return (true);
 }

 check_trx_id_sanity(id, name);

 if (id >= m_low_limit_id) {
 return (false);

 } else if (m_ids.empty()) {
 return (true);
 }

 const ids_t::value_type *p = m_ids.data();

 return (!std::binary_search(p, p + m_ids.size(), id));
 }

What is the rest , Namely  
Read Committed
  and  
Repeated Read
  Are not the same as , That's what I said before  
read view
  Can you support it , How to support it , If the  
read view
  Is created at the beginning of the transaction , That seems to support only  RR  Transaction isolation level , Actually , This is done by creating  
read view
The timing of , about  RR  Level , In the first place of the transaction  
select
  Statement is to create , about  RC  Level , Is at the end of each  
select
  Statements are created once before execution , That will ensure that all submitted data can be read

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-04-26 
Link to this article : 
I'll explain it to you  MVCC
原网站

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