SELECT Trade.TradeId,Trade.Type,Trade.Symbol,Trade.TradeDate, SUM(TradeLine.Notional) / 1000 AS Expr1FROM Trade INNER JOIN TradeLine ON Trade.TradeId = TradeLine.TradeIdWHERE (TradeLine.Id IN (SELECT PairOffId FROM TradeLine AS TradeLine_ WHERE (TradeDate 0)))GROUP BY Trade.TradeId,Trade.Type,Trade.Symbol,Trade.TradeDateORDER BY Trade.Type,Trade.TradeDate当表开始增长时,我担心WHERE子句中IN性能。有没有人对这种查询有更好的策略?子查询返回的记录比TradeLine表中的记录数增长缓慢得多。TradeLine表本身以每天10次的速度增长。1 b, J. G: S: t [$ V9 r
谢谢你。% m9 r; F( q/ o3 k5 S
编辑:我用了将子查询WHERE移到FROM想法。我对所有帮助这个新查询的答案投了赞成票。+ u! h& r: E& e" Y8 T
SELECT Trade.TradeId,Trade.Type,Trade.Symbol,Trade.TradeDate, PairOff.Notional / 1000 AS Expr1 FROM Trade INNER JOIN TradeLine ON Trade.TradeId = TradeLine.TradeId INNER JOIN (SELECT PairOffId,SUM(Notional) AS Notional FROM TradeLine AS TradeLine_ WHERE (TradeDate 0)) AS PairOff ON TradeLine.Id = PairOff.PairOffId ORDER BY Trade.Type,Trade.TradeDate : g. k/ Y/ I/ Z0 ] 解决方案: 8 m) N- S5 |9 A1 U) A' W
IN子句中的子查询不依赖于外部查询中的任何内容。您可以安全地将其移动到FROM一个理性的查询计划生成器将自动执行此操作。 7 a- X. v/ |# X另外,必须调用EXPLAIN PLAN生产中使用的任何查询。这样做并检查DBMS对此查询计划的看法。