left join和inner join的区别
1.返回不同
inner join:inner join 只返回两个表中联结字段相等的行。
left join:left join返回包括左表中的所有记录和右表中联结字段相等的记录。
2.数量不同
inner join:inner join的数量小于等于左表和右表中的记录数量。
left join:left join的数量以左表中的记录数量相同。
3.记录属性不同
inner join:inner join不足的记录属性会被直接舍弃。
left join:left join不足的记录属性用NULL填充。
二、sql优化常用的几种方法
1、对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。
2、应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描。
3、应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num is null
可以在num列设置默认值0,然后通过=等号查询:
select id from t where num=0
4、应尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num=10 or num=20
此查询可优化为:select id from t where num=10
union all
select id from t where num=20