0
我有一個postgres數據庫中的多個表,它們擁有完全獨特的信息。這些信息如果在查詢中正確連接在一起,將會產生我期待的所有可能的組合。我正在尋找的信息是完整的SKU。彙總表中的行數
爲了產生完整的SKU,這個查詢產生所期望的結果: 功能查詢
SELECT
materials.code,
"part_base_parts".code as part_base_parts_id,
shanks.code AS shank_id,
measurements.description
FROM
"part_base_parts"
LEFT JOIN "part_types" ON "part_base_parts"."part_type_id" = "part_types"."id"
RIGHT JOIN "parts_to_shanks" ON "part_base_parts"."id" = "parts_to_shanks"."part_base_part_id"
RIGHT JOIN "parts_to_measurements" ON "part_base_parts"."id" = "parts_to_measurements"."part_base_part_id"
RIGHT JOIN "parts_to_materials" ON "part_base_parts"."id" = "parts_to_materials"."part_base_part_id"
JOIN materials ON "parts_to_materials"."material_id" = materials."id"
JOIN shanks ON "parts_to_shanks"."shank_id" = shanks."id"
JOIN measurements ON "parts_to_measurements"."measurement_id" = measurements."id"
ORDER BY
part_base_parts_id ASC,
materials.code ASC,
shank_id ASC,
measurements.description ASC
鑑於此查詢,我產生32640條記錄(沒有應用索引),用0.82秒查詢時。事情是這樣的......
給定輸出
code part_base_parts_id shank_id description
AA 5105 A 03.0
.
. 32,638 rows in here.
.
ST 6939 D 9/16
這是唯一讓我走了一半,雖然。我需要從查詢中取回結果並從每列中生成總計數。所以,我需要有結果將是:
預期的效果
code: AA - ###0
...
ST - ###0
part_base_parts_id: 5105 - ###0
...
6939 - ###0
shank_id: A - ###0
...
D - ###0
description: 03.0 - ###0
...
9/16 - ###0
是否有生產「預期效果」從Postgres的的方法嗎?
缺少一個'UNION ALL'。我會添加'ORDER BY key,value'。 – klin
我修復了複製/粘貼錯誤,並按照@klin的建議添加了ORDER BY。 –
該查詢工作@ jakub-kania和@klin!我確實在查詢時間上受到了打擊。從.82秒到最後結果〜3.7秒。我將研究索引並查看要應用它們的表以及這可能如何影響查詢。關於增加表演時間的想法? –