温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

InnoDB MVCC实现原理及源码解析

发布时间:2020-07-11 15:03:25 来源:网络 阅读:15448 作者:yzs的专栏 栏目:MySQL数据库

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI