explain extended select count(*) from (select id from states where country_id < 100 union (select id from countries where id > 10 and name='aaa')) t;
+----+--------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 4356 | 100.00 | NULL |
| 2 | DERIVED | states | ALL | NULL | NULL | NULL | NULL | 4120 | 100.00 | Using where |
| 3 | UNION | countries | range | PRIMARY | PRIMARY | 4 | NULL | 236 | 100.00 | Using where |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+--------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
QUERY: explain extended select count(*) from (select id from states where country_id < 100 union (select id from countries where id > 10 and name='aaa')) t
TRACE: {
"steps": [
{
"join_preparation": {
"select#": 2,
"steps": [
{
"expanded_query": "/* select#2 */ select `states`.`id` AS `id` from `states` where (`states`.`country_id` < 100)"
}
]
}
},
{
"join_preparation": {
"select#": 3,
"steps": [
{
"expanded_query": "/* select#3 */ select `countries`.`id` AS `id` from `countries` where ((`countries`.`id` > 10) and (`countries`.`name` = 'aaa'))"
}
]
}
},
{
"creating_tmp_table": {
"tmp_table_info": {
"table": "intermediate_tmp_table",
"row_length": 5,
"key_length": 4,
"unique_constraint": false,
"location": "memory (heap)",
"row_limit_estimate": 3355443
}
}
},
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select count(0) AS `count(*)` from (/* select#2 */ select `states`.`id` AS `id` from `states` where (`states`.`country_id` < 100) union (/* select#3 */ select `countries`.`id` AS `id` from `countries` where ((`countries`.`id` > 10) and (`countries`.`name` = 'aaa')))) `t`"
}
]
}
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"join_optimization": {
"select#": 2,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "(`states`.`country_id` < 100)",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "(`states`.`country_id` < 100)"
},
{
"transformation": "constant_propagation",
"resulting_condition": "(`states`.`country_id` < 100)"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "(`states`.`country_id` < 100)"
}
]
}
},
{
"table_dependencies": [
{
"table": "`states`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"ref_optimizer_key_uses": [
]
},
{
"rows_estimation": [
{
"table": "`states`",
"table_scan": {
"rows": 4120,
"cost": 12
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": "`states`",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "scan",
"rows": 4120,
"cost": 836,
"chosen": true
}
]
},
"cost_for_plan": 836,
"rows_for_plan": 4120,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`states`.`country_id` < 100)",
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": "`states`",
"attached": "(`states`.`country_id` < 100)"
}
]
}
},
{
"refine_plan": [
{
"table": "`states`",
"access_type": "table_scan"
}
]
}
]
}
},
{
"join_optimization": {
"select#": 3,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "((`countries`.`id` > 10) and (`countries`.`name` = 'aaa'))",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "((`countries`.`id` > 10) and (`countries`.`name` = 'aaa'))"
},
{
"transformation": "constant_propagation",
"resulting_condition": "((`countries`.`id` > 10) and (`countries`.`name` = 'aaa'))"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "((`countries`.`id` > 10) and (`countries`.`name` = 'aaa'))"
}
]
}
},
{
"table_dependencies": [
{
"table": "`countries`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"ref_optimizer_key_uses": [
]
},
{
"rows_estimation": [
{
"table": "`countries`",
"range_analysis": {
"table_scan": {
"rows": 246,
"cost": 52.3
},
"potential_range_indices": [
{
"index": "PRIMARY",
"usable": true,
"key_parts": [
"id"
]
}
],
"setup_range_conditions": [
],
"group_index_range": {
"chosen": false,
"cause": "not_group_by_or_distinct"
},
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "PRIMARY",
"ranges": [
"10 < id"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": true,
"using_mrr": false,
"index_only": false,
"rows": 236,
"cost": 48.419,
"chosen": true
}
],
"analyzing_roworder_intersect": {
"usable": false,
"cause": "too_few_roworder_scans"
}
},
"chosen_range_access_summary": {
"range_access_plan": {
"type": "range_scan",
"index": "PRIMARY",
"rows": 236,
"ranges": [
"10 < id"
]
},
"rows_for_plan": 236,
"cost_for_plan": 48.419,
"chosen": true
}
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": "`countries`",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "range",
"rows": 236,
"cost": 95.619,
"chosen": true
}
]
},
"cost_for_plan": 95.619,
"rows_for_plan": 236,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "((`countries`.`id` > 10) and (`countries`.`name` = 'aaa'))",
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": "`countries`",
"attached": "((`countries`.`id` > 10) and (`countries`.`name` = 'aaa'))"
}
]
}
},
{
"refine_plan": [
{
"table": "`countries`",
"access_type": "range"
}
]
}
]
}
},
{
"table_dependencies": [
{
"table": " `t`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"rows_estimation": [
{
"table": " `t`",
"table_scan": {
"rows": 4356,
"cost": 227
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": " `t`",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "scan",
"rows": 4356,
"cost": 1099,
"chosen": true
}
]
},
"cost_for_plan": 1099,
"rows_for_plan": 4356,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": null,
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": " `t`",
"attached": null
}
]
}
},
{
"refine_plan": [
{
"table": " `t`",
"access_type": "table_scan"
}
]
}
]
}
},
{
"join_explain": {
"select#": 1,
"steps": [
{
"creating_tmp_table": {
"tmp_table_info": {
"table": " `t`",
"row_length": 5,
"key_length": 0,
"unique_constraint": false,
"location": "memory (heap)",
"row_limit_estimate": 3355443
}
}
},
{
"join_explain": {
"select#": 2,
"steps": [
]
}
},
{
"join_explain": {
"select#": 3,
"steps": [
]
}
},
{
"join_preparation": {
"select#": "fake",
"steps": [
{
"expanded_query": "/* select#fake */ select `id` AS `id` from dual"
}
]
}
},
{
"join_optimization": {
"select#": "fake",
"steps": [
{
"table_dependencies": [
{
"table": "``.``",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"rows_estimation": [
{
"table": "``.``",
"table_scan": {
"rows": 0,
"cost": 10
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": "``.``",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "scan",
"rows": 0,
"cost": 10,
"chosen": true
}
]
},
"cost_for_plan": 10,
"rows_for_plan": 0,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": null,
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": "``.``",
"attached": null
}
]
}
},
{
"refine_plan": [
{
"table": "``.``",
"access_type": "table_scan"
}
]
}
]
}
},
{
"join_explain": {
"select#": "fake",
"steps": [
]
}
}
]
}
}
]
}

