【发布时间】:2019-12-03 10:08:03
【问题描述】:
我有并且很容易选择:
define account_id = 7
select * from A where ACCOUNT_ID = &account_id
UNION
select * from B where ACCOUNT_ID = &account_id;
我想将 account_id 作为另一个选择的输入,我这样做了:
select * from A where ACCOUNT_ID in(select accound_id from ACCOUNTS where EMAIL like 'aa@aa.com') -- id 7 returned
UNION
select * from B where ACCOUNT_ID in(select accound_id from ACCOUNTS where EMAIL like 'aa@aa.com')
如何优化为只调用一次select accound_id from ACCOUNTS where EMAIL like 'aa@aa.com'?
【问题讨论】:
-
第1步:你能用
UNION ALL代替UNION吗? -
select accound_id from ACCOUNTS where EMAIL like 'aa@aa.com将此查询的结果分配给一个变量并在联合中使用该变量 -
使用 where exists 而不是 In 语句重写查询
-
我先
UNION [ALL],然后将结果与当前子查询连接起来。 -
但主要的问题是,为什么你有两张如此相似的表A和B?你不能用一张公用表来代替吗?
标签: sql oracle union query-performance