我有两个单独的查询,我试图有效加入。
Query 1:
Select Id From Accounts Where Status='OPEN' and Product = 'Product A'
Query 2:
Select AccountId From Transactions Group By AccountId Having Count(*) > 20;
表事务包含数百万行。
我想要实现的是将第一个帐户退回到处于未结状态且产品A超过20个交易的帐户。
到目前为止我得到了这个,但由于事务表的全表扫描,它并不是很有效:
Select A.Id From Accounts A
Left Outer Join (Select AccountId From Transactions Group By AccountId Having Count(*) > 20 ) T on A.Id=T.AccountId
Where A.Status='OPEN' and A.Product = 'Product A'
And rownum = 1
如何优化此查询?
答案 0 :(得分:0)
这会更好,过滤特定帐户和分组??
Select AccountId From Transactions where AccountId in (Select id From Accounts Where Status='OPEN' and Product = 'Product A') Group By AccountId Having Count(*) > 20;