使用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 ...
随机推荐
- VS 2013 代码注释掉,编译不执行问题
先说说我遇到的问题吧.修改别人的代码,注释原来的代码或者添加新的代码,都不执行,还是编译原来的代码.而且还有一个错误:lc.exe 已退出 代码为 -1 解决方法: 1.先把lc.exe 已退出 代码 ...
- Vue通过input筛选数据
<div id="app"> <input v-model='search' /> <ul> <li v-for="item i ...
- XXE攻防——XML外部实体注入
XXE攻防——XML外部实体注入 转自腾讯安全应急响应中心 一.XML基础知识 XML用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的 ...
- 在servlet中使用Spring注入
修改servlet 的 init 方法,添加以下代码: SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, ...
- 二十、在Intellij IDEA中使用Debug
Debug用来追踪代码的运行流程,通常在程序运行过程中出现异常,启用Debug模式可以分析定位异常发生的位置,以及在运行过程中参数的变化.通常我们也可以启用Debug模式来跟踪代码的运行流程去学习三方 ...
- HDU 2089(暴力和数位dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others) M ...
- HDU 1273 漫步森林(数学 找规律)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1273 漫步森林 Time Limit: 2000/1000 MS (Java/Others) M ...
- STM32F103 ucLinux开发之四(内核启动后的调试)
Stm32-uclinux启动后的调试 1. 修改__pfn_to_page使得能够启动 根据STM32F103 ucLinux开发之三(内核启动后不正常)的描述,内核无法启动是选择了平板内存模式后 ...
- js获取浏览器上一访问页面URL地址,document.referrer方法
如题,可用document.referrer方法获取上一页面的url 但是也有不可使用的情况 直接在浏览器地址栏中输入地址: 使用location.reload()刷新(location.href或者 ...
- 通过 openURL 方法跳转至设置 - iOS
iOS 10 以下系统版本可以通过 openURL 的方式跳转至指定的设置界面,code 如下: NSURL *url = [NSURL URLWithString:@"prefs:root ...