【select】优化问题
【select】优化问题
编写 sql 脚本时, 通常情况下,如果 select 后面 直接 跟 select 的查询速度会比较慢。
把嵌套的 select 拿到 join 后面可以提高查询速度。
如:
嵌套的 select 查询:
[sql] select co.CustomerOrderID, SumPrice = (select sum(Price) from CustomerOrderDetail cod where cod.CustomerOrderID = co.CustomerOrderID) from CustomerOrder co</span>
改成:
[sql] select co.CustomerOrderID, tCo.Price from CustomerOrder co join (select CustomerOrderID, sum(Price) Price from CustomerOrderDetail cod group by CustomerOrderID) tCo on tCo.CustomerOrderID = co.CustomerOrderID</span>