【MySQL】【翻译】 MySQL 5.7 的内部临时表新特性
admin
2023-05-03 03:41:40
0

【MySQL】【翻译】MySQL Internal Temporary Tables in MySQL 5.7(MySQL 5.7 内部临时表)

Alexander Rubin | December 4, 2017 | Posted In: Insight for DBAs, MySQL, Percona Monitoring and Management

翻译:张锐志

In this blog post, I investigate a case of spiking InnoDB Rows inserted in the absence of a write query, and find internal temporary tables to be the culprit.

本文中研究了在没有写查询的情况下,InnoDB行插入却因内部临时表的问题发生性能尖刺的情形。

Recently I was investigating an interesting case for a customer. We could see the regular spikes on a graph depicting “InnoDB rows inserted” metric (jumping from 1K/sec to 6K/sec), however we were not able to correlate those spikes with other activity. The innodb_row_inserted graph (picture from PMM demo) looked similar to this (but on a much larger scale):

事情发生在我研究一个客户的案例时,在”InnoDB行插入“指标图上,发现了从1k行每秒激增到6K行每秒的尖刺,但却无法和其他活动或者现象连接起来,PMM监控图形上也有同样的反映。

【MySQL】【翻译】 MySQL 5.7 的内部临时表新特性

Other graphs (Com*, Handler*) did not show any spikes like that. I’ve examined the logs (we were not able to enable general log or change the threshold of the slow log), performance_schema, triggers, stored procedures, prepared statements and even reviewed the binary logs. However, I was not able to find any single *write* query which could have caused the spike to 6K rows inserted.

其他例如句柄和接口的图形都没有显示同样的尖刺,在无法开启general log的情况下,我们尝试检查了所有的日志,performance_schema,触发器,存储过程,预编译语句,甚至包括binlog后发现没有任何单个的写查询语句可以导致每秒插入飙升到6K行。

Finally, I figured out that I was focusing on the wrong queries. I was trying to correlate the spikes on the InnoDB Rows inserted graph to the DML queries (writes). However, the spike was caused by SELECT queries! But why would SELECT queries cause the massive InnoDB insert operation? How is this even possible?

在最后才发现,行插入飙升一定和DML有关的这种想法是错误的,出乎意料的是,尖刺是由于SELECT查询导致的,但为何SELECT查询会导致大量的InnoDB行插入操作呢?

It turned out that this is related to temporary tables on disk. In MySQL 5.7 the default setting for internal_tmp_disk_storage_engine is set for InnoDB. That means that if the SELECT needs to create a temporary table on disk (e.g., for GROUP BY) it will use the InnoDB storage engine.

原来是与磁盘临时表有关。在MySQL 5.7版本中,内部磁盘临时表的默认引擎是InnoDB引擎,这就意味着当SELECT操作需要在磁盘上创建临时表时(例如GROUP BY操作),就会使用到InnoDB引擎。

Is that bad? Not necessarily. Krunal Bauskar published a blog post originally about the InnoDB Intrinsic Tables performance in MySQL 5.7. The InnoDB internal temporary tables are not redo/undo logged. So in general performance is better. However, here is what we need to watch out for:

