01.select * from emp where emp_no not in (select emp_no from emp_bill)
select * from emp where emp_no not in (select emp_no from emp_bill)
因为NOT IN 效率比较低,因此需要优化,有两种方法:
方法一:
01.select * from emp a where not exists (select 1from emp_bill b where b.emp.no=a.emp_no)
select * from emp a where not exists (select 1from emp_bill b where b.emp.no=a.emp_no)
select 1 是一种用法,也可以select 某字段 或者select 'x'都行的,也就是说exists其实是判断是否有数据返回,而不关心返回的数据。
方法二:
01.select a. * from emp a ,emp_bill b where a.emp_no = b.emp_no( + ) and b.emp_no is n
select a. * from emp a ,emp_bill b where a.emp_no = b.emp_no( + ) and b.emp_no is n
分享到:
相关推荐
最近项目上用select查询时使用到了not in来排除用不到的主键id一开始使用的sql如下: select s.SORT_ID, s.SORT_NAME, s.SORT_STATUS, s.SORT_LOGO_URL, s.SORT_LOGO_URL_LIGHT from SYS_SORT_PROMOTE s...
用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格。
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句.txt欢迎下载!
sql语句优化之用EXISTS替代IN、用NOT EXISTS替代NOT IN的语句
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句2.txt,欢迎下载!
1. 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 ...
MySQL优化之in,exists,not in,not exists的区别in与existsin查询过程结论:exists查询过程:结论:not in与not existsnot in查询过程:结论:not exists查询过程:结论: 首先我们使用两个用户表作为实例 insert ...
NOT IN、JOIN、IS NULL、NOT EXISTS效率对比 语句一:select count(*) from A where A.a not in (select a from B) 语句二:select count(*) from A left join B on A.a = B.a where B.a is null 语句三:select ...
尽量用not exists 或者外连接替代 not in 操作符。因为not in不能 应用表的索引 3。尽量不用<> 或者!= 操作符。不等于操作符是永远不会用到索引的,因此 对它的处理只会产生全表扫描,改为 a> XX or a 4.在设计...
下面只是从理论上提出了一些建议,最好的原则是大家在上面的基础上,能够使用执行计划来分析,得出最佳的语句的写法。
not exists的子查询,对于子查询不返回行和子查询返回行的查询结果是有区别的 这些细小的差别千万不要被我们所忽视,一旦项目庞大了,想跟踪到具体的错误所花费的时间也是可观的。尽量把这些不必要的错误扼杀在摇篮...
本文将讨论优化器如何评估优化如下的情况和表达式:常量 LIKE 操作符 IN 操作符 ANY和SOME 操作符 ALL 操作符 BETWEEN 操作符 NOT 操作符;传递(Transitivity) 确定性(DETERMINISTIC)函数。
SELECT COUNT(*) FROM t1 where tl.c1 not IN (SELECT t2.c1 FROM t2); 预期的结果是:有多少条数据在 t1 中,同时不在 t2 中,结果为:0,也就是 t1 中数据都在 t2 中,但是很容易就发现某些数据在 t1 中不在 t2 中...
and t.id not in (select distinct a.app_id from test2 a where a.type = 1 and a.rule_id in (152, 153, 154)) 17:20:57 laojiu>@plan PLAN_TABLE_OUTPUT ————————————————————————...
矩形件优化排样算法的研究与实现,矩形件优化排样算法的研究与实现
系统要求进行SQL优化,对效率比较低的SQL进行优化,使其运行效率更高,其中要求对SQL中的部分in/not in修改为exists/not