使用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 ...
随机推荐
- BZOJ4419:[SHOI2013]发微博(乱搞)
Description 刚开通的SH微博共有n个用户(1..n标号),在短短一个月的时间内,用户们活动频繁,共有m条按时间顺序的记录: ! x 表示用户x发了一条微博: + x y 表示用户x和用 ...
- BZOJ4827:[HNOI2017]礼物(FFT)
Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手环,一个留给自己,一 个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在 ...
- PHP 基础系列(三) 【转】PHP 函数实现原理及性能分析
作者:HDK (百度) 前言 在任何语言中,函数都是最基本的组成单元.对于PHP的函数,它具有哪些特点?函数调用是怎么实现的?php函数的性能如何,有什么使用建议?本文将从原理出发进行分析结合实际的性 ...
- es6之数组方法
//兼容插件 babel-polyfill values()等存在兼容问题,需要加载babel-polyfill插件 .keys() 获取数组的key值 .values() 获取数组的value值 ...
- 课时53.video标签(掌握)
这节课来学习一下html5中新增的标签,我们先来看一下,html5中新增了哪些标签? 打开W3school的网页,点击参考手册中的HTML/HTML5标签,有一个按字母顺序排列的标签,但凡标签后面带有 ...
- Dokcer-ce安装脚本
安装docker #!/bin/bash # coding: utf- # Copyright (c) set -e #返回值为0时,退出脚本 echo "1. 备份yum" { ...
- center os 7 修改 mysql 密码
笔记 本redis 密码 654321忘记密码:修改密码update user set authentication_string=password('123456') where user='roo ...
- iOS之在AppDelegate中push到指定页面
UITabBarController *tbc = (UITabBarController *)self.window.rootViewController; UINavigationControll ...
- iOS 12 真机调试 Xcode 9 提示 Could not locate device support files.
升级 iOS 12 之后,使用 Xcode 9 真机调试会提示错误: Could not locate device support files. This iPhone 6 Plus is runn ...
- TinyMCE插件:Filemanager [4.x-6.x] 文件名统一格式化
上传图片程序(filemanager/upload.php) 在if (!empty($_FILES) && $upload_files)中上传图片时,在文件正式上传至服务器前,有一次 ...