当前位置: 代码迷 >> Sql Server >> sql 话语优化挑战
  详细解决方案

sql 话语优化挑战

热度:102   发布时间:2016-04-24 18:21:51.0
sql 语句优化挑战
本帖最后由 thesum 于 2014-03-20 20:48:40 编辑
INSERT INTO master.dbo.[xl]
            (num6,
             SCD,
             BCD,
             Sscore,
             Bscore)
SELECT num5,
       SCD,
       b.CollectDate,
       Sscore,
       b.SwapScore
FROM   master.dbo.[xl] a,
       new.dbo.RecordInsure b
WHERE  num5 > 0
       AND num5 = b.TargetUserID
       AND b.CollectDate < SCD
       AND b.TradeType = 3
       AND (SELECT TOP 1 TargetUserID
            FROM   new.dbo.RecordInsure
            WHERE  CollectDate < a.SCD
                   AND TradeType = 3
                   AND ( TargetUserID = a.num5
                          OR SourceUserID = a.num5 )
            ORDER  BY CollectDate DESC) = a.num5
       AND (SELECT TOP 1 CollectDate
            FROM   new.dbo.RecordInsure
            WHERE  CollectDate < a.SCD
                   AND TradeType = 3
                   AND ( TargetUserID = a.num5
                          OR SourceUserID = a.num5 )
            ORDER  BY CollectDate DESC) = b.CollectDate
------解决方案--------------------
试试这个:

INSERT INTO master.dbo.[xl]
            (num6,
             SCD,
             BCD,
             Sscore,
             Bscore)
SELECT num5,
       SCD,
       b.CollectDate,
       Sscore,
       b.SwapScore
FROM   master.dbo.[xl] a
inner join 
(
    select TargetUserID,max(CollectDate) CollectDate
    from new.dbo.RecordInsure
    where TradeType = 3
    group by TargetUserID
)b
 on num5 = b.TargetUserID and b.CollectDate < a.SCD
WHERE  num5 > 0
  相关解决方案