oracle in 和 or 效率高,oracleinorin语句

在oracle 中,查询语句用 in 和 or 查询的结果条数不一样

先试试不要and a_devtestdt is null 这句、查看两段代码结果。

再试试下面

select dept from A

where (status='[D2]方案设计阶段' or status='[D2]方案审核中' or status ='[D2]开发中' or status='[D2]开发完成' or status='[D2]单元测试进行中')

and a_devtestdt is null

-----------------------

select dept from A

where status in('[D2]方案设计阶段','[D2]方案审核中','[D2]开发中','[D2]开发完成','[D2]单元测试进行中')

and a_devtestdt is null

一般有 OR 和 AND 同时出现的where 条件、最好加括号区分

Oracle语句中IN和=的区别有哪些

in 表示在一个结合内进行查询,比如 select * from character where letter in ('A','B','C')。

=的作用就是一个值的比较。

但是等号也可以实现in的效果,只是写起来比较麻烦。比如上面的例子,你也可以这样写

select * from character where letter='A' or letter='B' or letter='C'.

两个运算符都比较常用,根据具体的情况选择吧

oracle 中 in 不能超过1000的解决方法 具体的解决方法

简单的可以这样解决 a IN (1,2,。,999) or a in (1000,1001,。1999) or 。.

或者将in里的存放到一个临时表里,再关联查询!如:

with t as (

select 1 id from dual

union all

select 199999 id from dual)

select 。 from 表 where a in (select * from t)

请问oracle中=与in有什么区别,求大神

首先应用范围不一样:

in 可以理解为是范围内的选择

= 只有一个

例如:

select sno, sname from t1 where sno in ('sn1001','sn1002');

select sno, sname from t1 where sno in ('sn1001');

select sno, sname from t1 where sno ='sn1001';

select sno, sname from t1 where sno in (select sno from t2); --子查询结果可以不止一个结果

select sno, sname from t1 where sno =(select sno from t2); --子查询结果只能有一个

其次性能也不一样;=的性能大于in的性能,因为=能较好的使用索引等

oracle exists和in的区别

1、关于在 Oracle8i 时代中in和exists的区别 这里有条SQL语句:select * from A where id in(select id from B) 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与B表中的id相等,如果相等则将A表的记录加入结果集中,直到遍历完A表的所有记录; 它的查询过程类似于以下过程 List resultSet=[]; Array A=(select * from A); Array B=(select id from B); for(int i=0;iin(),因为它会B表数据全部遍历一次.如:A表有10000条记录,B表有1000000条记录,那么最多有可能遍历10000*1000000次,效率很差.再如:A表有10000条记录,B表有100条记录,那么最多有可能遍历10000*100次,遍历次数大大减少,效率大大提升.结论1:in()适合B表比A表数据小的情况 这里还有一条SQL语句: select a.* from A a where exists(select 1 from B b where a.id=b.id) 以上查询使用了exists语句,exists()会执行A.length次,它并不缓存exists()结果集,因为exists()结果集的内容并不重要,重要的是结果集中是否有记录,如果有则返回true,没有则返回false.它的查询过程类似于以下过程 List resultSet=[]; Array A=(select * from A) for(int i=0;i是否有记录返回 resultSet.add(A[i]); } } return resultSet; 结论2:exists()适合B表比A表数据大的情况 当B表比A表数据大时适合使用exists(),因为它没有那么遍历操作,只需要再执行一次查询就行.如:A表有10000条记录,B表有1000000条记录,那么exists()会执行10000次去判断A表中的id是否与B表中的id相等.如:A表有10000条记录,B表有100000000条记录,那么exists()还是执行10000次,因为它只执行A.length次,可见B表数据越多,越适合exists()发挥效果.再如:A表有10000条记录,B表有100条记录,那么exists()还是执行10000次,还不如使用in()遍历10000*100次,因为in()是在内存里遍历比较,而exists()需要查询数据库,我们都知道查询数据库所消耗的性能更高,而内存比较很快.当A表数据与B表数据一样大时,in与exists效率差不多,可任选一个使用.In适合内外表都很大的情况,exists适合外表结果集很小的情况。

