温馨提示×

温馨提示×

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

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

怎么理解MySQL中Innodb DB_ROLL_PTR指针

发布时间:2021-11-10 11:23:28 来源:亿速云 阅读:170 作者:iii 栏目:MySQL数据库

本篇内容主要讲解“怎么理解MySQL中Innodb DB_ROLL_PTR指针”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么理解MySQL中Innodb DB_ROLL_PTR指针”吧!

一、引入

我们知道每一条记录在聚集索引上都有如下的分布:

  • rowid(主键)+DB_TRX_ID+DB_ROLL_PTR+其他字段
    这样格式其中DB_TRX_ID+DB_ROLL_PTR作为一致性读的关键信息存储下来,其中DB_TRX_ID在存储上占用6字节,DB_ROLL_PTR在存储上占用7字节。那么DB_ROLL_PTR是如何解析到undo上的呢,这也是一位朋友问的问题。
    下面是trx0types.h中对这部分的定义

/** Row identifier (DB_ROW_ID, DATA_ROW_ID) */typedef ib_id_t row_id_t;/** Transaction identifier (DB_TRX_ID, DATA_TRX_ID) */typedef ib_id_t trx_id_t;/** Rollback pointer (DB_ROLL_PTR, DATA_ROLL_PTR) */ typedef ib_id_t roll_ptr_t;

而ib_id_t实际上都是64位非负整数

typedef unsigned __int64 ib_uint64_t;

二、函数流程

我大概看了一下流程如下:

trx_undo_prev_version_build  //Build a previous version of a clustered index record
 ->roll_ptr = row_get_rec_roll_ptr(rec, index, offsets); //获取rollback 指针
 ->rec_trx_id = row_get_rec_trx_id(rec, index, offsets); //获取TRX_ID
 ->trx_undo_get_undo_rec(roll_ptr, rec_trx_id, heap, is_redo_rseg,index->table->name, &undo_rec))//此处获取前版本,获取后放到undo_rec中
  ->trx_undo_get_undo_rec_low(roll_ptr, heap, is_redo_rseg); //undo_rec作为传出参数。传出访问到的undo
    ->trx_undo_decode_roll_ptr //做roll_ptr的解析工作获取segment id\page no\offset
    ->开MTR获取latch准备拷贝
    ->拷贝trx_undo_rec_copy
    ->提交MTR

实际上解析工具由trx_undo_decode_roll_ptr 完成。

三、实际解析

其实解析挺简单,都是写死了的。

/***********************************************************************//**
Decodes a roll pointer. */ //从高位到低位依次是  第1位是否是insert //第2到8位是segmentid//第9到40位为page no //第41位到56位为OFFSETUNIV_INLINEvoidtrx_undo_decode_roll_ptr(/*=====================*/
    roll_ptr_t  roll_ptr,   /*!< in: roll pointer */
    ibool*      is_insert,  /*!< out: TRUE if insert undo log */
    ulint*      rseg_id,    /*!< out: rollback segment id */
    ulint*      page_no,    /*!< out: page number */
    ulint*      offset)     /*!< out: offset of the undo
                    entry within page */{#if DATA_ROLL_PTR_LEN != 7# error "DATA_ROLL_PTR_LEN != 7"#endif#if TRUE != 1# error "TRUE != 1"#endif
    ut_ad(roll_ptr < (1ULL << 56));
    *offset = (ulint) roll_ptr & 0xFFFF; //获取低16位 为OFFSET
    roll_ptr >>= 16; //右移16位
    *page_no = (ulint) roll_ptr & 0xFFFFFFFF;//获取32位为 page no
    roll_ptr >>= 32;//右移32位
    *rseg_id = (ulint) roll_ptr & 0x7F;//获取7位为segment id
    roll_ptr >>= 7;//右移7位
    *is_insert = (ibool) roll_ptr; /* TRUE==1 *///最后一位}

到此,相信大家对“怎么理解MySQL中Innodb DB_ROLL_PTR指针”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

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

AI