加入收藏 | 设为首页 | 会员中心 | 我要投稿 济南站长网 (https://www.0531zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL中的myisam内部临时表解析

发布时间:2021-12-20 17:48:09 所属栏目:MySql教程 来源:互联网
导读:这篇文章主要讲解了MySQL中的myisam内部临时表分析,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习MySQL中的myisam内部临时表分析吧! 一、问题由来 一个朋友问我下面的tmp目录的文件是干什么的,一会就删
这篇文章主要讲解了“MySQL中的myisam内部临时表分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQL中的myisam内部临时表分析”吧!
 
一、问题由来
一个朋友问我下面的tmp目录的文件是干什么的,一会就删除了。他的版本是5.6
 
MySQL中的myisam内部临时表分析
 
tmpfile.png
 
<br data-filtered="filtered" font-size:18px;white-space:normal;background-color:#FFFFFF;" />我发现我的好多文章都是朋友问的问题。_
 
二、初步分析
因为对MySQL中的临时文件的种类和作用还是比较熟悉参考下文:
http://blog.itpub.net/7728585/viewspace-2146356/
但是都是基于5.7写的,但是对这种文件确实没见过,但是回想起在5.7官方文档中描述过,5.7过后默认的内部临时表磁盘文件使用了innodb引擎,但是5.6中默认还是myisam引擎的。5.7中使用什么引擎由参数internal_tmp_disk_storage_engine控制,但是在内存中始终是memory引擎的内部表,详细参考5.7官方文档:
8.4.4 Internal Temporary Table Use in MySQL
所以我告诉朋友这个应该是myisam引擎的内部临时表。
 
三、源码确认
我们发现这里的临时表名字为#sql_bec0_14.MYD等打开函数我们可以在如下代码中找到为什么这样命名方式:
 
 sprintf(path, "%s_%lx_%i", tmp_file_prefix,
            current_pid, temp_pool_slot);
所以我们大概明白:
 
#sql:来自tmp_file_prefix是宏定义
#define tmp_file_prefix "#sql" /**< Prefix for tmp tables */
bec0:来自mysqld的当前进程号
14:临时表缓冲区的某种槽号,没仔细看
四、什么时候用到内部临时表以及磁盘文件
这个问题在官方文档描述参考:
8.4.4 Internal Temporary Table Use in MySQL
我就不过多描述了,执行计划一般会出现use temporary字样,当然不出现也可能使用内部临时表,自行参考。
而对于是否磁盘文件则如下描述:
 
Presence of a BLOB or TEXT column in the table
Presence of any string column with a maximum length larger than 512 (bytes for binary strings,
characters for nonbinary strings) in the SELECT list, if UNION or UNION ALL is used
The SHOW COLUMNS and DESCRIBE statements use BLOB as the type for some columns, thus the
temporary table used for the results is an on-disk table.
The big_tables system variable can be used to force disk storage of internal temporary tables.
 
当然create_tmp_table函数代码中有这样一段逻辑如下来证明上面的描述,这段代码同时标记了internal_tmp_disk_storage_engine参数的作用,如下:
/* If result table is small; use a heap */
  if (select_options & TMP_TABLE_FORCE_MYISAM)
  {
    share->db_plugin= ha_lock_engine(0, myisam_hton);
    table->file= get_new_handler(share, &table->mem_root,
                                 share->db_type());
  }
  else if (blob_count || //大字段计数器
           (thd->variables.big_tables && //参数big_tables设置
            !(select_options & SELECT_SMALL_RESULT)))
  {
    /*
     * Except for special conditions, tmp table engine will be choosen by user.
     */
    switch (internal_tmp_disk_storage_engine) //参数internal_tmp_disk_storage_engine设置
    {
    case TMP_TABLE_MYISAM:
      share->db_plugin= ha_lock_engine(0, myisam_hton); //myisam引擎内部临时表
      break;
    case TMP_TABLE_INNODB:
      share->db_plugin= ha_lock_engine(0, innodb_hton);//innodb引擎内部临时表
      break;
    default:
      DBUG_ASSERT(0);
      share->db_plugin= ha_lock_engine(0, innodb_hton);
    }
 
 
    table->file= get_new_handler(share, &table->mem_root,
                                 share->db_type());
  }
  else
  {
    share->db_plugin= ha_lock_engine(0, heap_hton);////memory引擎内部临时表?
    table->file= get_new_handler(share, &table->mem_root,
                                 share->db_type());
  }
而对于tmp_table_size和max_heap_table_size 的比较这个逻辑依然在create_tmp_table函数中如下:
 
 
if (thd->variables.tmp_table_size == ~ (ulonglong) 0)       // No limit
    share->max_rows= ~(ha_rows) 0;
  else
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
                                 min(thd->variables.tmp_table_size,//参数tmp_table_size
                                     thd->variables.max_heap_table_size) ://参数max_heap_table_size
                                 thd->variables.tmp_table_size) /
                     share->reclength);
但是在测试的时候我将tmp_table_size设置得很小了,share->max_rows自然很小,但是还是没有磁盘内部临时表,很是纳闷,如下自己加入的打印输出如下:
 
 
2018-03-01T09:27:52.189710Z 3 [Note] (create_tmp_table 1404) tmp_table_size:1024,max_heap_table_size:1048576,blob_count:0,big_tables0
2018-03-01T09:27:52.189748Z 3 [Note] (create_tmp_table 1420) rows_limit:18446744073709551615,max_rows:73
当然我对这个函数的认知还非常有限,以后再说吧。

(编辑:济南站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读