让前端飞

Change Buffer 只适用于非唯一索引页?错

2022-05-26  本文已影响0人  273abf195ee7

最近在网上看到一些文章里说:“change buffer 只适用于非唯一索引页。”其实这个观点是错的,先来看看官方文档对 change buffer 的介绍:

文档地址:https://dev.mysql.com/doc/refman/8.0/en/innodb-change-buffer.html

The change buffer is a special data structure that caches changes to secondary index pages when those pages are not in the buffer pool.

这里的意思是,缓存那些不在 buffer pool 中的二级索引页,并不是指非唯一的二级索引。那具体使用 change buffer 的条件是什么?其实具体使用条件主要汇集在以下五点:

接下来我们结合源码和文档来看看具体操作。

源码地址:GitHub - mysql/mysql-server: MySQL Server, the world's most popular open source database, and MySQL

先看第一点设置选项尾 innodb_change_buffering,它能够针对三种类型的操作 INSERT、DELETE-MARK 、DELETE 进行缓存,三者对应 dml 语句关系如下:

// 代码路径:storage/innobase/include/ibuf0ibuf.h
 
/* Possible operations buffered in the insert/whatever buffer. See
ibuf_insert(). DO NOT CHANGE THE VALUES OF THESE, THEY ARE STORED ON DISK. */
typedef enum {
  IBUF_OP_INSERT = 0,
  IBUF_OP_DELETE_MARK = 1,
  IBUF_OP_DELETE = 2,

  /* Number of different operation types. */
  IBUF_OP_COUNT = 3
} ibuf_op_t;

/** Combinations of operations that can be buffered.
@see innodb_change_buffering_names */
enum ibuf_use_t {
  IBUF_USE_NONE = 0,
  IBUF_USE_INSERT,             /* insert */
  IBUF_USE_DELETE_MARK,        /* delete */
  IBUF_USE_INSERT_DELETE_MARK, /* insert+delete */
  IBUF_USE_DELETE,             /* delete+purge */
  IBUF_USE_ALL                 /* insert+delete+purge */
};

此外 innodb_change_buffering 还可以通过设置其他选项来进行相应的缓存操作:

第二点还需要大家进行判断条件即可,所以就不进行扩展讲解了,我们来细说一下第三点。

第三点的具体参考函数为 ibuf_should_try,它满足 ibuf 缓存条件后,会使用两种模式去尝试获取数据页。

这里说明一下,在 MySQL5.5 之前的版本中,由于只支持缓存 insert 操作,所以最初叫做 insert buffer,只是后来的版本中支持了更多的操作类型缓存,才改叫 change buffer,但是代码中与 change buffer 相关的 函数或变量还是以 ibuf 前缀开头。

下面是函数的具体实现,地址在:storage/innobase/include/ibuf0ibuf.ic

/** A basic partial test if an insert to the insert buffer could be possible and
 recommended. */
static inline ibool ibuf_should_try(
    dict_index_t *index,     /*!< in: index where to insert */
    ulint ignore_sec_unique) /*!< in: if != 0, we should
                             ignore UNIQUE constraint on
                             a secondary index when we
                             decide */
{
  return (innodb_change_buffering != IBUF_USE_NONE && ibuf->max_size != 0 &&
          index->space != dict_sys_t::s_dict_space_id &&
          !index->is_clustered() && !dict_index_is_spatial(index) &&
          !dict_index_has_desc(index) &&
          index->table->quiesce == QUIESCE_NONE &&
          (ignore_sec_unique || !dict_index_is_unique(index)) &&
          srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE);
}

上面加粗和标红的地方就是对唯一二级索引的判断的地方,意思是:

ignore_sec_unique 的取值在:storage/innobase/btr/btr0cur.cc

    if (btr_op != BTR_NO_OP &&
        ibuf_should_try(index, btr_op != BTR_INSERT_OP)) {
      /* Try to buffer the operation if the leaf
      page is not in the buffer pool. */

      fetch = btr_op == BTR_DELETE_OP ? Page_fetch::IF_IN_POOL_OR_WATCH
                                      : Page_fetch::IF_IN_POOL;
    }

其中红框中的 btr_op 指的是本次修改的具体操作是什么,也就是:

当此次具体的修改操作是 INSERT 操作时,ignore_sec_unique 为 0,也就是当修改的是唯一二级索引记录时,不可以使用 ibuf。

最后总结一下,看完了本文相信大家都能比较明确的意识到,网上说只有非唯一索引才能使用 change buffer 的说法,毫无疑问是错的。只要满足了其它 4 个条件,对唯一索引进行的删除操作完全可以使用 change buffer 优化。

上一篇 下一篇

猜你喜欢

热点阅读