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": "",
  ...
  如果查询语句使用多表更新。
  这里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;
  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将利用内部临时表进行中间结果缓存,如果数据量比较大的话,内部临时表将会把数据存储在磁盘上,这样显然会对性能有所影响。为了尽可能的减少性能损失,我们需要尽量避免上述情况的出现。