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. poj2010 Moo University - Financial Aid 优先队列

    Description Bessie noted that although humans have many universities they can attend, cows have none ...

  2. jvm 更多链接

    http://www.cnblogs.com/dingyingsi/p/3760447.html    :  讲解 jvm https://blog.csdn.net/Luomingkui1109/a ...

  3. $_POST,$_GET,$_REQUEST区分

    PHP $_REQUEST PHP $_REQUEST 用于收集 HTML 表单提交的数据. 下面的例子展示了一个包含输入字段及提交按钮的表单.当用户通过点击提交按钮来提交表单数据时, 表单数据将发送 ...

  4. eclipse配置,快捷键备忘

    1. General --> Workspace --> UTF-82. General --> Editors --> Associations --> JSP --& ...

  5. Vue3.0响应式实现

    基于Proxy // 弱引用映射表 es6 防止对象不能被回收 let toProxy = new WeakMap(); // 原对象: 代理过得对象 let toRaw = new WeakMap( ...

  6. Spring学习(六)--渲染Web视图

    一.将模型数据渲染为Html 在上一篇文章中,我们所编写的控制器方法都没有直接产生浏览器中渲染所需的HTML.这些方法只是将数据填充到模型中,然后将模型传递给一个用来渲染的视图.这些方法会返回一个St ...

  7. TCP协议-流量控制

    流量控制是通过滑动窗口来实现控制的.使用了坚持定时器,防止发送失败导致死锁.

  8. spring 配置属性细节

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28233811 概要(红色为上一篇所讲,蓝 ...

  9. Android关于SurfaceView,SurfaceHolder,SurfaceHolder.CallBack详解

    官方的定义: 1.SurfaceView SurfaceView是视图(View)的继承类,这个视图里内嵌了一个专门用于绘制的Surface.你可以控制这个Surface的格式和尺寸.Surfacev ...

  10. Metasploitable2使用指南

    Metasploitable2使用指南 Metasploitable2 虚拟系统是一个特别制作的ubuntu操作系统,本身设计作为安全工具测试和演示常见漏洞攻击.版本2已经可以下载,并且比上一个版本包 ...