调整Postgres查询

时间:2018-10-22 11:12:07

标签: sql postgresql query-performance sql-tuning

我需要根据否定条件将两个表连接起来,这需要花费很多时间来执行。

SELECT oola.ship_from_org_id ,
    oola.subinventory,
    oola.line_id ,
    crl.requirement_header_id,
    crl.inventory_item_id
    FROM racesbi_ods.ods_csp_requirement_lines crl
    LEFT JOIN  racesbi_ods.ods_csp_req_line_details crld
        ON crld.requirement_line_id = crl.requirement_line_id
    JOIN racesbi_ods.ods_oe_order_lines_all oola
        ON  crld.source_id               <> oola.line_id
        AND oola.header_id IN
            (SELECT header_id FROM racesbi_ods.ods_oe_order_lines_All
            WHERE line_id = crld.source_id
            )

为了进行调整,我尝试使用临时表。但我仍然面临性能问题。

create temporary table tst1 --ON commit drop    244067
as(select crl.requirement_header_id,
    crl.inventory_item_id,
    crld.requirement_line_id,
    crld.source_id FROM racesbi_ods.ods_csp_requirement_lines crl
    LEFT JOIN  racesbi_ods.ods_csp_req_line_details crld
        ON crld.requirement_line_id = crl.requirement_line_id
)  distributed randomly;
-- Query returned successfully: 244067 rows affected, 15264 ms execution time.

create temporary table tst2 --ON commit drop    2700951
as(
select ship_from_org_id,
    subinventory,
    line_id
    FROM racesbi_ods.ods_oe_order_lines_all
)  distributed randomly;


create temporary table tst3 --ON commit drop 
as(
select tst1.requirement_header_id,
    tst1.inventory_item_id,
    tst2.ship_from_org_id,
    tst2.subinventory,
    tst2.line_id
    FROM tst1 
    JOIN tst2 ON tst2.line_id != tst1.source_id
)  distributed randomly;

请帮助我们处理JOIN中的求反条件

0 个答案:

没有答案