不同的SQL可以互相替换
- 适应EXIST替换IN,效率可以更高。
# 使用IN效率低
select avg(degree) as avgdegree from score
where sno IN(select sno from student where class='95033')
# 改用EXISTS
select avg(degree) as avgdegree from score
where EXISTS (select 1 from student where class='95033' AND score.sno=student.sno)
- 效率:等值连接>外连接>子查询,因此尽可能的使用等值。可见MySQL语句优化总结
- distance能用group by替代就替代,效率更高。