日期:2014-05-16  浏览次数:20324 次

Oracle确定表中是否存在满足已知条件的记录方法比较

开发中碰到一个删除A表的记录之前要确定该记录在另外的B表C表等表中没有被使用,否则是不给删除A表中的该记录的。

想到了几个简单发方法,并对他们进行了测试和比较。记录下来以备后用。

1、select count(*) from?B?b where b.xx =? 'xx'--耗时3.104s

2、select 1 from B?b? where b.xx =? 'xx'--耗时0.047s

?

3_1、select 1 from dual where exists (select 1 from B?b? where b.xx =? 'xx')--耗时0.016s

?

3_2、select count(*) from dual where exists (select 1 from B?b? where b.xx =? 'xx')--耗时0.016s

?

测试时中B表中满足b.xx = 'xx'条件的记录100000以上。

?

?注:条件b.xx = 'xx'是A表和B表关联的条件。

?

采用3_2方法判断返回的结果是否大于0即可知道,2、3_1判断返回的结果集是否有记录。

1 楼 hzw2312 2012-02-22  
select 1 from B b  where b.xx =  'xx'

学习了~~~!!!
2 楼 liuzhiyuan 2012-02-22  
select count(*) from B b where b.xx =  'xx' and rownum<=1;