T-SQL多连接替代方案

时间:2014-04-27 02:42:29

标签: sql sql-server

我有两个表,PolicyCoverageCoverage(policyID)是引用Policy(policyID)的外键。 Coverage也有列covTypeIDincurred。每个政策有多个覆盖范围。我编写了一个查询来连接这些表,每个策略只有一行。这就是我想出的:

select polNo, alIncur, apdIncur, cargoIncur
from Policy as P
left join (
  select policyID, incurred as alIncur
  from Coverage
  where covTypeID = 1
) as ALC on ALC.policyID = P.policyID
left join (
  select policyID, incurred as apdIncur
  from Coverage
  where covTypeID = 2
) as APDC on APDC.policyID = P.policyID
left join (
  select policyID, incurred as cargoIncur
  from Coverage
  where covTypeID = 3
) as CARGOC on CARGOC.policyID = P.policyID;

我知道这个查询通过Coverage表进行了三次单独的传递,我听说JOIN也很慢。我怀疑有更快的方法来实现这一点,我想知道最佳做法是什么。

1 个答案:

答案 0 :(得分:1)

您可以在没有子查询的情况下重写查询,但我认为这不会影响性能:

select p.polNo, ALC.alIncur, APDC.apdIncur, CARGOC.cargoIncur
from Policy P left join 
     Coverage ALC
     on ALC.policyID = P.policyID and ALC.covTypeID = 1 left join
     Coverage APDC
     on APDC.policyID = P.policyID and APDC.covTypeID = 2 left join
     Coverage CARGOC
     on CARGOC.policyID = P.policyID and CARGOC.covTypeID = 3;

如果您在Coverage(PolicyId, covTypeID)上有索引,则此查询将获得良好的效果。

另一种方法是将不同的覆盖范围放在不同的行上:

select p.polNo, c.covTypeID,
       (case when c.covTypeID = 1 then 'ALC'
             when c.covTypeID = 2 then 'APDC'
             when c.covTypeID = 3 then 'CARGOC'
        end) as which
from Policy P left join 
     Coverage c
     on c.policyID = P.policyID
where c.covTypeID in (1, 2, 3);