0

Loop1 是通过一些简单的逻辑得出的。Loop2 是通过非常复杂的逻辑导出的。Loop3 很简单,就是在 Loop2 上加 2 得到

现在,我想通过 Loop1、Loop2 和 Loop3 的组合推导出 Loop4。问题是 loop2 是一个非常繁重的逻辑,如果我在 Loop3 中再次导出逻辑,查询运行速度非常慢。为了更清楚,我发现 loop3 使用 loop1&2 和 loop4 使用 loop1,2&3。请提出一种方法来使这个查询工作。

select sre.shipmentId, 
       loop1.TRY1, 
       loop2.TRY2, 
       loop3.TRY3, 
       (select case when u>0 then loop1.TRY1 when u>1 then loop2.TRY2 else loop3.TRY3 end) as loop4
from `shipmentRouteEvent` sre
left join (select sre1.shipmentId as s1, (case when .....>0 then .... end) ad TRY1
           from `shipmentRouteEvent` sre1
           where sre1.updateDate='2013-07-01'
          ) as loop1 on sre.id=try1.id
left join (select some heavy logic which will modify TRY1 to TRY2) as loop2
left join (select (TRY2+2) as TRY3) as loop3
where sre.updateDate='2013-07-01'
4

1 回答 1

0

我认为您想将其作为嵌套子查询来执行:

from (select . . .
      fro (select . . .
           from `shipmentRouteEvent` sre left join
                 (select sre1.shipmentId as s1, (case when .....>0 then .... end) ad TRY1
                  from `shipmentRouteEvent` sre1
                  where sre1.updateDate='2013-07-01'
                ) loop1
                on sre.id=try1.id
           ) loop1 left join
           (select some heavy logic which will modify TRY1 to TRY2
           ) loop2
           on . . .
      ) loop2 left outer join
      (. . .
      ) loop3
      on . . .

每一层的嵌套都可以让你使用下一层的结果,所以结果不需要重新计算。

于 2013-07-22T01:20:53.280 回答