MVCC原理探究及MySQL源码实现分析 - Go语言中文社区

MVCC原理探究及MySQL源码实现分析


MVCC原理探究及MySQL源码实现分析

数据库多版本读场景

session 1 session 2
select a from test; return a = 10  
start transaction;  
update test set a = 20;  
  start transaction;
  select a from test; return ?
commit;  
  select a from test; return ?

我们看下上面这个数据库日常操作的例子。

  • session 1修改了一条记录,没有提交;与此同时,session 2 来查询这条记录,这时候返回记录应该是多少呢?
  • session 1 提交之后 session 2 查询出来的又应该是多少呢?

由于MySQL支持多种隔离级别,这个问题是需要看session2的事务隔离级别的,情况如下:

  • 隔离级别为 READ-UNCOMMITTED 情况下: 
    session 1 commit前后 session 2 去查看都会看到的是修改后的结果 a = 20
  • 隔离级别为 READ-COMMITTED 情况下: 
    session 1 commit 前查看到的还是 a =10 , commit之后看到的是 a = 20
  • 隔离级别为 REPEATABLE-READ, SERIALIZABLE 情况下: 
    session 1 commit前后 session 2 去查看都会看到的是修改后的结果 a = 10

其实不管隔离级别,我们也抛开数据库中的ACID,我们思考一个问题:众所周知,InnoDB的数据都是存储在B-tree里面的,修改后的数据到底要不要存储在实际的B-tree叶子节点,session2是怎么做到查询出来的结果还是10,而不是20列?

MVCC实现原理

上述现象在数据库中大家经常看到,但是数据库到底是怎么实现的,深究的人就不多了。 
其实原理很简单,数据库就是通过UNDO和MVCC来实现的。

通过DB_ROLL_PT 回溯查找数据历史版本

  • 首先InnoDB每一行数据还有一个DB_ROLL_PT的回滚指针,用于指向该行修改前的上一个历史版本 

    当插入的是一条新数据时,记录上对应的回滚段指针为NULL


更新记录时,原记录将被放入到undo表空间中,并通过DB_ROLL_PT指向该记录。session2查询返回的未修改数据就是从这个undo中返回的。MySQL就是根据记录上的回滚段指针及事务ID判断记录是否可见,如果不可见继续按照DB_ROLL_PT继续回溯查找。

通过read view判断行记录是否可见

具体的判断流程如下:

  • RR隔离级别下,在每个事务开始的时候,会将当前系统中的所有的活跃事务拷贝到一个列表中(read view)
  • RC隔离级别下,在每个语句开始的时候,会将当前系统中的所有的活跃事务拷贝到一个列表中(read view) 
    并按照以下逻辑判断事务的可见性。 

MVCC解决了什么问题

  • MVCC使得数据库读不会对数据加锁,select不会加锁,提高了数据库的并发处理能力
  • 借助MVCC,数据库可以实现RC,RR等隔离级别,用户可以查看当前数据的前一个或者前几个历史版本。保证了ACID中的I-隔离性。

MySQL代码分析

前面我们介绍了什么是MVCC,以及它解决了什么问题。 
下面我们来看一下在MySQL源码中,到底是怎么实现这个逻辑的。

InnoDB隐藏字段源码分析

InnoDB表中会存有三个隐藏字段,这三个字段是mysql默认帮我们添加的。我们可以通过代码中查看到:

  1. dict_table_add_system_columns(
  2. /*==========================*/
  3. dict_table_t* table, /*!< in/out: table */
  4. mem_heap_t* heap) /*!< in: temporary heap */
  5. {
  6. ut_ad(table);
  7. ut_ad(table->n_def == (table->n_cols - table->get_n_sys_cols()));
  8. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  9. ut_ad(!table->cached);
  10. /* NOTE: the system columns MUST be added in the following order
  11. (so that they can be indexed by the numerical value of DATA_ROW_ID,
  12. etc.) and as the last columns of the table memory object.
  13. The clustered index will not always physically contain all system
  14. columns.
  15. Intrinsic table don't need DB_ROLL_PTR as UNDO logging is turned off
  16. for these tables. */
  17. dict_mem_table_add_col(table, heap, "DB_ROW_ID", DATA_SYS,
  18. DATA_ROW_ID | DATA_NOT_NULL,
  19. DATA_ROW_ID_LEN);
  20. #if (DATA_ITT_N_SYS_COLS != 2)
  21. #error "DATA_ITT_N_SYS_COLS != 2"
  22. #endif
  23. #if DATA_ROW_ID != 0
  24. #error "DATA_ROW_ID != 0"
  25. #endif
  26. dict_mem_table_add_col(table, heap, "DB_TRX_ID", DATA_SYS,
  27. DATA_TRX_ID | DATA_NOT_NULL,
  28. DATA_TRX_ID_LEN);
  29. #if DATA_TRX_ID != 1
  30. #error "DATA_TRX_ID != 1"
  31. #endif
  32. if (!table->is_intrinsic()) {
  33. dict_mem_table_add_col(table, heap, "DB_ROLL_PTR", DATA_SYS,
  34. DATA_ROLL_PTR | DATA_NOT_NULL,
  35. DATA_ROLL_PTR_LEN);
  36. #if DATA_ROLL_PTR != 2
  37. #error "DATA_ROLL_PTR != 2"
  38. #endif
  39. /* This check reminds that if a new system column is added to
  40. the program, it should be dealt with here */
  41. #if DATA_N_SYS_COLS != 3
  42. #error "DATA_N_SYS_COLS != 3"
  43. #endif
  44. }
  45. }
  • DB_ROW_ID:如果表中没有显示定义主键或者没有唯一索引则MySQL会自动创建一个6字节的row id存在记录中
  • DB_TRX_ID:事务ID
  • DB_ROLL_PTR:回滚段指针

InnoDB判断事务可见性源码分析

mysql中并不是根据事务的事务ID进行比较判断记录是否可见,而是根据每一行记录上的事务ID进行比较来判断记录是否可见。

我们可以通过实验验证 , 创建一张表里面插入一条记录

  1. dhy@10.16.70.190:3306 12:25:47 [dhy]>select * from dhytest;
  2. +------+
  3. | id |
  4. +------+
  5. | 10 |
  6. +------+
  7. 1 row in set (7.99 sec)

手工开启一个事务 更新一条记录 但是并不提交:

  1. dhy@10.10.80.199:3306 15:28:24 [dhy]>update dhytest set id = 20;
  2. Query OK, 3 rows affected (40.71 sec)
  3. Rows matched: 3 Changed: 3 Warnings: 0

在另外一个会话执行查询

  1. dhy@10.16.70.190:3306 12:38:33 [dhy]>select * from dhytest;

这时我们可以跟踪调试mysql 查看他是怎么判断记录的看见性,中间函数调用太多列举最重要部分

这里需要介绍一个重要的类 ReadView,Read View是事务开启时当前所有事务的一个集合,这个类中存储了当前Read View中最大事务ID及最小事务ID

  1. /** The read should not see any transaction with trx id >= this
  2. value. In other words, this is the "high water mark". */
  3. trx_id_t m_low_limit_id;
  4. /** The read should see all trx ids which are strictly
  5. smaller (<) than this value. In other words, this is the
  6. low water mark". */
  7. trx_id_t m_up_limit_id;
  8. /** trx id of creating transaction, set to TRX_ID_MAX for free
  9. views. */
  10. trx_id_t m_creator_trx_id;

当我们执行上面的查询语句时,跟踪到主要函数如下:

  1. 函数row_search_mvcc->lock_clust_rec_cons_read_sees
  2. bool
  3. lock_clust_rec_cons_read_sees(
  4. /*==========================*/
  5. const rec_t* rec, /*!< in: user record which should be read or
  6. passed over by a read cursor */
  7. dict_index_t* index, /*!< in: clustered index */
  8. const ulint* offsets,/*!< in: rec_get_offsets(rec, index) */
  9. ReadView* view) /*!< in: consistent read view */
  10. {
  11. ut_ad(index->is_clustered());
  12. ut_ad(page_rec_is_user_rec(rec));
  13. ut_ad(rec_offs_validate(rec, index, offsets));
  14. /* Temp-tables are not shared across connections and multiple
  15. transactions from different connections cannot simultaneously
  16. operate on same temp-table and so read of temp-table is
  17. always consistent read. */
  18. //只读事务或者临时表是不需要一致性读的判断
  19. if (srv_read_only_mode || index->table->is_temporary()) {
  20. ut_ad(view == 0 || index->table->is_temporary());
  21. return(true);
  22. }
  23. 版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
    原文链接:https://blog.csdn.net/woqutechteam/article/details/68486652
    站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