In和exists对比:若子查询结果集比较小,优先使用in,若外层查询比子查询小,优先使 用exists。因为若用in,则Oracle会优先查询子查询,然后匹配外层查询,若使用exists,则oracle会优先查询外层表,然后再与内层表匹配。

最优化 匹配原则,拿最小记录匹配大记录2、关于在 Oracle8i 之后 时代中in和exists的区别 in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询。一直以来认为exists比in效率高的说法是不准确的。

如果查询的两个表大小相当,那么用in和exists差别不大。如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:例如:表A(小表),表B(大表)1:select * from A where cc in (select cc from B) 效率低,用到了A表上cc列的索引;select * from A where exists(select cc from B where cc=A.cc) 效率高,用到了B表上cc列的索引。

相反的2:select * from B where cc in (select cc from A) 效率高,用到了B表上cc列的索引;select * from B where exists(select cc from A where cc=B.cc) 效率低,用到了A表上cc列的索引。带in的关联子查询是多余的,因为in子句和子查询中相关的操作的功能是一样的。

如:select staff_name from staff_member where staff_id in(select staff_id from staff_func where staff_member.staff_id=staff_func.staff_id); 为非关联子查询指定exists子句是不适当的,因为这样会产生笛卡乘积。如: select staff_name from staff_member where staff_id exists (select staff_id from staff_func); not in 和not exists 如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。

所以无论哪个表大,用not exists都比not in要快。尽量不要使用not in子句。

使用minus 子句都比not in 子句快,虽然使用minus子句要进行两次查询: select staff_name from staff_member where staff_id in (select staff_id from staff_member minus select staff_id from staff_func where func_id like '81%'); in 与 "=" 的区别 select name from student where name in ('zhang','wang','li','zhao'); 与 select name from student where name='zhang' or name='li' or name='wang' or name='zhao' 的结果是相同的。3、关于在 Oracle8i 之后 时代中in和exists的区别 在ORACLE 11G大行其道的今天,还有很多人受早期版本的影响,记住一些既定的规则,1.子查询结果集小,用IN2.外表小,子查询表大,用EXISTS 这是完全错误的观点。

在8i时代,这经常是正确的,但是现在已经11G了,马上12C就要面世了。其实在ORACLE 9i CBO就已经优化了IN,EXISTS的区别,ORACLE优化器。

oracle中in和exist的区别

in 和 exists区别

in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询。

一直以来认为exists比in效率高的说法是不准确的。

如果查询的两个表大小相当,那么用in和exists差别不大。

如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:

例如:表A(小表),表B(大表)

1:

select * from A where cc in (select cc from B)

效率低,用到了A表上cc列的索引;

select * from A where exists(select cc from B where cc=A.cc)

效率高,用到了B表上cc列的索引。

相反的

2:

select * from B where cc in (select cc from A)

效率高,用到了B表上cc列的索引;

select * from B where exists(select cc from A where cc=B.cc)

效率低,用到了A表上cc列的索引。

带in的关联子查询是多余的,因为in子句和子查询中相关的操作的功能是一样的。如:

select staff_name from staff_member where staff_id in

(select staff_id from staff_func where staff_member.staff_id=staff_func.staff_id);

为非关联子查询指定exists子句是不适当的,因为这样会产生笛卡乘积。如:

select staff_name from staff_member where staff_id

exists (select staff_id from staff_func);

not in 和not exists

如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;

而not extsts 的子查询依然能用到表上的索引。

所以无论哪个表大,用not exists都比not in要快。

尽量不要使用not in子句。使用minus 子句都比not in 子句快,虽然使用minus子句要进行两次查询:

select staff_name from staff_member where staff_id in (select staff_id from staff_member minus select staff_id from staff_func where func_id like '81%');

in 与 "=" 的区别

select name from student where name in ('zhang','wang','li','zhao');

select name from student where name='zhang' or name='li' or name='wang' or name='zhao'

的结果是相同的。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值