使用 JOIN 优化子查询
使用 JOIN 优化子查询
MySQL
使用join优化子查询
子查询:每次执行外层查询语句内层语句都会执行,查询效率低,耗时长。
使用join 优化子查询
查询出A 表中所有记录,包含共有B
select a.user_name,a.over,(select oer from user2 b where a.user_name=b.user_name) AS over2 from user1 a;
优化
select a.user_name,a.over,b.over as over2 from user1 a left join user2 b ON a.user_name=b.user_name;
使用join优化子查询
使用JOIN优化子查询结果:
使用join优化子查询:
select a.'user_name',a.'over',b.'over' as over2
from user1 a
left join user2 b on
a.'user_name'=b.'user_name';
工作中尽量少用子查询,因为查询会每次都会与子查询中的数据做匹配导致效率下降。
join速度快
1、使用连接(join) 通常要比使用单纯的子查询耗用的时间更短
使用JOIN优化子查询
select a.user_name,a.'over',(select over from user2 b where a.user_name=b_username) as over2 from user2 a
使用join优化子查询
使用join优化子查询
@MySQL---JOIN优化子查询技巧
1.一般子查询写法
SELECT a.user_name,a.voer, (SELECT over FROM user2 WHERE a.user_name = b,user_name) AS over2 FROM user1 a;
问题:子查询对A表的每一条记录都要进行一次子查询,
数据小时,没有多大影响,如果数据量大时,则要消耗大量的查
2.JOIN优化(左连接)后的写法
SELECT a.user_name,a.over,b.over FROM user1 a LEFT JOIN user2 b ON a.user_name = b.user_name;
用left优化子查询()