但这种尖刺就一定意味着性能的下降吗?Krunal Bauskar曾经写过一篇关于5.7 InnoDB原生表性能的文章,InnoDB的内部临时表的操作并不会记录在redo和undo中,一般情况下相比原本MyISAM引擎的临时表性能更好点,但是仍需注意一下几点:

  1. Change of the place where MySQL stores temporary tables. InnoDB temporary tables are stored in ibtmp1 tablespace file. There are a number of challenges with that:

    更改MySQL存储临时表的位置,原本InnoDB临时表被存储在ibtmp1表空间中,可能遇到以下的问题:

    • Location of the ibtmp1 file. By default it is located inside the innodb datadir. Originally MyISAM temporary tables were stored in tmpdir. We can configure the size of the file, but the location is always relative to InnoDB datadir, so to move it to tmpdir we need something like this: innodb_temp_data_file_path=../../../tmp/ibtmp1:12M:autoextend

      ibtmp1文件默认保存在InnoDB的数据目录,原本MyISAM临时表被放在MySQL的tmp目录,如若像MyISAM一样把临时表文件存储在MySQL的tmp目录,需要更改为innodb_temp_data_file_path=../../../tmp/ibtmp1:12M:autoextend

    • Like other tablespaces it never shrinks back (though it is truncated on restart). The huge temporary table can fill the disk and hang MySQL (bug opened). One way to fix that is to set the maximum size of ibtmp1 file: innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:1G

      临时表空间和其他的表空间一样都不会自动缩小其占用容量,可能会发生临时表空间容量占满磁盘,MySQL挂掉的情况,可以通过控制其最大的容量来解决:innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:1G

    • Like other InnoDB tables it has all the InnoDB limitations, i.e., InnoDB row or column limits. If it exceeds these, it will return “Row size too large” or “Too many columns” errors. The workaround is to set internal_tmp_disk_storage_engine to MYISAM.

      内部临时InnoDB表同样共享常规的InnoDB表的限制,如行或列的最大数量限制,超过最大值后,会返回Row size too large” or “Too many columns”的错误,遇到此种情况,可以将默认临时表引擎改回MyISAM

  2. When all temp tables go to InnoDB, it may increase the total engine load as well as affect other queries. For example, if originally all datasets fit into buffer_pool and temporary tables were created outside of the InnoDB, it will not affect the* InnoDB* memory footprint. Now, if a huge temporary table is created as an InnoDB table it will use innodb_buffer_pool and may “evict” the existing pages so that other queries may perform slower.

    当所有的临时表都改成InnoDB引擎后,会增加引擎的负载,影响到其他的查询。例如:当所有的表都放入buffer_pool中,且临时表都不是InnoDB引擎,那么不会对InnoDB的内存占用造成任何影响,但是临时表改成InnoDB引擎后,会和普通InnoDB表一样占用InnoDB_buffer_pool的空间,而且可能因为临时表空间占用过大挤出真正的热数据,让某些高频查询变慢

Conclusion 结论

Beware of the new change in MySQL 5.7, the internal temporary tables (those that are created for selects when a temporary table is needed) are stored in InnoDB ibtmp file. In most cases this is faster. However, it can change the original behavior. If needed, you can switch the creation of internal temp tables back to MyISAM: set globalinternal_tmp_disk_storage_engine=MYISAM

内部InnoDB临时表(可能仅仅因为是SELECT查询导致)被保存在InnoDB的ibtmp文件中,在大部分情况下,会加速临时表或者查询的速度,但是会影响到原本InnoDB内存的占用情况和原本临时表处理的逻辑,如果在某种情况确实需要规避的话,可以尝试将临时表的引擎改回MyISAM。set global internal_tmp_disk_storage_engine=MYISAM 。这个案例要求我们要对MySQL 5.7的特性要有所注意和了解。

相关内容

热门资讯

卖“毒蛋”的人,抓到了 作者 | 何国胜 编辑 | 向现“(人)抓到了,目前案件正在侦办中。”7月28日晚间,苏州禁毒部门有...
尺素金声丨实施零关税国家达63... 海关总署发布的数据显示,今年5月1日起,我国对53个非洲建交国全面实施零关税举措,目前,我国实施零关...
职业索赔盯上基层诊所,倒逼用药... 文 | 布丁基层诊所正在被职业索赔盯上。据新京报,去年夏天,一男子走进河南南阳一家诊所,要求购买三瓶...
“西瓜我全买了”就可以肆意妄为... 拿西瓜砸了人,把瓜都买了,就能一走了之吗?事实证明,这套逻辑在法治社会行不通。7月28日晚,据海峡都...
科学家在日本广岛发现新物质,系... 在美国对日本广岛进行原子弹轰炸近81年后,科学家们在广岛的沙滩上发现了一种奇异且从未被发现过的新物质...
AI失控,反噬开始 作者 | 贺一 编辑 | 阿树近期,中国开源模型在美国频繁引发热议。7月28日,月之暗面发布Kimi...
“总统千金天价离婚”,分到43... 2026年7月24日下午,首尔高等法院,一场持续近十年的司法拉锯战终于接近尾声。法庭裁定SK集团会长...
巴基斯坦,又拿下一个历史性协议 全世界都没想到,接连的中东大战,巴基斯坦正成为最大赢家。去年以色列追杀哈马斯,空袭卡塔尔首都,阿拉伯...
汇正财经贺峰的一对一指导服务怎...   对于考虑购买证券投资顾问服务的投资者来说,'一对一指导服务怎么样'是一个重要的考量维度。需要首先...
重庆失联00后网格员龚宝冬确认...   重庆失联00后网格员龚宝冬确认遇难  【重庆失联00后网格员龚宝冬确认遇难】2026年7月29日...