如何先连接两个表然后再连接到另一个表

时间:2020-03-03 00:11:05

标签: sql db2

我有一个DB2数据库,我必须连接三个表。我想联接前两个表,并在联接两个表之后,我想将联接的表联接到另一个第三表。我尝试使用左联接,但找不到预期的结果。我尝试了以下方法:

select AFJKAR as "ELR_Elig_Redirect_SchdID",
       AFEZAM as "Priority",
       AFTSAS as "ELC_Status",
       AFT7CE as "ELC_From_Date",
       AFT8CE as "ELC_Thru_Date",
       AFTTAS as "ELC_Redirect_Action", 
       AFJLAR as "GPI_List",
       AIZAHA as "GPI_List_ID",
       AILUIG as "GPI_Number",
       AICXHG as "GPI_From_Date",
       AICYHG as "GPI_Thru_Date",
       SUEFC4 as "GPI_ID",
      SUB4T3 as "Drug_Name"
from CLMPRDFIL.RCELCP as RCE
left join CLMPRDFIL.RCGP2P as RCG on RCE.AFJLAR = RCG.AIZAHA 
left join CLMPRDFIL.RCGPIP as RCGP on RCG.AIZAHA = RCGP.SUEFC4;

基本上,我想先加入 RCE RCGP2P 表。加入之后,我想通过 RCGPIP 加入它。

1 个答案:

答案 0 :(得分:1)

为此使用相应的优化配置文件/指南。
Optimization profiles and guidelines
如果您认为可以在此指定所需的加入顺序,则可以使用特定的加入顺序获得更好的性能。
请注意,您应该首先尝试收集这些表上的统计信息,以使Db2优化器使用正确的连接顺序。例如,尝试使用前两个表的联接键在其上创建统计视图并收集其统计信息。然后查看原始查询的访问计划,以检查是否获得所需的加入顺序。