1.exists 和 in 使用區別和效率
1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ;
T1數據量小而T2數據量非常大時,T1<<T2 時,1)的查詢效率高。
2) select * from T1 where T1.a in (select T2.a from T2) ;
T1數據量非常大而T2數據量小時,T1>>T2 時,2) 的查詢效率高。
綜合以上對IN/EXISTS的討論,我們可以得出一個基本通用的結論:IN適合於外表大而內表小的情況;EXISTS適合於外表小而內表大的情況。
2.between and效率應該比in高
3.union和 union 使用區別和效率
union all 和 union 合併表數據
union all 效率大於 union
union all 不去重
union 會自動去重