InnoDB MVCC实现原理及源码解析

原文链接:InnoDB MVCC实现原理及源码解析

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原理流程
InnoDB MVCC实现原理及源码解析
4、READ-VIEW解读

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

2)read view结构

struct read_view_t{  
    ulint       type;   /*!< VIEW_NORMAL, VIEW_HIGH_GRANULARITY */  
    undo_no_t   undo_no;/*!< 0 or if type is  
                VIEW_HIGH_GRANULARITY  
                transaction undo_no when this high-granularity  
                consistent read view was created */  
    trx_id_t    low_limit_no;  
                /*!< The view does not need to see the undo  
                logs for transactions whose transaction number  
                is strictly smaller (<) than this value: they  
                can be removed in purge if not needed by other  
                views */  
    trx_id_t    low_limit_id;  
                /*!< The read should not see any transaction  
                with trx id >= this value. In other words,  
                this is the "high water mark". */  
    trx_id_t    up_limit_id;  
                /*!< The read should see all trx ids which  
                are strictly smaller (<) than this value.  
                In other words,  
                this is the "low water mark". */  
    ulint       n_trx_ids;  
                /*!< Number of cells in the trx_ids array */  
    trx_id_t*   trx_ids;/*!< Additional trx ids which the read should  
                not see: typically, these are the read-write  
                active transactions at the time when the read  
                is serialized, except the reading transaction  
                itself; the trx ids in this array are in a  
                descending order. These trx_ids should be  
                between the "low" and "high" water marks,  
                that is, up_limit_id and low_limit_id. */  
    trx_id_t    creator_trx_id;  
                /*!< trx id of creating transaction, or  
                0 used in purge */  
    UT_LIST_NODE_T(read_view_t) view_list;  
                /*!< List of read views in trx_sys */  
};  

主要包括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来完成,这个需要注意。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值