热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

InternalTemporaryTables

8.4.4 HowMySQLUsesInternalTemporaryTables这是MySQL手册中的一节,尝试补充了一些解释。用的版本是MySQL5.6.15社区版8.4.4 

8.4.4 How MySQL Uses Internal Temporary Tables

这是MySQL手册中的一节,尝试补充了一些解释。用的版本是MySQL5.6.15社区版

In some cases, the server creates internal temporary tables while processing queries. Such a table can be held in memory and processed by the MEMORY storage engine, or stored on disk and processed by the MyISAM storage engine. The server may create a temporary table initially as an in-memory table, then convert it to an on-disk table if it becomes too large. Users have no direct control over when the server creates an internal temporary table or which storage engine the server uses to manage it.

Temporary tables can be created under conditions such as these:

  • UNION queries use temporary tables.
    union all,顺序把各select的所有记录写入一个临时表,都写完了,再查出来发送给客户端;
    union,与上面类似,不同的是,临时表是有唯一键的,重复数据只记录一条;
  • Some views require temporary tables, such those evaluated using the TEMPTABLE algorithm, or that use UNION or aggregation.
  • If there is an ORDER BY clause and a different GROUP BY clause, or if the ORDER BY or GROUP BY contains columns from tables other than the first table in the join queue, a temporary table is created.
    即使只有一个group by,如果用不上索引,也会需要临时表;此时临时表有一个唯一键,即group by的列;还保存有select的列,包括聚合函数的列,比如sum的值;
    如果order by时出现filesort,此时不一定有临时表;filesort有单独的排序区sort buffer,和临时文件保存排序数据,数据量大时,会保存在硬盘;filesort和临时表不是一个概念,没有必然联系;
    mysql> show create table three\G
    *************************** 1. row ***************************
    Table: three
    Create Table: CREATE TABLE `three` (
    `id3` int(11) DEFAULT NULL,
    `name3` varchar(10) DEFAULT NULL,
    `age` tinyint(4) DEFAULT NULL,
    KEY `i_id3` (`id3`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)mysql> explain select avg(age),name3 from three group by name3;
    +—-+————-+——-+——+—————+——+———+——+——+———————————+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
    +—-+————-+——-+——+—————+——+———+——+——+———————————+
    |  1 | SIMPLE      | three | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using temporary; Using filesort |
    +—-+————-+——-+——+—————+——+———+——+——+———————————+
    1 row in set (0.08 sec)

    mysql> explain select avg(age),name3 from three group by name3 order by null;
    +—-+————-+——-+——+—————+——+———+——+——+—————–+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra           |
    +—-+————-+——-+——+—————+——+———+——+——+—————–+
    |  1 | SIMPLE      | three | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using temporary |
    +—-+————-+——-+——+—————+——+———+——+——+—————–+
    1 row in set (0.00 sec)

    mysql> explain select avg(age),name3 from three group by name3 order by age;
    +—-+————-+——-+——+—————+——+———+——+——+———————————+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
    +—-+————-+——-+——+—————+——+———+——+——+———————————+
    |  1 | SIMPLE      | three | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using temporary; Using filesort |
    +—-+————-+——-+——+—————+——+———+——+——+———————————+
    1 row in set (0.00 sec)

    mysql> explain select avg(age) avgage,name3 from three group by name3 order by avgage;
    +—-+————-+——-+——+—————+——+———+——+——+———————————+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
    +—-+————-+——-+——+—————+——+———+——+——+———————————+
    |  1 | SIMPLE      | three | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using temporary; Using filesort |
    +—-+————-+——-+——+—————+——+———+——+——+———————————+
    1 row in set (0.02 sec)
    只要group by用不上索引,就需要临时表来解决去重和计算聚合函数;
    加上索引,执行计划就有所变化了:
    mysql> alter table three add key i_name3(name3);
    Query OK, 0 rows affected (0.51 sec)
    Records: 0  Duplicates: 0  Warnings: 0

    mysql> show create table three\G
    *************************** 1. row ***************************
    Table: three
    Create Table: CREATE TABLE `three` (
    `id3` int(11) DEFAULT NULL,
    `name3` varchar(10) DEFAULT NULL,
    `age` tinyint(4) DEFAULT NULL,
    KEY `i_id3` (`id3`),
    KEY `i_name3` (`name3`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)

    mysql> explain select avg(age),name3 from three group by name3;
    +—-+————-+——-+——-+—————+———+———+——+——+——-+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra |
    +—-+————-+——-+——-+—————+———+———+——+——+——-+
    |  1 | SIMPLE      | three | index | i_name3       | i_name3 | 13      | NULL |    7 | NULL  |
    +—-+————-+——-+——-+—————+———+———+——+——+——-+
    1 row in set (0.00 sec)

    上面这个SQL用到了索引,利用innodb b+tree的特性,按顺序读取name3的值,并计算其聚合函数;当读取的name3的值发生变化时,就表示变化发生前的那个name3的值对应的聚合函数值计算完了,发送给网络接口;由此就不需要用临时表来去掉name3重复值,以及计算聚合函数值了;
    下面这个SQL也用到了索引,但是按照聚合函数值排序,就比上面多了临时表和filesort;
    mysql> explain select avg(age) avgage,name3 from three group by name3 order by avgage;
    +—-+————-+——-+——-+—————+———+———+——+——+———————————+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra                           |
    +—-+————-+——-+——-+—————+———+———+——+——+———————————+
    |  1 | SIMPLE      | three | index | i_name3       | i_name3 | 13      | NULL |    7 | Using temporary; Using filesort |
    +—-+————-+——-+——-+—————+———+———+——+——+———————————+
    1 row in set (0.04 sec)
    上面这个SQL,group by使用了索引。利用索引的顺序读取name3,并计算avg(age),算好一个avgage值之后,把该avgage和name3写入临时表;
    都写完之后,从临时表读取数据,按avgage排序,然后发送结果;
    而group by与order by的列不属于同一个表时,只是多了个join过程,其他过程应该是同理的;

  • DISTINCT combined with ORDER BY may require a temporary table.
    distinct与group by同理
    mysql> show create table three\G
    *************************** 1. row ***************************
    Table: three
    Create Table: CREATE TABLE `three` (
    `id3` int(11) DEFAULT NULL,
    `name3` varchar(10) DEFAULT NULL,
    `age` tinyint(4) DEFAULT NULL,
    KEY `i_id3` (`id3`),
    KEY `i_name3` (`name3`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)mysql> explain select distinct(age) from three;
    +—-+————-+——-+——+—————+——+———+——+——+—————–+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra           |
    +—-+————-+——-+——+—————+——+———+——+——+—————–+
    |  1 | SIMPLE      | three | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using temporary |
    +—-+————-+——-+——+—————+——+———+——+——+—————–+
    1 row in set (0.01 sec)

    mysql> explain select distinct(name3) from three;
    +—-+————-+——-+——-+—————+———+———+——+——+————-+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
    +—-+————-+——-+——-+—————+———+———+——+——+————-+
    |  1 | SIMPLE      | three | index | i_name3       | i_name3 | 13      | NULL |    7 | Using index |
    +—-+————-+——-+——-+—————+———+———+——+——+————-+
    1 row in set (0.04 sec)

     
  • If you use the SQL_SMALL_RESULT option, MySQL uses an in-memory temporary table, unless the query also contains elements (described later) that require on-disk storage.
  • Derived tables (subqueries in the FROM clause).
    mysql> show create table one\G
    *************************** 1. row ***************************
    Table: one
    Create Table: CREATE TABLE `one` (
    `id` int(11) DEFAULT NULL,
    `name` varchar(10) DEFAULT NULL,
    KEY `i_name` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.04 sec)mysql> explain select * from (select * from one) a;
    +—-+————-+————+——+—————+——+———+——+——+——-+
    | id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra |
    +—-+————-+————+——+—————+——+———+——+——+——-+
    |  1 | PRIMARY     | | ALL  | NULL          | NULL | NULL    | NULL |    2 | NULL  |
    |  2 | DERIVED     | one        | ALL  | NULL          | NULL | NULL    | NULL |    2 | NULL  |
    +—-+————-+————+——+—————+——+———+——+——+——-+
    2 rows in set (0.00 sec)
    在执行计划里叫DERIVED,实现上就是一个临时表;
    上面SQL把one的所有记录写入临时表,然后再查出来;
  • Tables created for subquery or semi-join materialization.
    想用in看一下子查询,结果被优化成join了;
    mysql> explain select * from one where id in(select id2 from two);
    +—-+————-+——-+——+—————+——-+———+—————+——+——————————+
    | id | select_type | table | type | possible_keys | key   | key_len | ref           | rows | Extra                        |
    +—-+————-+——-+——+—————+——-+———+—————+——+——————————+
    |  1 | SIMPLE      | one   | ALL  | NULL          | NULL  | NULL    | NULL          |    2 | Using where                  |
    |  1 | SIMPLE      | two   | ref  | i_id2         | i_id2 | 5       | testdb.one.id |    1 | Using index; FirstMatch(one) |
    +—-+————-+——-+——+—————+——-+———+—————+——+——————————+
    2 rows in set (0.00 sec)
    5.6加了个firstmatch的优化,而且把subquery变成join了,效率提高不少;mysql> explain select * from one where exists (select id2 from two where one.id=two.id2);
    +—-+——————–+——-+——+—————+——-+———+—————+——+————-+
    | id | select_type        | table | type | possible_keys | key   | key_len | ref           | rows | Extra       |
    +—-+——————–+——-+——+—————+——-+———+—————+——+————-+
    |  1 | PRIMARY            | one   | ALL  | NULL          | NULL  | NULL    | NULL          |    2 | Using where |
    |  2 | DEPENDENT SUBQUERY | two   | ref  | i_id2         | i_id2 | 5       | testdb.one.id |    1 | Using index |
    +—-+——————–+——-+——+—————+——-+———+—————+——+————-+
    2 rows in set (0.04 sec)
    mysql> explain select * from one where exists (select id2 from two where one.id=two.id2) order by name;
    +—-+——————–+——-+——+—————+——-+———+—————+——+—————————–+
    | id | select_type        | table | type | possible_keys | key   | key_len | ref           | rows | Extra                       |
    +—-+——————–+——-+——+—————+——-+———+—————+——+—————————–+
    |  1 | PRIMARY            | one   | ALL  | NULL          | NULL  | NULL    | NULL          |    2 | Using where; Using filesort |
    |  2 | DEPENDENT SUBQUERY | two   | ref  | i_id2         | i_id2 | 5       | testdb.one.id |    1 | Using index                 |
    +—-+——————–+——-+——+—————+——-+———+—————+——+—————————–+
    2 rows in set (0.00 sec)
    上面这两个SQL都没有用到临时表;
    子查询用到临时表的情况需要再观察;
    semijoin的目前还不懂,后面再说了;

To determine whether a query requires a temporary table, use EXPLAIN and check the Extra column to see whether it says Using temporary (see Section 8.8.1, “Optimizing Queries with EXPLAIN”). EXPLAIN will not necessarily say Using temporary for derived or materialized temporary tables.

If an internal temporary table is created initially as an in-memory table but becomes too large, MySQL automatically converts it to an on-disk table. The maximum size for in-memory temporary tables is the minimum of the tmp_table_size and max_heap_table_size values. This differs from MEMORY tables explicitly created withCREATE TABLE: For such tables, only the max_heap_table_size system variable determines how large the table is permitted to grow and there is no conversion to on-disk format.

When the server creates an internal temporary table (either in memory or on disk), it increments theCreated_tmp_tables status variable. If the server creates the table on disk (either initially or by converting an in-memory table) it increments the Created_tmp_disk_tables status variable.

Some conditions prevent the use of an in-memory temporary table, in which case the server uses an on-disk table instead:

  • Presence of a BLOB or TEXT column in the table
  • Presence of any string column in a GROUP BY or DISTINCT clause larger than 512 bytes for binary strings or 512 characters for nonbinary strings. (Before MySQL 5.6.15, the limit is 512 bytes regardless of string type.)
  • 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

简单地说,临时表是用来以表的形式保存中间结果的。数据少时是heap表,在内存中,数据多时是MyISAM表,在硬盘上。

注意,不是所有的中间结果都用临时表。filesort保存中间结果用的临时文件,不是临时表。

Internal Temporary Tables


推荐阅读
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
author-avatar
三星anycall
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有