二个表关联,求高效的sql写法

blliy117 2009-11-04 10:06:03
两个存放有千万级数据的表
A(agent_id,name)和B(agent_id,xxx),A表的agent_id是主键,B表的agent_id是外键,关联到A表,
写条sql,从A表取出不存在于B表的数据

求高效的sql写法
...全文
191 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
超叔csdn 2009-11-05
  • 打赏
  • 举报
回复
肯定left join
gerrynj 2009-11-05
  • 打赏
  • 举报
回复
还以为not exists效率最高呢
liusong_china 2009-11-05
  • 打赏
  • 举报
回复
left join效率高一些
iqlife 2009-11-05
  • 打赏
  • 举报
回复
关注结果和效率评测
qq82296344 2009-11-05
  • 打赏
  • 举报
回复
select a.* from a, b
where a.agent_id != b.agent_id 最简单了
guangli_zhang 2009-11-05
  • 打赏
  • 举报
回复
select a.* from a, b
where a.agent_id = b.agent_id (+)
and b.agent_id is null;
zhangwonderful 2009-11-04
  • 打赏
  • 举报
回复
完全同意7楼的说法
xiaowu1010 2009-11-04
  • 打赏
  • 举报
回复
Oracle查询优化(在可能的情况下,用or不用in,用exist不用count,用外连查询不用子查询,子 查询要进行全表扫描,对性能影响大等),外连查询,索引(对性能的影响),锁(表级锁和行级锁),存储过程,报表查询(其实也就是复杂业务的查询)
xiaowu1010 2009-11-04
  • 打赏
  • 举报
回复
left join效率高点。
blliy117 2009-11-04
  • 打赏
  • 举报
回复
造这么多数据累啊,我是希望大家的经验给我一点启发嘛1
小灰狼W 2009-11-04
  • 打赏
  • 举报
回复
楼主测试比较下
我也关注下结果
blliy117 2009-11-04
  • 打赏
  • 举报
回复

select a.* from a, b
where a.agent_id = b.agent_id (+)
and b.rowid is null;



select * from a
where not exists(select 1 from b
where agent_id=a.agent_id)


那个高一些
小灰狼W 2009-11-04
  • 打赏
  • 举报
回复
select * from a
where not exists(select 1 from b
where agent_id=a.agent_id)

KingSunSha 2009-11-04
  • 打赏
  • 举报
回复
通常有3种写法,not in, not exists 和 left join


select * from a
where agent_id not in (select agent_id from b);
select * from a
where not exists (select 1 from b where agent_id=a.agent_id);
select a.* from a, b
where a.agent_id = b.agent_id (+)
and b.rowid is null;


大部分情况下,left join效率高。

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