在某些场景中,需要对派生表生成临时结果集进行materialized,如果该临时结果集中包含索引键,那么查询有可能通过该索引键来进行优化。

如对下面查询:

SELECT
T2.purpose_code,
T1.instance_count
FROM `assets_cluster` AS T2
STRAIGHT_JOIN(
SELECT
cluster_id,
COUNT(1) AS instance_count
FROM `assets_instance`
GROUP BY `cluster_id`
HAVING COUNT(1)>1
) AS T1
ON T1.cluster_id=T2.cluster_id

对应查询在执行计划为:

*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: T2
partitions: NULL
type: index
possible_keys: PRIMARY
key: idx_purpose_code
key_len: 387
ref: NULL
rows: 684
filtered: 100.00
Extra: Using index
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: <derived2>
partitions: NULL
type: ref
possible_keys: <auto_key0>
key: <auto_key0>
key_len: 8
ref: exps_db.T2.cluster_id
rows: 10
filtered: 100.00
Extra: NULL
*************************** 3. row ***************************
id: 2
select_type: DERIVED
table: assets_instance
partitions: NULL
type: index
possible_keys: ForeignKey_cluster_id
key: ForeignKey_cluster_id
key_len: 8
ref: NULL
rows: 1727
filtered: 100.00
Extra: Using index

对应的执行计划JOSN为:

{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "10397.97"
},
"nested_loop": [
{
"table": {
"table_name": "T2",
"access_type": "index",
"possible_keys": [
"PRIMARY"
],
"key": "idx_purpose_code",
"used_key_parts": [
"purpose_code"
],
"key_length": "387",
"rows_examined_per_scan": 851,
"rows_produced_per_join": 851,
"filtered": "100.00",
"using_index": true,
"cost_info": {
"read_cost": "10.00",
"eval_cost": "170.20",
"prefix_cost": "180.20",
"data_read_per_join": "15M"
},
"used_columns": [
"cluster_id",
"purpose_code"
]
}
},
{
"table": {
"table_name": "T1",
"access_type": "ref",
"possible_keys": [
"<auto_key0>"
],
"key": "<auto_key0>",
"used_key_parts": [
"cluster_id"
],
"key_length": "8",
"ref": [
"exps_db.T2.cluster_id"
],
"rows_examined_per_scan": 10,
"rows_produced_per_join": 8514,
"filtered": "100.00",
"cost_info": {
"read_cost": "8514.81",
"eval_cost": "1702.96",
"prefix_cost": "10397.97",
"data_read_per_join": "199K"
},
"used_columns": [
"cluster_id",
"instance_count"
],
"materialized_from_subquery": {
"using_temporary_table": true,
"dependent": false,
"cacheable": true,
"query_block": {
"select_id": 2,
"cost_info": {
"query_cost": "372.20"
},
"grouping_operation": {
"using_filesort": false,
"table": {
"table_name": "assets_instance",
"access_type": "index",
"possible_keys": [
"ForeignKey_cluster_id"
],
"key": "ForeignKey_cluster_id",
"used_key_parts": [
"cluster_id"
],
"key_length": "8",
"rows_examined_per_scan": 1771,
"rows_produced_per_join": 1771,
"filtered": "100.00",
"using_index": true,
"cost_info": {
"read_cost": "18.00",
"eval_cost": "354.20",
"prefix_cost": "372.20",
"data_read_per_join": "5M"
},
"used_columns": [
"instance_id",
"cluster_id"
]
}
}
}
}
}
}
]
}
}

由于查询中使用STRAIGHT_JOIN关键词,将assets_cluster作为外表,派生表结果集作为内表,由于内表上包含cluster_id的索引(auto_key0),因此采用NEST LOOP方式循环外表的每行记录对内表做KEY LOOKUP查询。

