【问题标题】:Avoiding multiple left joins of a single table for multiple columns避免单个表的多个列的多个左连接
【发布时间】:2018-07-18 21:22:44
【问题描述】:

我面临的问题的模拟表和数据:

create table table1 (people varchar(10), special_id varchar(20));
create table table2 (special_id varchar(20), dependent_value int8, value_wanted varchar(5));

insert into table1 values 
('person1', 'abc'),
('person1', 'abc'),
('person1', 'abc'),
('person1', 'abc'),
('person1', 'bbb'),
('person1', 'bbb'),
('person1', 'ccd');


insert into table2 values
('abc', '02', 'boom'),
('abc', '01', 'zoom'),
('bbb', '01', 'woom'),
('abc', '03', 'whom');

这是我想要达到的结果:

+---------+------------+---------+---------+
| people  | special_id | code_01 | code_02 |
+---------+------------+---------+---------+
| person1 | abc        | zoom    | boom    |
| person1 | abc        | zoom    | boom    |
| person1 | abc        | zoom    | boom    |
| person1 | abc        | zoom    | boom    |
| person1 | bbb        | woom    | NULL    |
| person1 | bbb        | woom    | NULL    |
| person1 | ccd        | NULL    | NULL    |
+---------+------------+---------+---------+

使用模拟表,我可以通过这样做来创建上面的表:

select t1.*, t2.value_wanted as code_01, t2_1.value_wanted as code_02
from table1 t1
left join table2 t2
on t2.special_id = t1.special_id and t2.dependent_value = '01'
left join table2 t2_1
on t2_1.special_id = t1.special_id and t2_1.dependent_value = '02';

问题是为了添加code_03 和其他列,我必须不断添加左连接。这似乎不是很有效。考虑到性能,有没有更好的方法来做到这一点?

【问题讨论】:

    标签: sql postgresql performance


    【解决方案1】:

    你也可以使用聚合:

    select t1.*, t2.code_01, t2.code_02, t2.code_03
    from table1 t1 left join table2
         (select t2.special_id,
                 max(case when t2.dependent_value = '01' then t2.value_wanted end) as code_01,
                 max(case when t2.dependent_value = '02' then t2.value_wanted end) as code_02,
                 max(case when t2.dependent_value = '03' then t2.value_wanted end) as code_03
          from table2 t2
          group by t2.special_id
         ) t2
         on t2.special_id = t1.special_id ;
    

    您需要向子查询添加新条件,而不是新连接。这是更快还是更慢。 . .那要看情况了。

    【讨论】:

    • 谢谢,这行得通。在性能方面,max(t2.value_wanted) FILTER (WHERE t2.dependent_value = '01') as code_01 会产生相同的结果,但会带来轻微的性能提升。
    • @Simon 。 . .我猜要快 10-20%。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多