在某些场景中,需要对派生表生成临时结果集进行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. RabbitMQ 队列、消息持久化

    RabbitMQ的消息队列的持久化是一个很不错的功能,设置也非常简单.如下代码: 1.设置队列持久化(在声明队列的时候设置) channel.QueueDeclare(queue: "q.l ...

  2. vue-6-事件处理

    <div id="example-2"> <button v-on:click="greet">Greet</button> ...

  3. thymeleaf之下拉框回显选中

    #1.select下拉框取值    <div class="form-group ">        <label id="resource" ...

  4. div 自适应高度

    自适应高度 ,设置最小高度:通常情况下,没有设置高度,div默认自适应高度且无最低高度 1 div{ _height:200px; /* css 注解: 仅IE6设别此属性,假定最低高度是200px ...

  5. Python如何操作redis

    做UI自动化时,遇到一个问题,需要在后台操作完成后,产生结果才能在前端进行操作,但是用自动化在后台操作又很麻烦,就想直接操作数据库,然后再 在前端进行操作:这时遇到一个问题,在后台操作时,会写入到数据 ...

  6. MVC实现上传图片的方法

    Form提交时,须注意form需要添加属性enctype="multipart/form-data",否则Request.Files.Count=0,无法上传图片. cshtml代 ...

  7. pl/sql developer中dbms_output.put_line函数的运用

    pl/sql developer中dbms_output.put_line函数可以打印想显示在屏幕上的信息,运用时需要注意几点: 1 必须处于begin   ...  end: 2 需要先执行 set ...

  8. JavaWeb:c3p0配置问题-----java.sql.SQLException: Connections could not be acquired from the underlying database!

    错误原因 c3p0的配置错误 错误显示 -classpath "D:\Program\Software\IntelliJIDEA\IntelliJ IDEA 2018.2.5\lib\ide ...

  9. linux各种系统下载地址

    1.Arch Linux Arch Linux在安装过程中提供了强大的可定制选择,支持你下载和安装自己所需的程序包.虽然这个选择对新手来说没有多大的帮助,但是它确实能够帮助那些使用Arch构建系统和存 ...

  10. vue 路由跳转,路由传参的几种方式

    1. router-link <router-link :to="{ path: 'yourPath', params: { name: 'name', dataObj: data } ...