MySQL--派生表临时结果集中的AutoKey的更多相关文章

  1. MySQL派生表(derived)优化一例

    1.什么是派生表derived 关键字:子查询–>在From后where前的子查询 mysql; +----+-------------+------------+------+-------- ...

  2. MySQL 派生表(Derived Table) Merge Optimization

    本文将通过演示告诉你:MySQL中派生表(Derived Table)是什么?以及MySQL对它的优化. Background 有如下一张表: mysql> desc city; +------ ...

  3. 数据库~Mysql派生表注意的几点~关于百万数据的慢查询问题

    基础概念 派生表是从SELECT语句返回的虚拟表.派生表类似于临时表,但是在SELECT语句中使用派生表比临时表简单得多,因为它不需要创建临时表的步骤. 术语:*派生表*和子查询通常可互换使用.当SE ...

  4. MySQL临时表与派生表(简略版)

    MySQL临时表与派生表 当主查询中包含派生表,或者当select 语句中包含union字句,或者当select语句中包含一个字段的order by 子句(对另一个字段的group by 子句)时,M ...

  5. MySQL子查询,派生表和通用表达式

    一:子查询 1.介绍 在另一个查询(外部查询)中嵌套另一个查询语句(内部查询),并使用内部查询的结果值作为外部查询条件. 2.子查询在where中 SELECT customerNumber, che ...

  6. MYSQL优化派生表(子查询)在From语句中的

    Mysql 在5.6.3中,优化器更有效率地处理派生表(在from语句中的子查询): 优化器推迟物化子查询在from语句中的子查询,知道子查询的内容在查询正真执行需要时,才开始物化.这一举措提高了性能 ...

  7. MySQL 子查询(三) 派生表、子查询错误

    From MySQL 5.7 ref:13.2.10.8 Derived Tables 八.派生表 派生表是一个表达式,用于在一个查询的FROM子句的范围内生成表. 例如,在一个SELECT查询的FR ...

  8. MySql 学习笔记 (派生表)

    派生表也是一种子查询那么它出现在 select * from ( select * from b <--这个就是派生表啦 )派生表其实不是个好东西,在生产的时候他是可以通过索引来过滤的,但是一但 ...

  9. MySQL-子查询,派生表,通用表达式

    MySQL-子查询 MySQL子查询是嵌套在另一个查询中的查询. MySQL子查询还可以嵌套在另一个子查询中. MySQL子查询称为内部查询,而包含子查询的查询称为外部查询. 查询返回在位于美国(US ...

随机推荐

  1. 查看json数据更新情况

    #! python3 # -*- coding:utf8 -*- #主要为读取excel中接口地址,打开网页爬取url页面中数据,解析json,检查是否符合逻辑(正常) import requests ...

  2. 第二章 使用unittest模块扩展功能测试

    2.1使用功能测试驱动开放一个最简单的应用 # functional_tests.py # -*- coding: utf-8 -*- from selenium import webdriver b ...

  3. IE浏览器兼容的处理方式之一,使用特殊的注释 <!--[if IE]> ....<![endif]-->

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. centos tar 常用

    tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...

  5. Positioning

      boxPostion.html <html><head> <title>Box Position</title><meta charset=& ...

  6. powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model.

    powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model ...

  7. vim 使用和配置

    vim 启动的时候,默认加载用户目录下.vimrc的配置文件,如果不存在,则会加载系统配置文件/etc/vim/vimrc ~/.vimrc文件配置 #避免中文乱码set fileencodings= ...

  8. mysql引擎事物支持

    事务的特性事务具体四大特性,也就是经常说的ACID 1. 原子性(Atomicity) 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚,因此事务的操作如果成功就必须要完全应用到数据库,如果 ...

  9. JS数据的基本类型

    字符串   String 数字    Number 布尔    Boolean Null     空 Undefined Object   对象  Array 数组   json   function ...

  10. Python 静态方法

    class Person: @staticmethod # 静态方法 def yue(): print("fsadf") # 静态方法可以使用对象访问. 也可以使用类名访问. 但是 ...