使用union 外加count
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的更多相关文章
- C语言学习015:联合(union)与枚举(enum)
联合 联合和结构的区别是,结构会为每个字段申请一片内存空间,而联合只是申请了一片内存空间然后所有字段都会保存到这片空间中,这片空间的大小由字段中最长的决定,下面我们就开始定义一个联合 //联合的定义 ...
- mysql合并 两个count语句一次性输出结果的方法
mysql合并 两个count语句一次性输出结果的方法 需求场景:经常要查看有两个表统计数,用SELECT COUNT(*) FROM hotcontents,SELECT COUNT(*) FROM ...
- High Performance MySQL笔记:count
在SQL中使用count()好像是非常自然的事情: SELECT COUNT(*) FROM TABLE_NAME; 有时候确实会想过,count(*)和单独的count(column_name)有什 ...
- LeetCode 1101. The Earliest Moment When Everyone Become Friends
原题链接在这里:https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/ 题目: In a soc ...
- c语言知识点总结(摘自head first c)
gcc name.c -o name; ./name或者gcc name.c -o name && ./name;同时执行关键字:void sizeof(运算符,它能告诉你某样东 ...
- 常用的一些复杂SQL语句
1.根据表中的birthday统计年龄段人数: //以下代码表示查询出来后的结果集添加一列字段 cast('20以下' as char) as age SELECT COUNT((DATE_FORMA ...
- 高级sql注入
1. 避开输入过滤 输入过滤存在于外部和内部,外部属于web应用防火墙WAF,入侵防御系统IPS,入侵检测系统IDS,内部属于代码中对输入进行过滤 过滤select,insert等sql关键字和' | ...
- EF 5 最佳实践白皮书
Performance Considerations for Entity Framework 5 By David Obando, Eric Dettinger and others Publish ...
- 导入CSV格式的数据
导入CSV格式的数据 (参见http://dev.mysql.com/doc/refman/5.6/en/load-data.html) 1.数据库表(st_pptn_r) CREATE TABLE ...
随机推荐
- 实用技巧:利用Excel实现客户档案的统一管理
背景: 一个朋友新开了家门市,生意不错,客源旺盛. 有次我们喝茶时,他透露一个问题,就是客户的档案管理很不理想,都是纸面的,很容易丢失,也不方便查找. 我自诩混迹IT界多年,当然要替好友解决这个小麻烦 ...
- thinkphp 和 laravel使用sql语句操作db和源码浅析
thinkphp 和 laravel是phper开发中用的比较多的两个框架,无所谓好坏,看个人习惯及喜爱! 前言对于一个PHP应用,可能最多的就是操作数据,以致于初学者有时只把php当做数据库增删查改 ...
- Mybatis的mapper代理开发方法
一.开发规范 1.映射文件中的namespase等于mapper接口类路径 2.statement的id与mapper中的方法名一致 3.让mapper的接口方法输入参数类型与statement中的p ...
- Anaconda常用命令
conda版本: conda --version 环境信息: 激活环境后,conda info 查看环境已安装包: conda list 新建环境: conda create -n {NAME} [ ...
- [LuoguP3195] [HNOI2008]玩具装箱TOY
[HNOI2008]玩具装箱(Link) 题目描述 \(P\)教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊 ...
- labview
1.labview适用于测试测量领域,用于快速构建PC端上位机程序 2.例子: 我的电气参数测试仪. 上位机软件比想象的做的快了十几天... 学校的创新基金可以交差了,界面还是个人比较满意了.. 上图 ...
- Flask—09-项目部署(01)
项目部署 WEB工作原理 客户端(chrom) <=> WEB服务器(nginx) <=> WSGI(uWSGI) <=> Python(Flask) <=& ...
- E. K Balanced Teams
类比背包问题,为每个学生附加一个权重$pos[i]$,意思是选择该学生后,之后可以选择$p[i]~p[i]+5$的学生. 转换公式: $$d[i][j]=max(d[i+1][q],d[i+pos][ ...
- 【LAMP整合Redis键值缓存为库分担压力】
LAMP+ redis 架构图: 安装phpredis扩展 Php主配置文件引入redis库文件 Redis扩展 // 对httpd php扩展连接指定redis服务器
- CSS中的动画
1.transition 在CSS3中,可以通过transition为元素从一种样式变换为另外一种样式的过程添加效果. transition为简写属性,用于在一个属性中设置四个过渡属性,分别是: tr ...