求助——SQL语句优化问题

鑫鑫同学 2012-03-30 10:45:28
有两个客户基本信息表:需要将该两个客户基本信息表信息同步。现状:两个客户基本信息表都有重复的数据,可以不去更新重复的数据。
需求:通过p_customer_temp表更新custbaseinfo表数据,条件:通过 p_customer_temp表的cardid 匹配custbaseinfo表。
我的SQL语句:
select count(1) from p_customer_temp pc1 where exists
(select 1 from ( select cardid from p_customer_temp pct where pct.cardid is not null and pct.flag ='1' group by cardid having count(cardid) =1 ) pc2 where pc1.cardid = pc2.cardid)
and not exists (select 1 from custbaseinfo cust where exists(select 1 from custbaseinfo cust1 where cust1.crednum = cust.crednum)
and pc1.cardid = cust.crednum group by cust.crednum having count(cust.crednum)>1 )
求精辟的SQL语句!!!求优化!!!
...全文
198 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacklee_888 2012-04-08
  • 打赏
  • 举报
回复
用merge
hudingchen 2012-03-31
  • 打赏
  • 举报
回复
你sql写太罗嗦,说明表结构,需求,给出数据,和想得到的结果。
ych12333 2012-03-31
  • 打赏
  • 举报
回复
最少的代码才是最快的。
ych12333 2012-03-31
  • 打赏
  • 举报
回复
你这写的太复杂了。很简单的问题你写的那么复杂。直接就是INSERT INTO完了把不在custbaseinfo表中的p_customer_temp 他的数据插入就可以了。哪那么复杂。
xinjie_chen 2012-03-31
  • 打赏
  • 举报
回复
能不能这样,在被插入表中设置客户名为主键,之后使用plsql语句插入:
begin
insert into t1
select * from t2
when exception then
null;
end;
qdkfriend 2012-03-30
  • 打赏
  • 举报
回复
优化不仅仅是SQL语句本身能解决的问题
有时候要从数据库本身入手 索引 分区 临时表 少一些连接查询
并不是一条SQL都得到结果的方法就是好方法
yixilan 2012-03-30
  • 打赏
  • 举报
回复
看看这些表的索引有啥;
再通过执行计划看看各字段,各表连接时,是否按索引查询,根据执行计划调整。

3,499

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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