• ADADADADAD

    InnoDB MVCC实现原理及源码解析[ mysql数据库 ]

    mysql数据库 时间:2024-12-25 09:57:41

    作者:文/会员上传

    简介:

    1、原理介绍数据多版本(MVCC)是MySQL实现高性能的一个主要的一个主要方式,通过对普通的SELECT不加锁,直接利用MVCC读取指版本的值,避免了对数据重复加锁的过程。InnoDB支持MVCC

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    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:修改该行数据的事务的IDDATA_ROLL_PTR:指向该行回滚段的指针。

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

    3、READ-VIEW原理流程

    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 isVIEW_HIGH_GRANULARITYtransaction undo_no when this high-granularityconsistent read view was created */trx_id_tlow_limit_no;/*!< The view does not need to see the undologs for transactions whose transaction numberis strictly smaller (<) than this value: theycan be removed in purge if not needed by otherviews */trx_id_tlow_limit_id;/*!< The read should not see any transactionwith trx id >= this value. In other words,this is the "high water mark". */trx_id_tup_limit_id;/*!< The read should see all trx ids whichare 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 shouldnot see: typically, these are the read-writeactive transactions at the time when the readis serialized, except the reading transactionitself; the trx ids in this array are in adescending order. These trx_ids should bebetween the "low" and "high" water marks,that is, up_limit_id and low_limit_id. */trx_id_tcreator_trx_id;/*!< trx id of creating transaction, or0 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,即最近创建的除自身外最大的事务IDup_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来完成,这个需要注意。

    InnoDB MVCC实现原理及源码解析.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: innodbmvccd