ITPub博客

首页 > 数据库 > MySQL > InnoDB MVCC实现原理及源码解析

InnoDB MVCC实现原理及源码解析

原创 MySQL 作者:yzs87 时间:2018-04-15 15:52:03 0 删除 编辑

1、原理介绍

数据多版本(MVCC)是MySQL实现高性能的一个主要的一个主要方式,通过对普通的SELECT不加锁,直接利用MVCC读取指版本的值,避免了对数据重复加锁的过程。InnoDB支持MVCC多版本,其中RC和RR隔离级别是利用consistent read view方式支持的,即在某个时刻对事物系统打快照记下所有活跃读写事务ID,之后读操作根据事务ID与快照中的事务ID进行比较,判断可见性。

2、InnoDB数据行结构

行结构中,除了用户定义的列外还有3个系统列:DATA_ROW_ID、DATA_TRX_ID、DATA_ROLL_PTR,如果表没有定义主键那么DATA_ROW_ID作为主键列,否则行结构中没有DATA_ROW_ID列。其中:

    DATA_TRX_ID:修改该行数据的事务的ID

    DATA_ROLL_PTR:指向该行回滚段的指针。

整个MVCC实现,关键靠这2个字段来完成。

3、READ-VIEW原理流程


4、READ-VIEW解读

1)read view是和SQL语句绑定的,在每个SQL语句执行前申请或获取(RR隔离级别:事务第一个select申请,之后都用这个;RC隔离级别:每个select都会申请)

2)read view结构


点击(此处)折叠或打开

  1. struct read_view_t{
  2.     ulint type; /*!< VIEW_NORMAL, VIEW_HIGH_GRANULARITY */
  3.     undo_no_t undo_no;/*!< 0 or if type is
  4.                 VIEW_HIGH_GRANULARITY
  5.                 transaction undo_no when this high-granularity
  6.                 consistent read view was created */
  7.     trx_id_t low_limit_no;
  8.                 /*!< The view does not need to see the undo
  9.                 logs for transactions whose transaction number
  10.                 is strictly smaller (<) than this value: they
  11.                 can be removed in purge if not needed by other
  12.                 views */
  13.     trx_id_t low_limit_id;
  14.                 /*!< The read should not see any transaction
  15.                 with trx id >= this value. In other words,
  16.                 this is the "high water mark". */
  17.     trx_id_t up_limit_id;
  18.                 /*!< The read should see all trx ids which
  19.                 are strictly smaller (<) than this value.
  20.                 In other words,
  21.                 this is the "low water mark". */
  22.     ulint n_trx_ids;
  23.                 /*!< Number of cells in the trx_ids array */
  24.     trx_id_t* trx_ids;/*!< Additional trx ids which the read should
  25.                 not see: typically, these are the read-write
  26.                 active transactions at the time when the read
  27.                 is serialized, except the reading transaction
  28.                 itself; the trx ids in this array are in a
  29.                 descending order. These trx_ids should be
  30.                 between the "low" and "high" water marks,
  31.                 that is, up_limit_id and low_limit_id. */
  32.     trx_id_t creator_trx_id;
  33.                 /*!< trx id of creating transaction, or
  34.                 0 used in purge */
  35.     UT_LIST_NODE_T(read_view_t) view_list;
  36.                 /*!< List of read views in trx_sys */
  37. };

主要包括3个成员{low_limit_id,up_limit_id,trx_ids}。

    low_limit_id:表示创建read view时,当前事务活跃读写链表最大的事务ID,即最近创建的除自身外最大的事务ID

    up_limit_id:表示创建read view时,当前事务活跃读写链表最小的事务ID。

    trx_ids:创建read view时,活跃事务链表里所有事务ID

3)对于小于等于RC的隔离级别,每次SQL语句结束后都会调用read_view_close_for_mysql将read view从事务中删除,这样在下一个SQL语句启动时,会判断trx->read_view为NULL,从而重新申请。对于RR隔离级别,则SQL语句结束后不会删除read_view,从而下一个SQL语句时,使用上次申请的,这样保证事务中的read view都一样,从而实现可重复读的隔离级别。

4)对于可见性判断,分配聚集索引和二级索引。聚集索引:

     记录的DATA_TRX_ID < view->up_limit_id:在创建read view时,修改该记录的事务已提交,该记录可见

   DATA_TRX_ID >= view->low_limit_id:当前事务启动后被修改,该记录不可见

   DATA_TRX_ID 位于(view->up_limit_id,view->low_limit_id):需要在活跃读写事务数组查找trx_id是否存在,如果存在,记录对于当前read view是不可见的。

   二级索引:

    由于InnoDB的二级索引只保存page最后更新的trx_id,当利用二级索引进行查询的时候,如果page的trx_id小于view->up_limit_id,可以直接判断page的所有记录对于当前view是可见的,否则需要回clustered索引进行判断。

5)如果记录对于view不可见,需要通过记录的DB_ROLL_PTR指针遍历history list构造当前view可见版本数据

6)start transaction和begin语句执行后并没有在innodb层分配事务ID、回滚段、read_view、将事务放到读写事务链表等,这个操作需要第一个SQL语句调用函数trx_start_low来完成,这个需要注意。



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31493717/viewspace-2152957/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
专注于MySQL、Aerospike、PostgreSQL数据库

注册时间:2017-10-03

  • 博文量
    149
  • 访问量
    285476