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 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时 ...
随机推荐
- Minimum Cost 【POJ - 2516】【网络流最小费用最大流】
题目链接 题意: 有N个商家它们需要货物源,还有M个货物供应商,N个商家需要K种物品,每种物品都有对应的需求量,M个商家每种物品都是对应的存货,然后再是K个N*M的矩阵表示了K个物品从供货商运送到商家 ...
- SEC3 - MySQL常见命令
1.查看当前所有的数据库 show databases; 2. 打开指定的库名 use 库名称: 3.查看当前库中所有的表 show tables; 4. 查看其他库的所有表 show tables ...
- 正则表达式从入门到放弃「Java」
正则表达式能做什么? 正则表达式可以用来搜索.编辑或处理文本. 「都懂它可以处理文本,可到底是怎么回事?」 正则表达式的定义 百度百科:正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特 ...
- java调用数据库中的函数和存储过程
1.调用函数 {?= call <procedure-name>[(<arg1>,<arg2>, ...)]} {call <procedure-name&g ...
- Reinforcement Learning for Self Organization and Power Control of Two-Tier Heterogeneous Networks
R. Amiri, M. A. Almasi, J. G. Andrews and H. Mehrpouyan, "Reinforcement Learning for Self Organ ...
- List带索引的常用方法,以及集合的三种遍历
package cn.learn.collection.List; import com.sun.source.tree.NewArrayTree; import java.util.ArrayLis ...
- HDFS-Suffle
一.Shuffle机制 1.官网图 2.MR确保每个Reducer的输入都是按照key排序的.系统执行排序的过程(即将Mapper输出作为输入传给Reducer)成为Shuffle 二.Partiti ...
- 【洛谷p1025】数的划分
数的划分[传送门] 算法的话,dfs+剪枝: 据说是01年之前的NOIp提高组: 思路: 这道题是求把n无序的划分成k份的方案数,最直接的搜索方法是依次枚举x1,x2……xk的值,然后判断,显然这么搜 ...
- pycharm修改字体大小和主题
一,修改文字大小: 二,修改主题:你可能对编辑器的外观仍不满意,例如你希望将文档字符串改变为另外一种颜色,下面介绍具体更改方法:
- XMPP即时通讯协议使用(一)——Openfire安装
Openfire服务器安装 下载地址:https://www.igniterealtime.org/downloads/index.jsp,根据你的操作系统,选择对应的下载版本.本文选择的是openf ...