oracle_in_exists_left-join
查询A表中的数据没有出现在B表中
mysql> select * from user;
+------+-------+
| uid | uname |
+------+-------+
| 1 | ccc |
| 2 | xxx |
| 3 | yyy |
+------+-------+
3 rows in set (0.00 sec)
mysql> select * from results;
+------+------+
| rid | uid |
+------+------+
| 100 | 1 |
| 200 | 2 |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from user where uid not in (select uid from results);
+------+-------+
| uid | uname |
+------+-------+
| 3 | yyy |
+------+-------+
1 row in set (0.00 sec)
mysql> select u.* from user u where not exists(select * from results where uid=u.uid);
+------+-------+
| uid | uname |
+------+-------+
| 3 | yyy |
+------+-------+
1 row in set (0.00 sec)
mysql> select u.* from user u left join results as r on u.uid=r.uid where r.uid is null;
+------+-------+
| uid | uname |
+------+-------+
| 3 | yyy |
+------+-------+
1 row in set (0.00 sec)
============================================================
EXISTS的执行流程
select * from t1 where exists ( select null from t2 where y = x )
可以理解为:
for x in ( select * from t1 )
loop
if ( exists ( select null from t2 where y = x.x )
then
OUTPUT THE RECORD
end if
end loop
对于in 和 exists的性能区别:
如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in,反之如果外层的主查询记录较少,子查询中的表大,又有索引时使用exists。
其实我们区分in和exists主要是造成了驱动顺序的改变(这是性能变化的关键),如果是exists,那么以外层表为驱动表,先被访问,如果是IN,那么先执行子查询,所以我们会以驱动表的快速返回为目标,那么就会考虑到索引及结果集的关系了
另外IN时不对NULL进行处理
如:
select 1 from dual where null in (0,1,2,null)
为空
2.NOT IN 与NOT EXISTS:
NOT EXISTS的执行流程
select .....
from rollup R
where not exists ( select 'Found' from title T
where R.source_id = T.Title_ID);
可以理解为:
for x in ( select * from rollup )
loop
if ( not exists ( that query ) ) then
OUTPUT
end if;
end;
注意:NOT EXISTS 与 NOT IN 不能完全互相替换,看具体的需求。如果选择的列可以为空,则不能被替换。
例如下面语句,看他们的区别:
select x,y from t;
x y
------ ------
1 3
3 1
1 2
1 1
3&nb
相关新闻>>
- 发表评论
-
- 最新评论 更多>>