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。的更多相关文章

  1. Is "UNION ALL" Always Better Than "UNION"? Watch Out!

    无论是教科书还是平常的实践都告诉我们 - “尽量避免用UNION,尽可能用UNION ALL替代”. 原因很简单,UNION会对结果集进行排序去重操作,这是一个很消耗资源的操作. 但是,今天碰到了一个 ...

  2. SQL Server-聚焦UNIOL ALL/UNION查询(二十三)

    前言 本节我们来看看有关查询中UNION和UNION ALL的问题,简短的内容,深入的理解,Always to review the basics. 初探UNION和UNION ALL 首先我们过一遍 ...

  3. 联合体(union)的使用方法及其本质

    转自:http://blog.csdn.net/huqinwei987/article/details/23597091 有些基础知识快淡忘了,所以有必要复习一遍,在不借助课本死知识的前提下做些推理判 ...

  4. mysql 实战 or、in与union all 的查询效率

    OR.in和union all 查询效率到底哪个快. 网上很多的声音都是说union all 快于 or.in,因为or.in会导致全表扫描,他们给出了很多的实例. 但真的union all真的快于o ...

  5. UNION 和 UNION ALL 区别

    UNION用的比较多union all是直接连接,取到得是所有值,记录可能有重复 union 是取唯一值,记录没有重复 1.UNION 的语法如下: [SQL 语句 1] UNION [SQL 语句 ...

  6. 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 ...

  7. 转 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 ...

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

    本文出处:http://www.cnblogs.com/wy123/p/7884986.html 周围又有人在讨论UNION和UNION ALL,对于UNION和UNION ALL,网上说的最多的就是 ...

  9. UNION 和 UNION ALL 操作符

    SQL UNION 操作符 1.UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意:UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时 ...

随机推荐

  1. Bootstrap 学习笔记7 模态框插件

    网站弹出框使用: 基本使用: <!-- 模态框的声明 --> <div class="modal" id="myModal" tabindex ...

  2. 机器学习实战笔记-2-kNN近邻算法

    # k-近邻算法(kNN) 本质是(提取样本集中特征最相似数据(最近邻)的k个分类标签). K-近邻算法的优缺点 例 优点:精度高,对异常值不敏感,无数据输入假定: 缺点:计算复杂度高,空间复杂度高: ...

  3. 任务调度(02)Spring Schedule

    任务调度(02)Spring Schedule [toc] Spring 3.0 提供两种任务调度方式:一是定时任务调度:二是异步任务调度.这两种任务调度方式都是基于 JUC 实现的,是一种非常轻量级 ...

  4. No-sql之redis常用命令

    转自:http://blog.csdn.net/nicewuranran/article/details/51793760 No-SQL之Redis 介绍 Redis是一种基于内存存储的key-val ...

  5. SpringBoot 接口并行高效聚合

    转自:juejin.im/post/5d064b90e51d45777540fda7 背景 接口开发是后端开发中最常见的场景, 可能是RESTFul接口, 也可能是RPC接口. 接口开发往往是从各处捞 ...

  6. Android -ui控件

    一:TextView控件 TextView --> View 1.创建TextView的两种方式: 1.1编写TextView类 TextView tv = new TextView(this) ...

  7. 7、numpy——广播

    1.广播的引出 广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 如果两个数组 a 和 b 形状相同,即满足 a. ...

  8. JavaScript给动态插入的元素添加事件绑定

    由于实际的需要,有时需要往网页中动态的插入HTML内容,并在插入的节点中绑定事件处理函数.我们知道,用Javascript向HTML文档中 插入内容,有两种方法, 一种是在写HTML代码写入JS,然后 ...

  9. AI-Tensorflow-神经网络优化算法-梯度下降算法-学习率

    记录内容来自<Tensorflow实战Google一书>及MOOC人工智能实践 http://www.icourse163.org/learn/PKU-1002536002?tid=100 ...

  10. CentOS7单用户模式修改密码

    以下内容均摘抄自:https://blog.csdn.net/ywd1992/article/details/83538730  亲测有用,谢谢大佬的好文章 1.启动centos系统,并且当在GRUB ...