select t1. * ,t2.* from v1 t1,v2 t2 where t1.id=t2.id and not exists (select 1 from t3 where t3.id=t1.id)
要加快用行的速度只能是使用索引 所以要对T1,T2,T3中的ID字段来建立索引 至于2楼说的 select t1.*,t2.* from v1 t1 full join v2 t2 on t1.id=t2.id left join t3 on t1.id=t3.id where len(t3.id)=0 我看非也 着只不过把SQL语句换了个写发,难道就能够提高速度 有点不只到了? 继续关注中
光从语句考虑,NOT EXISTS是比NOT IN 快也许以下语句效率可以: select t1.*,t2.* from v1 t1 full join v2 t2 on t1.id=t2.id left join t3 on t1.id=t3.id where t3.id is null估计三个表的ID都是主键,所以没有提索引.
用EXISTS和IN从查询分析器上看,无论有没有索引,这两种方式都速度都一样,用索引捎快,用外连接稍微慢一点,慢在了t3.id is null这句上,用LEN函数肯定慢,因为有函数作为查询条件无法利用索引只能扫描全表。 最后结果,还是用EXISTS比较好,程序清晰好读。
where t1.id=t2.id
and not exists
(select 1 from t3 where t3.id=t1.id)
所以要对T1,T2,T3中的ID字段来建立索引
至于2楼说的
select t1.*,t2.*
from v1 t1 full join v2 t2 on t1.id=t2.id left join t3 on t1.id=t3.id
where len(t3.id)=0
我看非也
着只不过把SQL语句换了个写发,难道就能够提高速度
有点不只到了?
继续关注中
select t1.*,t2.*
from v1 t1 full join v2 t2 on t1.id=t2.id left join t3 on t1.id=t3.id
where t3.id is null估计三个表的ID都是主键,所以没有提索引.
最后结果,还是用EXISTS比较好,程序清晰好读。