1、外连接强制驱动表
驱动表是什么?在多表连接当中, 承当for循环中外层循环的角色此时, MySQL会拿着驱动表的每个满足条件的关联列的值, 去依次找到for循环内循环中的关联值一一进行判断和匹配建议:1. 小表作为驱动表, 降低next loop次数2. left join 可以强制左表为驱动表例如:select city.name ,city.population,country.name,country.surfaceareafrom city join countryon city.CountryCode=country.Codewhere city.name='wuhan';改写为: 强制驱动表的left join 。select city.name ,city.population,country.name,country.surfaceareafrom city left join countryon city.CountryCode=country.Codewhere 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';