这2个查询语句怎么合为一个?

firmbank 2014-01-21 04:53:52
有三张表 aa, bb, cc
表字段均一样 (id, flg)

select a.id, b.flg from aa a, bb b where a.flg = '1' and a.id = b.id
union
select a.id, c.flg from aa a, cc c where a.flg != '1' and a.id = c.id;

aa表数据量很大且flg非索引字段,bb和cc很小,怎么优化这个语句?
谢谢!
...全文
180 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
_拙计 2014-01-24
  • 打赏
  • 举报
回复
select flg, id
          from bb
         where exists (select 1
                  from aa
                 where aa.flg = '1'
                   and aa.id = bb.id)
        union all
        select flg, id
          from cc
         where exists (select 1
                  from aa
                 where aa.flg <> '1'
                   and aa.id = cc.id)
会不会快点?
sunboy_yf 2014-01-22
  • 打赏
  • 举报
回复
指定bb,cc为驱动表试验一下。

17,377

社区成员

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

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