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 

原文地址:https://www.cnblogs.com/wangjiale1024/p/11407033.html

时间: 2024-10-13 14:34:58

Union比or快 Using UNION is faster when it comes to cases like scan two different column。的相关文章

转 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 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列的顺序必须相同. SQL UNION 语法 SELECT column_name(s) FROM tab

SQL Server-聚焦UNIOL ALL/UNION查询

初探UNION和UNION ALL 首先我们过一遍二者的基本概念和使用方法,UNION和UNION ALL是将两个表或者多个表进行JOIN,当然表的数据类型必须相同,对于UNION而言它会去除重复值,而UNION ALL则会返回所有数据,这就是二者的区别和使用方法.下面我们来看一个简单的例子. USE TSQL2012 GO--USE UNION ALL SELECT 1     UNION ALL  SELECT 2     UNION ALL SELECT 2     UNION ALL S

SQL语句中:UNION与UNION ALL的区别

UNION用的比较多union all是直接连接,取到得是所有值,记录可能有重复 union 是取唯一值,记录没有重复 1.UNION 的语法如下: [SQL 语句 1] UNION [SQL 语句 2] 2.UNION ALL 的语法如下: [SQL 语句 1] UNION ALL [SQL 语句 2] 效率:UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同. 1.对重复结果的处理:UNION在进行表链接后会筛选掉重复的记录,Union Al

union 和 all union

sql union用法和sql union all用法,sql union效率 1.sql union用法 sql union在查询中可以将两个SQL 语句的结果合并起来.如果这样看的话, UNION 跟 JOIN 是相似的,两个指令都可以由多个表格中撷取资料. sql union的一个限制是两个 SQL 语句所产生的栏位需要是同样的资料种类.另外,当我们用 UNION这个指令时,我们只会看到不同的资料值 (类似 SELECT DISTINCT). sql union只是将两个结果联结起来一起显

( 转 ) mysql 实战 or、in与union all 的查询效率

OR.in和union all 查询效率到底哪个快. 网上很多的声音都是说union all 快于 or.in,因为or.in会导致全表扫描,他们给出了很多的实例. 但真的union all真的快于or.in?本文就是采用实际的实例来探讨到底是它们之间的效率. 1:创建表,插入数据.数据量为1千万[要不效果不明显]. Sql代码   drop table if EXISTS BT; create table BT( ID int(10) NOT NUll, VName varchar(20) D

SqlServer中的UNION操作符在合并数据时去重的原理以及UNION运算符查询结果默认排序的问题

原文:SqlServer中的UNION操作符在合并数据时去重的原理以及UNION运算符查询结果默认排序的问题 本文出处:http://www.cnblogs.com/wy123/p/7884986.html 周围又有人在讨论UNION和UNION ALL,对于UNION和UNION ALL,网上说的最多的就是性能问题(实在不想说出来这句话:UNION ALL比UNION快)其实根本不想炒UNION和UNION ALL这碗剩饭了,每次看到网上说用这个不用那个,列举的一条一条的那种文章,只要看到说U

oracle union 和 union all

java.sql.SQLSyntaxErrorException: ORA-01789: 查询块具有不正确的结果列数 原因: 发现是sql语句用union时的 两个语句查询的字段不一致 解决:将 2个 union 的sql语句,select的列,改为一样的字段. UNION用的比较多union all是直接连接,取到得是所有值,记录可能有重复 union 是取唯一值,记录没有重复 1.UNION 的语法如下: [SQL 语句 1] UNION [SQL 语句 2] 2.UNION ALL 的语法

Union-Find(并查集): Quick union算法

Quick union算法 Quick union: Java implementation Quick union 性能分析 在最坏的情况下,quick-union的find root操作cost(访问array的次数)会达到N. 所以quick-union的性能也不好.

SQL SERVER: 合并相关操作(Union,Except,Intersect) - 转载

SQL Server 中对于结果集有几个处理,值得讲解一下 1. 并集(union,Union all) 这个很简单,是把两个结果集水平合并起来.例如 SELECT * FROM A UNION SELECT * FROM B [注意]union会删除重复值,也就是说A或B中重复的数据行,最终只会出现一次,而union all则会保留重复行. 2. 差异(Except) 就是两个集中不重复的部分.例如 SELECT * FROM A EXCEPT SELECT * FROM B 这个的意思是,凡