mysql mma 原理_MySQL Internal Temporary

什么是Internal Temporary?

临时表分为两种,一种是当执行一些SQL的时候MySQL会自动创建的一些中间结果集,称为internal temporary,这些中间结果集可能放在memory中,也有可能放在disk上;

还有一种是手动执行create temporary table 语法生成的外部临时表,这种临时表存储在memory上,数据库shutdown,就会自动删除;

本篇讲的临时表都是指内部临时表,测试使用的MySQL版本是8.0.13;

怎么判断有没有使用内部临时表?

执行计划explain或explain format=json 中出现using temporary;

show status中Created_tmp_disk_tables或Created_tmp_tables数值增加;

什么情况下产生Internal temporary table?

(1)除了后面提到的特殊情况,所有使用union的SQL,但是使用union all没有使用临时表

(2)用到TEMPTABLE算法或者是UNION查询中的视图mysql> desc select * from t_order union select * from t_group;

+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+

| id | select_type  | table      | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra           |

+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+

|  1 | PRIMARY      | t_order    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   10 |   100.00 | NULL            |

|  2 | UNION        | t_group    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   10 |   100.00 | NULL            |

| NULL | UNION RESULT |  | NULL       | ALL  | NULL          | NULL | NULL    | NULL | NULL |     NULL | Using temporary |

+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+

3 rows in set, 1 warning (0.01 sec)

但是使用union all没有使用临时表

mysql> desc select * from t_order union all select * from t_group;

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+

| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+

|  1 | PRIMARY     | t_order | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   10 |   100.00 | NULL  |

|  2 | UNION       | t_group | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   10 |   100.00 | NULL  |

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+

2 rows in set, 1 warning (0.00 sec)

(3)使用衍生表

(4)子查询和semi-joinmysql> desc select /*+ set_var(optimizer_switch='derived_merge=off') */ * from (select * from t_order)t;

+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+

| id | select_type | table      | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Ex

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值