简体   繁体   English

避免多次连接同一个表

[英]Avoid joining same table multiple times

I need help in avoiding joining same table multiple times. 我需要帮助避免多次连接同一个表。 Here is my sample query. 这是我的示例查询。

Table1 have 3 columns ABC_ID,DEF_ID,XYZ_ID and is associated to table2 on ID column . 表1有3列ABC_ID,DEF_ID,XYZ_ID,并与ID列上的table2相关联。 3 column values in table1 can be null and If values are present then i need to return associated value from table 2 using different column names as in select statement. table1中的3个列值可以为null,如果存在值,则需要使用select语句中的不同列名返回表2中的关联值。

As i am using left join i ended up joining table2 three times with table1 on each type if ID column which is having lot of performance issues. 当我使用左连接时,如果ID列有很多性能问题,我最终会在每种类型上使用table1连接table2三次。 How can i write this in different way to avoid join multiple times. 我怎么能以不同的方式写这个来避免连接多次。 Here is sample data. 这是样本数据。 Any help is appreciated 任何帮助表示赞赏

    select 
          (
          CASE
            WHEN ( table2.ID = table1.ABC_ID)
            THEN table2.ID_VAL
            ELSE 'TEST1'
          END ) AS "TEST1",
          (
          CASE
            WHEN (table2a.ID = table1.DEF_ID)
            THEN table2a.ID_VAL
            ELSE 'TEST2'
          END ) AS "TEST2",
          (
          CASE
            WHEN (table2b.ID = table1.XYZ_ID)
            THEN table2b.ID_VAL
            ELSE 'TEST3'
          END ) AS "TEST3"
    from table1 table1 
    left join table2 table2 on   (table2.ID=table1.ABC_ID)
    left join table2 table2a on   ( table2a.id=table1.DEF_ID)
    left join table2 table2b on   ( table2b.id=table1.XYZ_ID)
    where table1.Id_NUM='1'

Table1 表格1

    Id_NUM  ABC_ID     DEF_ID    XYZ_ID
    1       12345      456789    32145
    2       null       456789    32145
    3       12345      null      null

Table2 表2

    ID         ID_VAL
    12345      abcded
    456789     kjwsddk
    321456     wedfgfv

OUTPUT OUTPUT

    TEST1         TEST2       TEST3
    12345         456789      32145

Your join s are fine, but the query can be simplified: 您的join很好,但查询可以简化:

select coalesce(t2a.ID_VAL, 'TEST1') as test1,
       coalesce(t2d.ID_VAL, 'TEST2') as test2,
       coalesce(t2x.ID_VAL, 'TEST3') as test3
from table1 t1 left join
     table2 t2a
     on t2a.ID = t1.ABC_ID left join
     table2 t2d
     on t2d.id = t1.DEF_ID left join
     table2 t2x
     on t2x.id = t1.XYZ_ID
where t1.Id_NUM = 1;

Notes: 笔记:

  • The table aliases should be meaningful. 表别名应该是有意义的。 In this case, the aliases include abbreviations for the column used for the join. 在这种情况下,别名包括用于连接的列的缩写。
  • Presumably, id_num is a number, so don't use single quotes for the value. 据推测, id_num是一个数字,所以不要使用单引号作为值。
  • There is no reason to escape the column names. 没有理由逃避列名称。 The double quotes just add query clutter. 双引号只是添加查询混乱。
  • The case expressions can be replaced by coalesce() , which is simpler (this is not exactly the same if id_val can be NULL , but I'm guessing that doesn't happen). case表达式可以用coalesce()替换,这更简单(如果id_val可以为NULL ,这不完全相同,但我猜这不会发生)。

There is no reason to avoid joining the table three times. 没有理由避免三次加入桌子。 That is what your data model requires, because you have three separate foreign key references. 这就是您的数据模型所需要的,因为您有三个单独的外键引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM