MySQL--派生表临时结果集中的AutoKey
在某些场景中,需要对派生表生成临时结果集进行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的更多相关文章
- MySQL派生表(derived)优化一例
1.什么是派生表derived 关键字:子查询–>在From后where前的子查询 mysql; +----+-------------+------------+------+-------- ...
- MySQL 派生表(Derived Table) Merge Optimization
本文将通过演示告诉你:MySQL中派生表(Derived Table)是什么?以及MySQL对它的优化. Background 有如下一张表: mysql> desc city; +------ ...
- 数据库~Mysql派生表注意的几点~关于百万数据的慢查询问题
基础概念 派生表是从SELECT语句返回的虚拟表.派生表类似于临时表,但是在SELECT语句中使用派生表比临时表简单得多,因为它不需要创建临时表的步骤. 术语:*派生表*和子查询通常可互换使用.当SE ...
- MySQL临时表与派生表(简略版)
MySQL临时表与派生表 当主查询中包含派生表,或者当select 语句中包含union字句,或者当select语句中包含一个字段的order by 子句(对另一个字段的group by 子句)时,M ...
- MySQL子查询,派生表和通用表达式
一:子查询 1.介绍 在另一个查询(外部查询)中嵌套另一个查询语句(内部查询),并使用内部查询的结果值作为外部查询条件. 2.子查询在where中 SELECT customerNumber, che ...
- MYSQL优化派生表(子查询)在From语句中的
Mysql 在5.6.3中,优化器更有效率地处理派生表(在from语句中的子查询): 优化器推迟物化子查询在from语句中的子查询,知道子查询的内容在查询正真执行需要时,才开始物化.这一举措提高了性能 ...
- MySQL 子查询(三) 派生表、子查询错误
From MySQL 5.7 ref:13.2.10.8 Derived Tables 八.派生表 派生表是一个表达式,用于在一个查询的FROM子句的范围内生成表. 例如,在一个SELECT查询的FR ...
- MySql 学习笔记 (派生表)
派生表也是一种子查询那么它出现在 select * from ( select * from b <--这个就是派生表啦 )派生表其实不是个好东西,在生产的时候他是可以通过索引来过滤的,但是一但 ...
- MySQL-子查询,派生表,通用表达式
MySQL-子查询 MySQL子查询是嵌套在另一个查询中的查询. MySQL子查询还可以嵌套在另一个子查询中. MySQL子查询称为内部查询,而包含子查询的查询称为外部查询. 查询返回在位于美国(US ...
随机推荐
- Android : 添加apk私有权限
一.Android的系统权限: apk在安装时,Android 为每个软件包提供唯一的 Linux 用户 ID.此 ID 在软件包在该设备上的使用寿命期间保持不变.在不同设备上,相同软件包可能有不同的 ...
- jdk8-全新时间和日期api
1.jdk8日期和时间api是线程安全的 1.java.time 处理日期时间 2.java.time.temporal: 时间校正器.获取每个月第一天,周几等等 3.java.time.forma ...
- python笔记1-基础概念、python安装使用配置
Python 1.基础概念 一.什么是python? python是一种面向对象.解释型的计算机语言,它的特点是语法简洁.优雅.简单易学.在1989诞生,Guido(龟叔)开发.这里的python并不 ...
- 8.Python爬虫实战一之爬取糗事百科段子
大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...
- normalization 阅读笔记
https://zhuanlan.zhihu.com/p/33173246 阅读笔记 1. normalization whiting - PCA 2. Internal Covariate Shif ...
- L265 - 5 questions to ask yourself before you ask for a raise or promotion
You’ve been in your role for a while now, giving 110% to every assignment your manager hands out. Yo ...
- phpstom pojie
https://blog.csdn.net/gu_wen_jie/article/details/79136475
- crontab入门及进阶学习笔记
crontab不是通常意义下的linux指令,它更是一个配置工具.通过这个工具,我们可以为系统定制固定周期的任务. 1.crond和crontab 1) crond:cron服务的守护进程,用于定 ...
- springboot (spring mvc)集成swagger
最近用springboot构建rest接口,考虑到最方便的验证接口,想到了引入swagger. 基本的步骤大致如下: 1.pom中引入swagger依赖: <dependency> < ...
- 前端_标签01_input标签
<c:forEach items="${itemList }" var="item" varStatus="status">&l ...