MySQL8.0 · 引擎特性 · InnoDB 批量读特性

Note:

  1. 相关worklog: WL#7093: Optimizer provides InnoDB with a bigger buffer
  2. 基于MySQL8.0.12

通常情况下,InnoDB每获得一行记录会:

  • 记录下当前的cursor
  • 返回记录
  • 下次进入innodb层时,重新恢复其在btree上的cursor,并读取下一条记录

但在满足一定条件时,InnoDB会顺序读取一部分记录并放到一个cache中。

  • 读取当前page的一些记录
  • 记录cursor
  • 返回记录
  • 再次进入Innodb层,直接从cache中取数据,如果cache已经取空,则继续到btree上读记录

在之前的版本中,这个cache是由innodb来控制的,挂在row_prebuilt_t->fetch_cache数组中,数量也是固定的,最多预读8条记录。

在MySQL8.0中,对这部分逻辑做了修改(wl#7093), 由server层来为innodb提供一个Buffer,并告诉innodb需要预读多少条记录。这种做法相比之前的版本显然更加合理,因为只有server层才理解sql,知道随后是否是顺序scan,是否需要预读更多的数据。server层通过估算可以去决定buffer的大小。

根据worklog的描述,执行器和innodb部分都会去决定是否使用record buffer.

执行器在如下场景不会使用record buffer:(ref set_record_buffer(const QEP_TAB *tab))

- If the access type is not one of ref, ref_or_null, index_merge,
  range, index or ALL.
- If the scan is estimated to return no more than one row.
- If the scan is a loose index scan, which typically doesn't read many
  consecutive rows.
- If the scan is against an internally generated temporary table.
- If the storage engine reports that it won't use the buffer.

InnoDB在如下场景不会使用record buffer (ref row_prebuilt_t::can_prefetch_records()):

- If the scan is not read-only.
- If the scan accesses BLOB-based columns (such as BLOB, TEXT, JSON,
  GEOMETRY).
- If it is a fulltext query.
- If the table does not have a user-defined primary key or a unique
  constraint that could be used as a primary key.
- Invoked from innodb api, aks: memcached plugin
- Invoked by Handler syntax

一些细节:

  • InnoDB目前硬限制了最大cache的行数为100行,(由于cache数据时,是持有了page latch的,为了避免过度长时间持有latch,需要设置上限。) 这个100未来是可以优化的,例如在算最大行数时,将记录大小和page size也考虑进去
  • Server层硬限制cache的大小总共不超过MAX_RECORD_BUFFER_SIZE,即128kb
  • cache内存从thd上分配,因此只有到sql结束时才会释放
  • 在innodb里,若满足end_range,就会停止读入cache (row_search_end_range_check)

其他相关函数:

row_sel_dequeue_cached_row_for_mysql() // 从buffer中读取出记录
row_sel_enqueue_cache_row_for_mysql() // 向buffer中缓存记录
row_sel_fetch_last_buf() 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值