Union比or快 Using UNION is faster when it comes to cases like scan two different column。
problem: 595. Big Countries
A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.
Write a SQL solution to output big countries' name, population and area.
Two obvious solutions:
#OR
SELECT name, population, area
FROM World
WHERE area > 3000000 OR population > 25000000
And Faster Union
#Union
SELECT name, population, area
FROM World
WHERE area > 3000000
UNION
SELECT name, population, area
FROM World
WHERE population > 25000000
Why Union is faster than OR?
Strictly speaking, Using UNION is faster when it comes to cases like scan two different column like this.
(Of course using UNION ALL is much faster than UNION since we don't need to sort the result. But it violates the requirements)
Suppose we are searching population and area, Given that MySQL usually uses one one index per table in a given query, so when it uses the 1st index rather than 2nd index, it would still have to do a table-scan to find rows that fit the 2nd index.
When using UNION, each sub-query can use the index of its search, then combine the sub-query by UNION.
I quote from a benchmark about UNION and OR, feel free to check it out:
Scenario 3: Selecting all columns for different fields
CPU Reads Duration Row Counts
OR 47 1278 443 1228
UNION 31 1334 400 1228
Scenario 4: Selecting Clustered index columns for different fields
CPU Reads Duration Row Counts
OR 0 319 366 1228
UNION 0 50 193 1228
Union not always faster than or!
Most good DBMSs use an internal query optimizer to combine the SELECT statements
before they are even processed. In theory, this means that from a performance
perspective, there should be no real difference between using multiple WHERE clause
conditions or a UNION. I say in theory, because, in practice, most query optimizers
don’t always do as good a job as they should. Your best bet is to test both methods to
see which will work best for you.
prob 181
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
description: find Employees Earning More Than Their Managers
ex:
+----------+
| Employee |
+----------+
| Joe |
+----------+
solution
# join is a little faster
select a.Name as Employee
from Employee a Join Employee b
on b.Id = a.ManagerId and a.Salary > b.Salary
select e.Name as Employee
from Employee e, Employee m
where e.ManagerId is not null and e.ManagerId = m.Id and e.Salary > m.Salary
把表中的性别f换为m m换成f
UPDATE salary SET sex = IF(sex='m','f','m');
Union比or快 Using UNION is faster when it comes to cases like scan two different column。的更多相关文章
- Is "UNION ALL" Always Better Than "UNION"? Watch Out!
无论是教科书还是平常的实践都告诉我们 - “尽量避免用UNION,尽可能用UNION ALL替代”. 原因很简单,UNION会对结果集进行排序去重操作,这是一个很消耗资源的操作. 但是,今天碰到了一个 ...
- SQL Server-聚焦UNIOL ALL/UNION查询(二十三)
前言 本节我们来看看有关查询中UNION和UNION ALL的问题,简短的内容,深入的理解,Always to review the basics. 初探UNION和UNION ALL 首先我们过一遍 ...
- 联合体(union)的使用方法及其本质
转自:http://blog.csdn.net/huqinwei987/article/details/23597091 有些基础知识快淡忘了,所以有必要复习一遍,在不借助课本死知识的前提下做些推理判 ...
- mysql 实战 or、in与union all 的查询效率
OR.in和union all 查询效率到底哪个快. 网上很多的声音都是说union all 快于 or.in,因为or.in会导致全表扫描,他们给出了很多的实例. 但真的union all真的快于o ...
- UNION 和 UNION ALL 区别
UNION用的比较多union all是直接连接,取到得是所有值,记录可能有重复 union 是取唯一值,记录没有重复 1.UNION 的语法如下: [SQL 语句 1] UNION [SQL 语句 ...
- Leetcode: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- 转 SQL Union和SQL Union All两者用法区别效率以及与order by 和 group by配合问题
SQL Union和SQL Union All两者用法区别效率以及与order by 和 group by配合问题 SQL Union和SQL Union All用法 SQL UNION 操作符 UN ...
- SqlServer中的UNION操作符在合并数据时去重的原理以及UNION运算符查询结果默认排序的问题
本文出处:http://www.cnblogs.com/wy123/p/7884986.html 周围又有人在讨论UNION和UNION ALL,对于UNION和UNION ALL,网上说的最多的就是 ...
- UNION 和 UNION ALL 操作符
SQL UNION 操作符 1.UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意:UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时 ...
随机推荐
- mybatis批量插入、更新和删除
https://blog.csdn.net/m0_37981235/article/details/79131493 https://www.jb51.net/article/132823.htm
- 应用安全 - Web安全 - 文件包含攻防
LFI - 无限制本地文件包含 通过目录遍历漏洞可以获取到系统中其他文件的内容 常见的敏感信息路径 Windows系统 c:\boot.ini // 查看系统版本 c:\windows\system3 ...
- Git008--远程仓库
Git--远程仓库 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...
- HeightCharts柱状图和饼状图
HTML: <div id="container1" style="height:350px; " ></div> <di ...
- 移动app云测试平台
一:移动App云测试平台 1.云测试平台背景 随着智能手机的普及率和渗透率越来越高,App开发软件也越来越多.但是因为安卓和IOS的碎片化,尤其是安卓,因为完全开源的原因,导致设备繁多,品牌众多,版本 ...
- hdu6341 Problem J. Let Sudoku Rotate (dfs)
题目传送门 题意: 给你16个16宫格的数独,里面是0~F,你可以逆时针旋转里面的每个16宫格 问你它是从标准数独逆时针旋转多少次得到? 思路: 可以知道每个16宫已经是标准的了,接下来只要考虑每行. ...
- EAN13条码的校验位的Excel算法
校验位原公式: 单元格=10-RIGHT(SUM(MID($B3,{1;2;3;4;5;6;7;8;9;10;11;12},1)*{1;3;1;3;1;3;1;3;1;3;1;3})) 简化公式: 单 ...
- Mysql--08 存储引擎
目录 一.存储引擎简介 二.MySQL自带的存储引擎类型 三.真实企业案例 四.Innodb存储引擎--表空间介绍 一.存储引擎简介  1.文件系统: 1.1 操作系统组织和存取数据的一种机制. 1 ...
- 洛谷P1446/BZOJ1004 Cards Burnside引理+01背包
题意:有n张牌,有R+G+B=n的3种颜色及其数量,要求用这三种颜色去染n张牌.n张牌有m中洗牌方式,问在不同洗牌方式下本质相同的染色方案数. 解法:这道题非常有意思,题解参考Hzwer学长的.我这里 ...
- Sass:字符串函数-To-upper-case()、To-lower-case()
1.To-upper-case() To-upper-case() 函数将字符串小写字母转换成大写字母.如: //SCSS .test { text: to-upper-case(aaaaa); te ...