1、外连接强制驱动表

  1. 驱动表是什么?
  2. 在多表连接当中, 承当for循环中外层循环的角色
  3. 此时, MySQL会拿着驱动表的每个满足条件的关联列的值, 去依次找到for循环内循环中的关联值一一进行判断和匹配
  4. 建议:
  5. 1. 小表作为驱动表, 降低next loop次数
  6. 2. left join 可以强制左表为驱动表
  7. 例如:
  8. select city.name ,city.population,country.name,country.surfacearea
  9. from city join country
  10. on city.CountryCode=country.Code
  11. where city.name='wuhan';
  12. 改写为: 强制驱动表的left join
  13. select city.name ,city.population,country.name,country.surfacearea
  14. from city left join country
  15. on city.CountryCode=country.Code
  16. where city.name='wuhan';

2、union 和 union all 区别 (面试题)

union 和 union all 区别
union: 聚合两个结果集, 会自动进行结果集去重复 (去重)
union all: 聚合两个结果集, 不会去重复 (不去重)

例子: 查询中国或美国的城市信息
select * from world.city where countrycode='CHN' or countrycode='USA'
select * from world.city where countrycode in ('CHN','USA')

改写:

select * from world.city where countrycode='CHN'
union all 
select * from world.city where countrycode='USA';