【MySQL 源码】UNION 比 UNION ALL 的性能差很多吗?
在数据库实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《【MySQL 源码】UNION 比 UNION ALL 的性能差很多吗?》,聊聊MySQL、源码、MySQL优化、C++、源码分析,希望可以帮助到正在努力赚钱的你。
原文地址: 【MySQL 源码】UNION 比 UNION ALL 的性能差很多吗?
欢迎访问我的个人博客: http://blog.duhbb.com/
引言
本文从源码角度分析了一下 MySQL 中 union 和 union all 的区别;得出了以下结论: union 和 union all 都会创建临时表, 但是又不太一样; 二者的查询计划不一样;union 默认会创建一个以返回列作为 key 的临时表, 所谓过滤就是将数据插入这个临时表; 临时表装数据的容器实际上是一个 unordered_set; 有一种存储引擎叫做临时表; union all 则是直接读取表的数据并返回给客户端, 不走临时表; union all 和 union 的场景还是得根据需要来判断, 如果没有 distinct 的需求话, 数据又不多, 可以考虑使用 union all.
Union 和 Union All 的区别
Union 和 Union All 之间的唯一区别是 Union All 不会删除重复的行或记录, 而是从所有表中选择满足您的具体查询条件的所有行并将它们组合到结果表中.
UNION 不适用于具有文本数据类型的列. 而 UNION ALL 适用于所有数据类型列.
MySQL 官方介绍
MySQL 官方文档在介绍 12.5 Non-Subquery UNION Execution 是这么说的:
非子查询联合 (non-subquery unions) 是在
void Query_expression::create_access_paths(THD *thd) { // 确定我们是否可以流式读取行, 即永远不需要将它们放到临时表中 // 如果可以的话, 我们会首先物化 UNION DISTINCT blocks, 然后将剩余任何 UNION ALL block // 通过 AppendIterator 追加. // // 如果不能流式的话, 即每个块都必须进入临时表 // 我们对于混合的 UNION ALL/DISTINCT 的策略有点不同 // 详情见 MaterializeIterator. bool streaming_allowed = true; if (global_parameters()->order_list.size() != 0) { // If we're sorting, we currently put it in a real table no matter what. // This is a legacy decision, because we used to not know whether filesort // would want to refer to rows in the table after the sort (sort by row ID). // We could probably be more intelligent here now. streaming_allowed = false; } // 省略前面 // 如果允许流式查询, 那么我们可以对 UNION ALL 的每个部分都做流式查询, // 而其他情况则都需要用到临时表. // // 处理我们需要物化的所有的 query block. // 这个可能是 UNION DISTINCT 的 query block 或者所有的 block. if (union_distinct != nullptr || ! streaming_allowed) { Mem_root_array<:queryblock> query_blocks = setup_materialization(thd, tmp_table, streaming_allowed); // 省略后面 </:queryblock>
光看代码感觉一头雾水, 还是 debug 一下吧.
if (! simple_query_expression) { /* Check that it was possible to aggregate all collations together for UNION. We need this in case of UNION DISTINCT, to filter out duplicates using the proper collation. TODO: consider removing this test in case of UNION ALL. */ for (Item *type : types) { if (type->result_type() == STRING_RESULT && type->collation.derivation == DERIVATION_NONE) { my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION"); return true; } } ulonglong create_options = first_query_block()->active_options() | TMP_TABLE_ALL_COLUMNS; if (union_result->create_result_table(thd, types, union_distinct != nullptr, create_options, "", false, instantiate_tmp_table))
这里执行的语句是:
select * from student union select * from student;
可以看到这里确实创建了临时表, 是在
bool Query_expression::prepare(THD *thd, Query_result *sel_result,
创建临时表调用的是这个方法:
/** Create a temp table according to a field list. Given field pointers are changed to point at tmp_table for send_result_set_metadata. The table object is self contained: it's allocated in its own memory root, as well as Field objects created for table columns. Those Field objects are common to TABLE and TABLE_SHARE. This function will replace Item_sum items in 'fields' list with corresponding Item_field items, pointing at the fields in the temporary table, unless save_sum_fields is set to false. The Item_field objects are created in THD memory root. @param thd thread handle @param param a description used as input to create the table @param fields list of items that will be used to define column types of the table (also see NOTES) @param group Group key to use for temporary table, NULL if none @param distinct should table rows be distinct @param save_sum_fields see NOTES @param select_options @param rows_limit @param table_alias possible name of the temporary table that can be used for name resolving; can be "". @remark mysql_create_view() checks that views have less than MAX_FIELDS columns. @remark We may actually end up with a table without any columns at all. See comment below: We don't have to store this. */ #define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128 #define AVG_STRING_LENGTH_TO_PACK_ROWS 64 #define RATIO_TO_PACK_ROWS 2 TABLE *create_tmp_table(THD *thd, Temp_table_param *param, const mem_root_deque<item> &fields, ORDER *group, bool distinct, bool save_sum_fields, ulonglong select_options, ha_rows rows_limit, const char *table_alias) {</item>
其中有一段代码是这样的:
} else if (distinct && share->fields != param->hidden_field_count) { /* Create an unique key or an unique constraint over all columns that should be in the result. In the temporary table, there are 'param->hidden_field_count' extra columns, whose null bits are stored in the first 'hidden_null_pack_length' bytes of the row. */ DBUG_PRINT("info", ("hidden_field_count: %d", param->hidden_field_count)); share->keys = 1; share->is_distinct = true; if (! unique_constraint_via_hash_field) { param->keyinfo->table = table; param->keyinfo->is_visible = true; param->keyinfo->user_defined_key_parts = share->fields - param->hidden_field_count; param->keyinfo->actual_key_parts = param->keyinfo->user_defined_key_parts; KEY_PART_INFO *key_part_info = share->mem_root.ArrayAlloc( param->keyinfo->user_defined_key_parts); if (key_part_info == nullptr) return nullptr; param->keyinfo->key_part = key_part_info; param->keyinfo->flags = HA_NOSAME | HA_NULL_ARE_EQUAL; param->keyinfo->actual_flags = param->keyinfo->flags; param->keyinfo->name = "【MySQL 文档翻译】理解查询计划 总结
- union 和 union all 都会创建临时表, 但是又不太一样
- 二者的查询计划不一样
- union 默认会创建一个以返回列作为 key 的临时表, 所谓过滤就是将数据插入这个临时表
- 临时表装数据的容器实际上是一个 unordered_set
- 有一种存储引擎叫做临时表
- union all 则是直接读取表的数据并返回给客户端, 不走临时表
- union all 和 union 的场景还是得根据需要来判断, 如果没有 distinct 的需求话, 数据又不多, 可以考虑使用 union all
原文地址: 【MySQL 源码】UNION 比 UNION ALL 的性能差很多吗?
欢迎访问我的个人博客: http://blog.duhbb.com/
以上就是《【MySQL 源码】UNION 比 UNION ALL 的性能差很多吗?》的详细内容,更多关于mysql的资料请关注golang学习网公众号!

- 上一篇
- 记一次FreeBSD系统中mysql服务异常的排查过程

- 下一篇
- 雪花算法(SnowFlake)
-
- 数据库 · MySQL | 1天前 |
- MySQL设置中文界面,超简单教程来了!
- 332浏览 收藏
-
- 数据库 · MySQL | 1天前 | mysql 索引提示
- MySQL进阶必看!FORCE/USE/IGNOREINDEX用法大揭秘
- 182浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- 手把手教你写MySQL存储过程,小白也能轻松上手
- 163浏览 收藏
-
- 数据库 · MySQL | 1天前 | mysql group by
- MySQL分组查询优化:GROUPBY原理+索引优化超全解析
- 324浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL设置中文语言,轻松拥有中文界面
- 211浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL建库语句从入门到精通:创建数据库+设置字符集&排序规则(附实例)
- 176浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- 从零开始学MySQL数据库操作,小白轻松变大神!
- 496浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL插入日期到时间字段,轻松搞定日期格式
- 484浏览 收藏
-
- 数据库 · MySQL | 1天前 | mysql 数据压缩
- MySQL怎么实现高效压缩存储?表压缩+列式存储详细解读
- 272浏览 收藏
-
- 数据库 · MySQL | 1天前 | mysql JOIN优化
- MySQL优化JOIN操作:七大技巧教你提升关联查询速度
- 106浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL出现中文乱码?超详细解决方案一次性搞定
- 211浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL主从复制这样配!搞懂这些参数,replication稳了~
- 131浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 茅茅虫AIGC检测
- 茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
- 15次使用
-
- 赛林匹克平台(Challympics)
- 探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
- 48次使用
-
- 笔格AIPPT
- SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
- 56次使用
-
- 稿定PPT
- 告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
- 51次使用
-
- Suno苏诺中文版
- 探索Suno苏诺中文版,一款颠覆传统音乐创作的AI平台。无需专业技能,轻松创作个性化音乐。智能词曲生成、风格迁移、海量音效,释放您的音乐灵感!
- 56次使用
-
- golang MySQL实现对数据库表存储获取操作示例
- 2022-12-22 499浏览
-
- 搞一个自娱自乐的博客(二) 架构搭建
- 2023-02-16 244浏览
-
- B-Tree、B+Tree以及B-link Tree
- 2023-01-19 235浏览
-
- mysql面试题
- 2023-01-17 157浏览
-
- MySQL数据表简单查询
- 2023-01-10 101浏览