使用union 外加count的更多相关文章

  1. C语言学习015:联合(union)与枚举(enum)

    联合 联合和结构的区别是,结构会为每个字段申请一片内存空间,而联合只是申请了一片内存空间然后所有字段都会保存到这片空间中,这片空间的大小由字段中最长的决定,下面我们就开始定义一个联合 //联合的定义 ...

  2. mysql合并 两个count语句一次性输出结果的方法

    mysql合并 两个count语句一次性输出结果的方法 需求场景:经常要查看有两个表统计数,用SELECT COUNT(*) FROM hotcontents,SELECT COUNT(*) FROM ...

  3. High Performance MySQL笔记:count

    在SQL中使用count()好像是非常自然的事情: SELECT COUNT(*) FROM TABLE_NAME; 有时候确实会想过,count(*)和单独的count(column_name)有什 ...

  4. LeetCode 1101. The Earliest Moment When Everyone Become Friends

    原题链接在这里:https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/ 题目: In a soc ...

  5. c语言知识点总结(摘自head first c)

    gcc name.c -o name;   ./name或者gcc name.c -o name &&  ./name;同时执行关键字:void sizeof(运算符,它能告诉你某样东 ...

  6. 常用的一些复杂SQL语句

    1.根据表中的birthday统计年龄段人数: //以下代码表示查询出来后的结果集添加一列字段 cast('20以下' as char) as age SELECT COUNT((DATE_FORMA ...

  7. 高级sql注入

    1. 避开输入过滤 输入过滤存在于外部和内部,外部属于web应用防火墙WAF,入侵防御系统IPS,入侵检测系统IDS,内部属于代码中对输入进行过滤 过滤select,insert等sql关键字和' | ...

  8. EF 5 最佳实践白皮书

    Performance Considerations for Entity Framework 5 By David Obando, Eric Dettinger and others Publish ...

  9. 导入CSV格式的数据

    导入CSV格式的数据 (参见http://dev.mysql.com/doc/refman/5.6/en/load-data.html) 1.数据库表(st_pptn_r) CREATE TABLE ...

随机推荐

  1. [BJOI2018]治疗之雨

    题目 我还没疯 发现如果我们将血量抽象成点,一轮操作抽象成图上的一条边,我们如果能求出每一条边的概率,我们就能搞一下这道题 假设我们求出了这个图\(E\),设\(dp_i\)表示从\(i\)点到达\( ...

  2. CF1063A Oh Those Palindromes

    嘟嘟嘟 只要将每一种字母放一块输出就行了. 证明1:比如 1 2 3 4 5 6,那么这个序列对答案的贡献分别是1和5,2和4 ,3和6……如果重新排列成x x x x o o,会发现对        ...

  3. zabbix时间不同步

    Linux下如何实现与Internet时间同步 一.安装ntp [root@server-2 ~]# yum install -y ntpdate 二.同步时间 // 方式一.使用域名连接,要经过DN ...

  4. SPOJ SUBXOR

    SPOJ SUBXOR 题意 给定一个由正整数构成的数组, 求 异或和小于k 的子序列的个数. 题解 假设答案区间为 [L, R], XOR[L, R] 等价于 XOR[1, L - 1] ^ XOR ...

  5. HDU 1290 献给杭电五十周年校庆的礼物(面分割空间 求得到的最大空间数目)

    传送门: 献给杭电五十周年校庆的礼物 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  7. hdu 1010(迷宫搜索,奇偶剪枝)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  8. Mark一下在模仿团购App搭建页面时犯的低级错误

    1.关于Xib拖线错误 2.下面这个错误的根源其实是代码提示时直接敲下了回车,没看仔细,导致后来找了好久才发现该错误,郁闷啊!

  9. [转载]对iOS开发中内存管理的一点总结与理解

    对iOS开发中内存管理的一点总结与理解   做iOS开发也已经有两年的时间,觉得有必要沉下心去整理一些东西了,特别是一些基础的东西,虽然现在有ARC这种东西,但是我一直也没有去用过,个人觉得对内存操作 ...

  10. Java中集合随笔

    先上一张图:关于collection接口的 一.Collection中的常用功能: boolean add(Object e): 向集合中添加元素void clear():清空集合中所有元素boole ...