当前位置: 代码迷 >> SQL >> 关于SQL话语优化
  详细解决方案

关于SQL话语优化

热度:54   发布时间:2016-05-05 12:41:22.0
关于SQL语句优化~
本帖最后由 huangcunguilai 于 2011-10-28 16:41:14 编辑
问题描述:
背景:
在oracle9同一实例下有两个用户User1,User2,并且有一张结构相同的表Tab1。(两个用户都有对方的DBA权限,表中数据量超过10万条记录)
问题:
现在要比较两个数据库中表Tab1的数据,列出差异。我实现的SQL语句如下:(但速度太慢)

select *  from 
(
select 'User1' Source, e.* from ( 
select  *
from  User1.T_PB_FLOW_PARA 
 minus 
select  *
from  User2.T_PB_FLOW_PARA ) E 
union all 
select 'User2' Source, f.* from ( 
select  *
from  User2.T_PB_FLOW_PARA 
minus 
select  *
from  User1.T_PB_FLOW_PARA ) F 
) ;

PS:求各位大侠帮忙给优化下,或者说下解决办法,小弟不胜感激
------解决方案--------------------
假设两表ID连接,在ID上有索引
select a.* from a left join b on a.id=b.id where b.id is null
union all
select b.* from b left join a on a.id=b.id where a.id is null
------解决方案--------------------
oracle 就是minus 
  相关解决方案