求SQL

Torrenza 2008-01-10 05:08:50
有两个表t1(id,charge) t2(id,charge)
两个表都在id字段上建有索引,两个表数据在1000万以上
现要列出t1表中id不在t2表中的数据

初次接触大数据量查询,还请高手指点
...全文
135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ntalex 2008-07-25
  • 打赏
  • 举报
回复
用minus直接减:
select id from t1
minus
select id from t2
behrman 2008-01-13
  • 打赏
  • 举报
回复
select ti.* from t1,t2 where t1.id=t2.id(+) and t1.id is not null
外连接
注:最好2表的数据都有分区表空间,并且有索引分区表空间。
Croatia 2008-01-12
  • 打赏
  • 举报
回复
赫赫,你可以看一下自己的执行计划的。会明白的。

最多的解释:
和执行顺序有关系。
Exist的时候,会先执行主查询,然后开始子查询,找到第一个匹配的。
IN的时候,会先检查子查询,然后这个结果保存在临时表里面。等到子查询全部结束了,开始主查询。

就是这个需要临时表的动作以及之后和临时表的操作,浪费了时间。
Torrenza 2008-01-11
  • 打赏
  • 举报
回复
select * from t01 where not exists(select 1 from t02 where t01.id=t02.id)
select * from t01 where id not in (select id from t02)

我试了一下,第一条语句要比第二条快很多,
有人能解释一下原因吗?
caofusheng 2008-01-11
  • 打赏
  • 举报
回复
使用表连接的方法速度最快
Croatia 2008-01-10
  • 打赏
  • 举报
回复
select t1.* from t1 where not exists (select 1 from t2 where id = t1.id and charge = t2.charge)
这样的句子,最差。

需要用到两次table access full,就是两次全表扫描,赫赫。
会死人的。
Croatia 2008-01-10
  • 打赏
  • 举报
回复
假如只是想要ID的话,
select id from t01 where not exists (select 1 from t02 where t01.id=t02.id)
select id from t01 where id not in (select id from t02)
这样的句子最快。用到了两个表的INDEX.执行计划完全一样。

假如想要t01里面的所有的数据,
select * from t01 where not exists (select 1 from t02 where t01.id=t02.id)
select * from t01 where id not in (select id from t02)
用到了
1.t01 的 table access full
2.t02 index的index fast full scan
也就是说,执行计划也是一样的。



mantisXF 2008-01-10
  • 打赏
  • 举报
回复
如果两个表的数据都在100万以上的话,用in的话可能速度快一些 ..
dawugui 2008-01-10
  • 打赏
  • 举报
回复
有两个表t1(id,charge) t2(id,charge)
两个表都在id字段上建有索引,两个表数据在1000万以上
现要列出t1表中id不在t2表中的数据

初次接触大数据量查询,还请高手指点

--只查id
select t1.* from t1 where id not in (select id from t2)

--同时查id , charge
select t1.* from t1 where not exists (select 1 from t2 where id = t1.id and charge = t2.charge)
bai_jiong 2008-01-10
  • 打赏
  • 举报
回复
select * from t1 newt1 where not exists
(select 1 from t2 where newt1.id=t2.id)

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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