暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

聊聊 table_open_cache 相关参数

yangyidba 2022-08-24
1413

前言

MySQL:Table_open_cache_hits
/Table_open_cache_misses
/Table_open_cache_overflows

这3个值的均和函数open_table
有关,且他们均和table cache和table share有关,下面是一个粗略的解释:

table cache:会话实例化(instance),通过table share表定义实例化,持有文件的文件描述符。table share:内存中表的定义。

一、Table_open_cache_hits
主要逻辑:

大概逻辑如下

retry_share:
  {
    Table_cache *tc= table_cache_manager.get_cache(thd);

    tc->lock();

    /*
      Try to get unused TABLE object or at least pointer to
      TABLE_SHARE from the table cache.
    */
    table= tc->get_table(thd, hash_value, key, key_length, &share);

    if (table)
    {... 
      thd->status_var.table_open_cache_hits++;
      goto table_found;

tc->get_table 这个调用,可以粗略看出是在表的 table sharefree list 中弹出一个instance(table cache),也就是不用实际的实例化,那么就是命中了,如下:

函数 Table_cache::get_table
el->free_tables.front()
这样Table_open_cache_hits +1。

二、Table_open_cache_misses
主要逻辑

如前问所述,如果找到空闲的instance(tabe cache)则重用即可,如果没找到还需要处理table share,然后通过table share 建立 instance(table cache)我们看看通过table share建立instance大概方式:

  error= open_table_from_share(thd, share, alias,
                               (uint) (HA_OPEN_KEYFILE |
                                       HA_OPEN_RNDFILE |
                                       HA_GET_INDEX |
                                       HA_TRY_READ_ONLY),
                                       EXTRA_RECORD,
                               thd->open_options, table, FALSE);

  if (error){}

thd->status_var.table_open_cache_misses++; 


大概就是通过函数 open_table_from_share
进行table cache的从table share到table cache的实例化,然后标记为Table_open_cache_misses +1

三、Table_open_cache_overflows
主要逻辑

前面我们说了如果没用命中(hint),则需要建立 instance(table cache),但是需要注意建立instance(table cache)的时候,如果超过了 table_open_cache
的设置,则会进行淘汰(注意5.7.26的代码来看 table_open_cache
可以分为多个 instance(默认16个)。那么这些淘汰的值记录到 Table_open_cache_overflows
,下面是逻辑:

函数:Table_cache::free_unused_tables_if_necessary

调用为open_table ->Table_cache::add_used_table->Table_cache::free_unused_tables_if_necessary

if (m_table_count > table_cache_size_per_instance && m_unused_tables)
  {
    mysql_mutex_lock(&LOCK_open);
    while (m_table_count > table_cache_size_per_instance &&
           m_unused_tables)
    {
      TABLE *table_to_free= m_unused_tables;
      remove_table(table_to_free);
      intern_close_table(table_to_free);
      thd->status_var.table_open_cache_overflows++;
    }
    mysql_mutex_unlock(&LOCK_open);
  }

注意这里的条件m_table_count > table_cache_size_per_instance
,就是淘汰的条件后者和table_open_cache
直接相关。

四、Opened_tables
/Open_tables

还有一个值 Opened_tables
, 它与 Table_open_cache_misses
类似,他的调用则是在open_table_from_share
进行自加。thd->status_var.opened_tables++;)
Open_tables
这是总的 instance(table cache)的个数如下:

static int show_open_tables(THD *thd, SHOW_VAR *var, char *buff)
{
  var->type= SHOW_LONG;
  var->value= buff;
  *((long *)buff)= (long)table_cache_manager.cached_tables();
  return 0;
}

/**
  Get total number of used and unused TABLE objects in all table caches.

  @note Doesn't require acquisition of table cache locks if inexact number
        of tables is acceptable.
*/

uint Table_cache_manager::cached_tables()
{
  uint result= 0;

  for (uint i= 0; i < table_cache_instances; i++)
    result+= m_table_cache[i].cached_tables();

  return result;
}

五、总结

Table_open_cache_hits
:能够从table share 的free list 中找到一个instance(table cache),则看做命中,值+1。

Table_open_cache_misses
:与Table_open_cache_hits
相反,如果找不到则需要重新实例化值+1,这通常发生在初始化第一次加载表,或者由于超过参数 table_open_cache
的设置被淘汰后需要重新实例化。

Table_open_cache_overflows
:就是上面说的淘汰的instance(table cache)的数量,每次淘汰值+1。

Opened_tables
:类似Table_open_cache_misses
,已经打开的表的数据量,是累计值。

Open_tables
:总的instance(table cache)的总数。我们通常可以通过他们判断 table_open_cache
参数设置是否合理。

作者:重庆八怪

链接:https://www.jianshu.com/p/9f3002209e74


文章转载自yangyidba,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论