我試圖在postgres中將三個錶鏈接在一起。
所有三個表都是從子查詢生成的。第一個表通過變量call_sign作爲FULL JOIN鏈接到第二個表(因爲我想要來自兩個表的條目的超集)。第三個表與call_sign上的第二個表也有INNER JOIN(理論上可能已經鏈接到第一個表)
查詢運行但速度很慢,我感覺隨着添加更多數據會變得更慢。我意識到我可以做某些事情來加速事情 - 比如不在子查詢中抽取不必要的數據,也不會將文本轉換爲數字。但是,有沒有更好的方法來構建這三個表之間的JOIN?
任何意見,將不勝感激,因爲我是postgres的新手。更有效的方式在Postgres中一起加入三個表
下面是代碼:
select
(CASE
WHEN tmp1.frequency_assigned is NULL
THEN tmp2.lower_frequency
ELSE tmp1.frequency_assigned END) as master_frequency,
(CASE
WHEN tmp1.call_sign is NULL
THEN tmp2.call_sign
ELSE tmp1.call_sign END) as master_call_sign,
(CASE
WHEN tmp1.entity_type is NULL
THEN tmp2.entity_type
ELSE tmp1.entity_type END) as master_entity_type,
(CASE
WHEN tmp1.licensee_id is NULL
THEN tmp2.licensee_id
ELSE tmp1.licensee_id END) as master_licensee_id,
(CASE
WHEN tmp1.entity_name is NULL
THEN tmp2.entity_name
ELSE tmp1.entity_name END) as master_entity_name,
tmp3.market_name
FROM
(select cast(replace(frequency_assigned, ',','.') as decimal) AS frequency_assigned,
frequency_upper_band,
f.uls_file_number,
f.call_sign,
entity_type,
licensee_id,
entity_name
from combo_fr f INNER JOIN combo_en e
ON f.call_sign=e.call_sign
ORDER BY frequency_assigned DESC) tmp1
FULL JOIN
(select cast(replace(lower_frequency, ',','.') as decimal) AS lower_frequency,
upper_frequency,
e.uls_file_number,
mf.call_sign,
entity_type,
licensee_id,
entity_name
FROM market_mf mf INNER JOIN combo_en e
ON mf.call_sign=e.call_sign
ORDER BY lower_frequency DESC) tmp2
ON tmp1.call_sign=tmp2.call_sign
INNER JOIN
(select en.call_sign,
mk.market_name
FROM combo_mk mk
INNER JOIN combo_en en
ON mk.call_sign=en.call_sign) tmp3
ON tmp2.call_sign=tmp3.call_sign
ORDER BY master_frequency DESC;
首先,您可以使用['COALESCE'](https://www.postgresql.org/docs/current/static/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL)而不是' CASE WHEN ... IS NULL'。 – kennytm
其次,你應該刪除子查詢中的所有'ORDER BY',在這種情況下這是完全無用的。 – Dario
在任何涉及的表格中,「call_sign'是可空的還是唯一的?其他領域是否可以空?換句話說,這些'CASE WHEN ...'是否會返回來自同一個表的每一行的值,或者可能會出現這樣的情況:答案中的某一行是來自'combo_fr'和'market_mf'的值的混合值(忽略'call_sign')? – Dario