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很小,怎么优化这个语句?
谢谢!
...全文
1802打赏收藏
这2个查询语句怎么合为一个?
有三张表 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很小,怎么优化这个语句? 谢谢!
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)