连接表时如何避免子查询?

时间:2019-09-02 14:41:07

标签: postgresql

-我在这里是新手,觉得这是琐碎的或错误的数据库建模-

在以下情况下:

create TABLE objects (
id BIGSERIAL NOT NULL UNIQUE PRIMARY KEY,
name text unique
);

create TABLE features (
id BIGSERIAL NOT NULL UNIQUE PRIMARY KEY,
name text
);

create TABLE features_map (
id BIGSERIAL NOT NULL UNIQUE PRIMARY KEY,
o_id BIGINT REFERENCES objects ON DELETE restrict,
f_id BIGINT REFERENCES features ON DELETE restrict,
value text
);

insert into features(id, name) values 
(1, 'length'), 
(2, 'wheels'); 

insert into objects(id, name) values 
(1, 'car'), 
(2, 'bike'); 

insert into features_map(o_id,f_id,value) values
(1,1,'4.5m'),
(1,2,'4'),
(2,1,'2.3m'),
(2,2,'2');

我想要获得所需的输出,即左连接,但将结果合并到具有不同列的单行中:

select o.id, o.name, 
(select value from features_map fm join features f on fm.f_id=f.id where fm.o_id=o.id and f.name='length') as length, 
(select value from features_map fm join features f on fm.f_id=f.id where fm.o_id=o.id and f.name='wheels') as wheels
from objects o;

id|name|length|wheels|
--|----|------|------|
 1|car |4.5m  |4     |
 2|bike|2.3m  |2     |

随着表大小的增加,这种类型的查询变得太慢,例如对象计数> 10000,要素地图计数> 40000。

使用join,查询保持很快,但是结果(显然)出现在多行中:

select *
from objects o
join features_map fm on o.id=fm.o_id
join features f on f.id=fm.f_id;

id|name|id|o_id|f_id|value|id|name  |
--|----|--|----|----|-----|--|------|
 1|car | 1|   1|   1|4.5m | 1|length|
 1|car | 2|   1|   2|4    | 2|wheels|
 2|bike| 3|   2|   1|2.3m | 1|length|
 2|bike| 4|   2|   2|2    | 2|wheels|

如何以join的速度获得所需的输出?

Ciao, aaWnSd

1 个答案:

答案 0 :(得分:1)

demo:db<>fiddle

您需要一个数据透视表。这可以通过将数据集分组然后与过滤后的值进行汇总来实现。

在这种情况下,已使用MIN()函数,但这并不重要。您也可以使用MAX()SUM(),因为您只有一个值。因此,一个值的MIN() ==这个值的MAX() == SUM() ...

SELECT
    o.id,
    o.name,
    MIN(value) FILTER (WHERE f.name = 'length') AS length,
    MIN(value) FILTER (WHERE f.name = 'wheels') AS wheels
FROM objects o
JOIN features_map fm ON o.id=fm.o_id
JOIN features f ON f.id=fm.f_id
GROUP BY o.id, o.name
ORDER BY o.id