请问这条sql可以怎样优化,效率更高?

ktzhxm 2017-08-30 05:49:30
请问如果数据量大的话,这条sql可以怎样优化效率高?
delete from table1 where pid not in(select id from table2)

select * from table1 where pid not in(select id from table2)
...全文
432 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2017-09-26
  • 打赏
  • 举报
回复
对于rowid删除数据的,如果数据量比较大, 1.建议把其他表的索引删除或者无效 2.通过rowid删除 3.如数数据量特别大的时候可以考虑直接获取create table 然后rname成原来的表名 delete from table1 t where rowid not in (select t2.rowid from table2 t1,table1 t2 where t1.pid = t2.pid ) 或 select * from table1 where pid not in(select id from table2) 如果table2没有重复数据的话,直接外连接就好了 select t.* from table1 t,table2 t1 where t.pid = t2.id(+) and t2.id is null;
lhdz_bj 2017-09-26
  • 打赏
  • 举报
回复
关键看具体的场景,搞清楚当时慢在了什么地方。 具体包括当时的数据环境、还有当时的执行计划等。
S_Pony 2017-09-13
  • 打赏
  • 举报
回复
select * from table1 a where not exists (select 1 from table2 b where a.pid=b.id) 1.把 * 替换为表中的字段名
碧水幽幽泉 2017-09-13
  • 打赏
  • 举报
回复
使用左连接,检索速度非常快。
select t1.*
from table1 t1
left join table2 t2
on t2.id = t1.pid
where t2.id is null
minsic78 2017-09-12
  • 打赏
  • 举报
回复
两张表的总数据量,还有TABLE1中符合not in TABLE2这个条件的数据量有多少?
qq_28398475 2017-09-12
  • 打赏
  • 举报
回复
数据量比较大的话用not exists效率会比较高哦 select * from table1 a where not exists (select 1 from table2 b where a.pid=b.id)
自在如风丶 2017-08-31
  • 打赏
  • 举报
回复
创建临时表,走索引 create table temp_1 as select /*+ parallel(a,20)*/ a.id from table2 a; create index idx_id on temp_1(id) parallel 20; select /*+ index (b)*/ * from table1 a where a.pid not exists (select 1 from temp_1 b where a.pid = b.id)
卖水果的net 2017-08-31
  • 打赏
  • 举报
回复
delete mytable where pid in( select id from t2 minus select pid from mytable ) PS:一定要多重方法反复测试。
  • 打赏
  • 举报
回复
select * from table1 a where not exists (select 1 from table2 b where a.pid=b.id)
  • 打赏
  • 举报
回复
select * from table1 a where not eists (select 1 from table2 b where a.pid=b.id)

17,377

社区成员

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

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