MySQL · 特性分析 · 内部临时表
http://mysql.taobao.org/monthly/2016/06/07/#rd
MySQL中的两种临时表
外部临时表
通过CREATE TEMPORARY TABLE 创建的临时表,这种临时表称为外部临时表。这种临时表只对当前用户可见,当前会话结束的时候,该临时表会自动关闭。这种临时表的命名与非临时表可以同名(同名后非临时表将对当前会话不可见,直到临时表被删除)。
内部临时表
内部临时表是一种特殊轻量级的临时表,用来进行性能优化。这种临时表会被MySQL自动创建并用来存储某些操作的中间结果。这些操作可能包括在优化阶段或者执行阶段。这种内部表对用户来说是不可见的,但是通过EXPLAIN或者SHOW STATUS可以查看MYSQL是否使用了内部临时表用来帮助完成某个操作。内部临时表在SQL语句的优化过程中扮演着非常重要的角色, MySQL中的很多操作都要依赖于内部临时表来进行优化。但是使用内部临时表需要创建表以及中间数据的存取代价,所以用户在写SQL语句的时候应该尽量的去避免使用临时表。
内部临时表有两种类型:一种是HEAP临时表,这种临时表的所有数据都会存在内存中,对于这种表的操作不需要IO操作。另一种是OnDisk临时表,顾名思义,这种临时表会将数据存储在磁盘上。OnDisk临时表用来处理中间结果比较大的操作。如果HEAP临时表存储的数据大于MAX_HEAP_TABLE_SIZE(详情请参考MySQL手册中系统变量部分),HEAP临时表将会被自动转换成OnDisk临时表。OnDisk临时表在5.7中可以通过INTERNAL_TMP_DISK_STORAGE_ENGINE系统变量选择使用MyISAM引擎或者InnoDB引擎。
本篇文章主要介绍哪些操作可能会利用到内部临时表。如果用户在书写SQL语句的时候能够尽量少的使用内部临时表进行查询优化,将有效的提高查询执行的效率。
首先我们定义一个表t1,CREATE TABLE t1( a int, b int); INSERT INTO t1 VALUES(1,2),(3,4);
下面所有的操作都是基于表t1进行举例的。
- 在SQL语句中使用SQL_BUFFER_RESULT hint
SQL_BUFFER_RESULT主要用来让MySQL尽早的释放表上的锁。因为如果数据量很大的话,需要较长时间将数据发送到客户端,通过将数据缓冲到临时表中可以有效的减少读锁对表的占用时间。
例如:
mysql> explain format=json select SQL_BUFFER_RESULT * from t1;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "2.00"
},
"buffer_result": {
"using_temporary_table": true,
"table": {
"table_name": "t1",
"access_type": "ALL",
...
- 如果SQL语句中包含了DERIVED_TABLE。
在5.7中,由于采用了新的优化方式,我们需要使用 set optimizer_switch=’derived_merge=off’来禁止derived table合并到外层的Query中。
例如:
mysql> explain format=json select * from (select * from t1) as tt;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "2.40"
},
"table": {
"table_name": "tt",
"access_type": "ALL",
...
"materialized_from_subquery": {
"using_temporary_table": true,
...
- 如果我们查询系统表的话,系统表的数据将被存储到内部临时表中。
我们当前不能使用EXPLAIN来查看是否读取系统表数据需要利用到内部临时表,但是可以通过SHOW STATUS来查看是否利用到了内部临时表。
例如:
mysql> select * from information_schema.character_sets;
mysql> show status like 'CREATE%';
- 如果DISTINCT语句没有被优化掉,即DISTINCT语句被优化转换为GROUP BY操作或者利用UNIQUE INDEX消除DISTINCT, 内部临时表将会被使用。
mysql> explain format=json select distinct a from t1;
EXPLAIN
{
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "1.60"
},
"duplicates_removal": {
"using_temporary_table": true,
...
- 如果查询带有ORDER BY语句,并且不能被优化掉。下面几种情况会利用到内部临时表缓存中间数据,然后对中间数据进行排序。
1)如果连接表使用BNL(Batched Nestloop)/BKA(Batched Key Access)
例如:
1))BNL默认是打开的
mysql> explain format=json select * from t1, t1 as t2 order by t1.a;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "22.00"
},
"ordering_operation": {
"using_temporary_table": true,
...
2))关掉BNL后,ORDER BY将直接使用filesort。
mysql> set optimizer_switch='block_nested_loop=off';
Query OK, 0 rows affected (0.00 sec)
mysql> explain format=json select * from t1, t1 as t2 order by t1.a;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "25.00"
},
"ordering_operation": {
"using_filesort": true,
...
2)ORDER BY的列不属于执行计划中第一个连接表的列。
例如:
mysql> explain format=json select * from t as t1, t as t2 order by t2.a;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "25.00"
},
"ordering_operation": {
"using_temporary_table": true,
...
3)如果ORDER BY的表达式是个复杂表达式。
那么什么样的ORDER BY表达式,MySQL认为是复杂表达式呢?
1))如果排序表达式是SP或者UDF。
例如:
drop function if exists func1;
delimiter |
create function func1(x int)
returns int deterministic
begin
declare z1, z2 int;
set z1 = x;
set z2 = z1+2;
return z2;
end|
delimiter ;
explain format=json select * from t1 order by func1(a);
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "2.20"
},
"ordering_operation": {
"using_temporary_table": true,
...
2))ORDER BY的列包含聚集函数
为了简化执行计划,我们利用INDEX来优化GROUP BY语句。
例如:
create index idx1 on t1(a);
explain format=json SELECt a FROM t1 group by a order by sum(a);
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "1.20"
},
"ordering_operation": {
"using_temporary_table": true,
"using_filesort": true,
"grouping_operation": {
"using_filesort": false,
...
drop index idx1 on t1;
3))ORDER BY的列中包含有SCALAR SUBQUERY,当然该SCALAR SUBQUERY没有被优化掉。
例如:
explain format=json select (select rand() from t1 limit 1) as a from t1 order by a;
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "1.20"
},
"ordering_operation": {
"using_temporary_table": true,
"using_filesort": true,
...
4) 如果查询既带有ORDER BY同时也有GROUP BY语句,但是两个语句使用的列不相同。
注意: 如果是5.7,我们需要将sql_mode设置为非only_full_group_by模式,否则会报错。
同样为了简化执行计划,我们利用INDEX来优化GROUP BY语句。
例如:
set sql_mode='';
create index idx1 on t1(b);
explain format=json select t1.a from t1 group by t1.b order by 1;
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "1.40"
},
"ordering_operation": {
"using_temporary_table": true,
"using_filesort": true,
"grouping_operation": {
"using_filesort": false,
...
drop index idx1 on t1;
- 如果查询带有GROUP BY语句,并且不能被优化掉。下面几种情况会利用到内部临时表缓存中间数据,然后对中间数据进行GROUP BY。
1)如果连接表使用BNL(Batched Nestloop)/BKA(Batched Key Access)。
例如:
explain format=json select t2.a from t1, t1 as t2 group by t1.a;
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "8.20"
},
"grouping_operation": {
"using_temporary_table": true,
"using_filesort": true,
"cost_info": {
"sort_cost": "4.00"
...
2) 如果GROUP BY的列不属于执行计划中的第一个连接表。
例如:
explain format=json select t2.a from t1, t1 as t2 group by t2.a;
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "8.20"
},
"grouping_operation": {
"using_temporary_table": true,
"using_filesort": true,
"nested_loop": [
...
3) 如果GROUP BY语句使用的列与ORDER BY语句使用的列不同。
例如:
set sql_mode='';
explain format=json select t1.a from t1 group by t1.b order by t1.a;
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "1.40"
},
"ordering_operation": {
"using_filesort": true,
"grouping_operation": {
"using_temporary_table": true,
"using_filesort": false,
...
4) 如果GROUP BY带有ROLLUP并且是基于多表外连接。
例如:
explain format=json select sum(t1.a) from t1 left join t1 as t2 on true group by t1.a with rollup;
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "7.20"
},
"grouping_operation": {
"using_temporary_table": true,
"using_filesort": true,
"cost_info": {
"sort_cost": "4.00"
},
...
5) 如果GROUP BY语句使用的列来自于SCALAR SUBQUERY,并且没有被优化掉。
例如:
explain format=json select (select avg(a) from t1) as a from t1 group by a;
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "3.40"
},
"grouping_operation": {
"using_temporary_table": true,
"using_filesort": true,
"cost_info": {
"sort_cost": "2.00"
},
...
- IN表达式转换为semi-join进行优化
1) 如果semi-join执行方式为Materialization
例如:
set optimizer_switch='firstmatch=off,duplicateweedout=off';
explain format=json select * from t1 where a in (select b from t1);
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "5.60"
},
"nested_loop": [
{
"rows_examined_per_scan": 1,
"materialized_from_subquery": {
"using_temporary_table": true,
"query_block": {
"table": {
"table_name": "t1",
"access_type": "ALL",
...
2) 如果semi-join执行方式为Duplicate Weedout
例如:
set optimizer_switch='firstmatch=off';
explain format=json select * from t1 where a in (select b from t1);
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "4.80"
},
"duplicates_removal": {
"using_temporary_table": true,
"nested_loop": [
{
...
- 如果查询语句带有UNION,MySQL将利用内部临时表帮助UNION操作消除重复。
例如:
explain format=json select * from t1 union select * from t1;
| {
"query_block": {
"union_result": {
"using_temporary_table": true,
"table_name": "<union1,2>",
...
- 如果查询语句使用多表更新。
这里Explain不能看到内部临时表被利用,所以需要查看status。
例如:
update t1, t1 as t2 set t1.a=3;
show status like 'CREATE%';
- 如果聚集函数中包含如下函数,内部临时表也会被利用。
1) count(distinct *)
例如:
explain format=json select count(distinct a) from t1;
2) group_concat
例如:
explain format=json select group_concat(b) from t1;
总之,上面列出了10种情况,MySQL将利用内部临时表进行中间结果缓存,如果数据量比较大的话,内部临时表将会把数据存储在磁盘上,这样显然会对性能有所影响。为了尽可能的减少性能损失,我们需要尽量避免上述情况的出现。
MySQL · 特性分析 · 内部临时表的更多相关文章
- MySQL · 特性分析 · 优化器 MRR & BKA【转】
MySQL · 特性分析 · 优化器 MRR & BKA 上一篇文章咱们对 ICP 进行了一次全面的分析,本篇文章小编继续为大家分析优化器的另外两个选项: MRR & batched_ ...
- MySQL · 特性分析 · MDL 实现分析
http://mysql.taobao.org/monthly/2015/11/04/ 前言 在MySQL中,DDL是不属于事务范畴的,如果事务和DDL并行执行,操作相关联的表的话,会出现各种意想不到 ...
- MySQL · 特性分析 · innodb 锁分裂继承与迁移
http://mysql.taobao.org/monthly/2016/06/01/ innodb行锁简介 行锁类型 LOCK_S:共享锁 LOCK_X: 排他锁 GAP类型 LOCK_GAP:只锁 ...
- 【MySQL】查询使用临时表
MySQL查询产生临时表的分析 官网说明的地址:http://dev.mysql.com/doc/refman/5.5/en/internal-temporary-tables.html 参考:htt ...
- MySQL -- 内部临时表
本文转载自MySQL -- 内部临时表 UNION UNION语义:取两个子查询结果的并集,重复的行只保留一行 表初始化 CREATE TABLE t1(id INT PRIMARY KEY, a I ...
- MySQL性能分析, mysql explain执行计划详解
MySQL性能分析 MySQL性能分析及explain用法的知识是本文我们主要要介绍的内容,接下来就让我们通过一些实际的例子来介绍这一过程,希望能够对您有所帮助. 1.使用explain语句去查看分析 ...
- MySQL协议分析
MySQL协议分析 标签: mysql 2015-02-27 10:22 1807人阅读 评论(1) 收藏 举报 分类: 数据库(19) 目录(?)[+] 1 交互过程 MySQL客户端与 ...
- 【MariaDB】MariaDB vs MySQL - 特性
原文链接: https://mariadb.com/kb/en/mariadb-vs-mysql-features/ xiaomo译------ 支持更多的存储引擎 除了标配的MyISAM, BLAC ...
- mysql语句分析
explain的每个输出行提供一个表的相关信息,并且每个行包括下面的列: 1,id select识别符.这是select的查询序列号.2,select_type 可以为一下任何一种类型simple ...
随机推荐
- 消除QQ表情小游戏
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- SCP和SFTP(转)
原文:http://www.cnblogs.com/wang_yb/p/3819441.html 不管SCP还是SFTP,都是SSH的功能之一.都是使用SSH协议来传输文件的. 不用说文件内容,就是登 ...
- UNITY3D MAC版本破解
百度网盘下载地址: http://pan.baidu.com/s/1eQmvLqa#path=%252F 包含本体和破解文件 首先说明一下,如果是公司做开发建议去购买正版. 之前网上也有很多人贴出了破 ...
- Improved logging in Objective-C
[Improved logging in Objective-C] Example of logging the current method and line number. Paste it in ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- CodeForces 711D Directed Roads (DFS判环+计数)
题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...
- 理解C#值类型和引用类型
网上偶尔浏览到这一篇文章,还不错就修改了下分享给大家. 工作许久了,可是对C#值类型和C#引用类型却一直无法很好的理解.这两天花了不少时间查找资料,看文章,终于有所收获,在此将自己理解整理出来,方便日 ...
- Add mappings to an Elasticsearch index in realtime
Changing mapping on existing index is not an easy task. You may find the reason and possible solutio ...
- mvc中ajax.beginform一次提交重复Post两次的问题解决
在MVC4中使用ajax.beginform来做添加商品到购物车中的提交操作,结果点击提交按钮后,出现两次post,这样导致商品的数量增加了一倍. 原因:@Scripts.Render("~ ...
- SQL Server中行列转换
典型实例 一.行转列 1.建立表格 ifobject_id('tb')isnotnulldroptabletb go createtabletb(姓名varchar(10),课程varchar(10) ...