-1

此查询耗时 944,319.072 毫秒并返回 27 行。

SELECT
    AP_ATTRIBUTE.stylename,
    AP_BASIC.style,
    AP_ATTRIBUTE.color,
    AP_ATTRIBUTE.size,
    AP_BASIC.ap_sizesequence_id AS apSizesequenceId,
    M_TRANSACTION.movementdate AS MOVEMENTDATE,
    SUM(M_TRANSACTION.MOVEMENTQTY) AS MOVEMENTQTY,
    COALESCE(M_PRODUCTPRICE.pricelist, 999999) AS pricelist,
    M_INOUT.M_INOUT_ID,
    M_INOUT.DOCUMENTNO AS DOCUMENTNO,
    C_BPARTNER.VALUE AS BPNAME,
    '' AS M_MOVEMENTLINE_ID,
    '' AS MOVEMENT,
    '' AS MOVEMENT_DESCRIPTION,
    '' AS M_INVENTORY_ID,
    '' AS M_MOVEMENT_ID,
    '' AS M_PRODUCTIONPLAN_ID,
    '' AS PRODUCTION_PLAN
FROM 
    M_TRANSACTION
    LEFT JOIN 
    AP_ATTRIBUTE ON AP_ATTRIBUTE.M_PRODUCT_ID = M_TRANSACTION.M_PRODUCT_ID
    LEFT JOIN 
    AP_BASIC ON AP_BASIC.AP_BASIC_ID = AP_ATTRIBUTE.AP_BASIC_ID
    LEFT JOIN 
    M_PRODUCTPRICE ON 
        M_TRANSACTION.M_PRODUCT_ID = M_PRODUCTPRICE.M_PRODUCT_ID
        AND M_PRODUCTPRICE.m_pricelist_version_id = '69CBAFD24AB24E3CB9B9A44E282D62EC',
        M_INOUTLINE,
        M_INOUT,
        C_BPARTNER
WHERE 
    M_TRANSACTION.M_INOUTLINE_ID = M_INOUTLINE.M_INOUTLINE_ID
    AND M_INOUTLINE.M_INOUT_ID = M_INOUT.M_INOUT_ID
    AND M_TRANSACTION.M_INOUTLINE_ID IS NOT NULL
    AND M_INOUT.C_BPARTNER_ID = C_BPARTNER.C_BPARTNER_ID
    AND M_TRANSACTION.AD_CLIENT_ID IN (
        '0',
        'FF80818129E99DC80129E9AD546A0708'
        )
    AND M_TRANSACTION.AD_ORG_ID IN (
        '44523886B0E0423584BB0E87133F3EE4',
        '4510F92E007E48B28EE6CEB088A78348',
        '0'
        )
    AND AP_ATTRIBUTE.isActive = 'Y'
    AND 1 = 1
    AND M_TRANSACTION.movementdate >= to_date('2012-12-01')
    AND M_TRANSACTION.movementdate < to_date('2013-01-01')
    AND M_INOUT.M_WAREHOUSE_ID = '50657AE0F49A421A9F10154C8632A726'
    AND M_INOUT.ISSOTRX = 'N'
GROUP BY
    AP_ATTRIBUTE.stylename,
    AP_BASIC.style,
    AP_ATTRIBUTE.color,
    AP_ATTRIBUTE.size,
    AP_BASIC.ap_sizesequence_id,
    M_TRANSACTION.movementdate,
    M_PRODUCTPRICE.pricelist,
    M_INOUT.M_INOUT_ID,
    M_INOUT.DOCUMENTNO,
    C_BPARTNER.VALUE
ORDER BY
    M_TRANSACTION.movementdate,
    M_INOUT.M_INOUT_ID,
    style,
    color,
    size

中有 2,962,657 行M_TRANSACTION
pk:m_transaction_id
索引:m_product_id

中有 106,380 行AP_ATTRIBUTE
pk: AP_ATTRIBUTE_id
无索引

中有 6,128 行AP_BASIC
pk: AP_BASIC_id
无索引

中有 223,917 行M_PRODUCTPRICE
索引:(m_pricelist_version_id,m_product_id)

中有 2,000,329 行M_INOUTLINE
pk:m_inoutline_id
索引:m_product_id、m_inout_id

中有 45,919 行M_INOUT
pk:m_inout_id
索引:m_inout_id

EXPLAIN ANALYZE揭示了这个计划(也在explain.depesz.com):

GroupAggregate  (cost=201073.74..201073.78 rows=1 width=138) (actual time=944255.849..944256.284 rows=517 loops=1)
  ->  Sort  (cost=201073.74..201073.74 rows=1 width=138) (actual time=944255.828..944255.859 rows=517 loops=1)
        Sort Key: m_transaction.movementdate, m_inout.m_inout_id, ap_basic.style, ap_attribute.color, ap_attribute.size, ap_attribute.stylename, ap_basic.ap_sizesequence_id, m_productprice.pricelist, m_inout.documentno, c_bpartner.value
        Sort Method: quicksort  Memory: 162kB
        ->  Nested Loop  (cost=0.00..201073.73 rows=1 width=138) (actual time=272067.456..944244.793 rows=517 loops=1)
              ->  Nested Loop  (cost=0.00..201066.14 rows=1 width=146) (actual time=272067.433..944239.495 rows=517 loops=1)
                    ->  Nested Loop  (cost=0.00..201058.34 rows=1 width=107) (actual time=13.225..943784.425 rows=28860 loops=1)
                          ->  Nested Loop Left Join (cost=0.00..201049.06 rows=1 width=107) (actual time=13.197..940249.631 rows=28860 loops=1)
                                ->  Nested Loop Left Join (cost=0.00..201040.64 rows=1 width=136) (actual time=13.122..938969.396 rows=28860 loops=1)
                                      ->  Nested Loop (cost=0.00..201032.88 rows=1 width=130) (actual time=13.086..938463.010 rows=28860 loops=1)
                                            Join Filter: ((m_transaction.m_product_id)::text = (ap_attribute.m_product_id)::text)
                                            ->  Seq Scan on m_transaction  (cost=0.00..188132.06 rows=1 width=78) (actual time=0.015..932.400 rows=28936 loops=1)
                                                  Filter: ((m_inoutline_id IS NOT NULL) AND ((ad_client_id)::text = ANY ('{0,FF80818129E99DC80129E9AD546A0708}'::text[])) AND (movementdate >= '2012-12-01 00:00:00'::timestamp without time zone) AND (movementdate < '2013-01-01 00:00:00'::timestamp without time zone) AND ((ad_org_id)::text = ANY ('{44523886B0E0423584BB0E87133F3EE4,4510F92E007E48B28EE6CEB088A78348,0}'::text[])))
                                            ->  Seq Scan on ap_attribute  (cost=0.00..11820.75 rows=86405 width=85) (actual time=0.001..20.121 rows=86645 loops=28936)
                                                  Filter: (isactive = 'Y'::bpchar)
                                      ->  Index Scan using ap_bs_key on ap_basic  (cost=0.00..7.76 rows=1 width=72) (actual time=0.013..0.014 rows=1 loops=28860)
                                            Index Cond: ((ap_basic_id)::text = (ap_attribute.ap_basic_id)::text)
                                ->  Index Scan using m_productprice_pricelist_ve_un on m_productprice  (cost=0.00..8.40 rows=1 width=36) (actual time=0.042..0.042 rows=1 loops=28860)
                                      Index Cond: (((m_pricelist_version_id)::text = '69CBAFD24AB24E3CB9B9A44E282D62EC'::text) AND ((m_transaction.m_product_id)::text = (m_product_id)::text))
                          ->  Index Scan using m_inoutline_key on m_inoutline  (cost=0.00..9.27 rows=1 width=66) (actual time=0.120..0.120 rows=1 loops=28860)
                                Index Cond: ((m_inoutline_id)::text = (m_transaction.m_inoutline_id)::text)
                    ->  Index Scan using m_inout_key on m_inout (cost=0.00..7.79 rows=1 width=72) (actual time=0.015..0.015 rows=0 loops=28860)
                          Index Cond: ((m_inout_id)::text = (m_inoutline.m_inout_id)::text)
                          Filter: (((m_warehouse_id)::text = '50657AE0F49A421A9F10154C8632A726'::text) AND (issotrx = 'N'::bpchar))
              ->  Index Scan using c_bpartner_key on c_bpartner (cost=0.00..7.57 rows=1 width=56) (actual time=0.007..0.008 rows=1 loops=517)
                    Index Cond: ((c_bpartner_id)::text = (m_inout.c_bpartner_id)::text)

总运行时间:944,256.536 毫秒

27资料列</p>

总运行时间:944,319.072 毫秒

如何提高查询的性能?

4

1 回答 1

1

我重新格式化,去除了噪音和杂物,添加了表别名并转换为显式 ANSI 连接语法,除此之外:

SELECT
    a.stylename,
    b.style,
    a.color,
    a.size,
    b.ap_sizesequence_id,
    t.movementdate,
    SUM(t.movementqty) AS movementqty,
    COALESCE(p.pricelist, 999999) AS pricelist,
    i.m_inout_id,
    i.documentno,
    c.value as bpname,
    '' as m_movementline_id,
    '' as movement,
    '' as movement_description,
    '' as m_inventory_id,
    '' as m_movement_id,
    '' as m_productionplan_id,
    '' as production_plan
FROM   M_TRANSACTION       t
JOIN   AP_ATTRIBUTE        a ON a.m_product_id = t.m_product_id
JOIN   M_INOUTLINE         l ON l.m_inoutline_id = t.m_inoutline_id
JOIN   M_INOUT             i ON i.m_inout_id = l.m_inout_id
JOIN   C_BPARTNER          c ON c.c_bpartner_id = i.c_bpartner_id
LEFT   JOIN AP_BASIC       b ON b.ap_basic_id = a.ap_basic_id
LEFT   JOIN M_PRODUCTPRICE p ON p.m_product_id = t.m_product_id
            AND p.m_pricelist_version_id = '69CBAFD24AB24E3CB9B9A44E282D62EC'
WHERE  t.ad_client_id = ANY(
       '{FF80818129E99DC80129E9AD546A0708
        ,0}')
AND    t.ad_org_id = ANY(
       '{44523886B0E0423584BB0E87133F3EE4
        ,4510F92E007E48B28EE6CEB088A78348
        ,0}')
AND    t.movementdate >= '2012-12-01 0:0'::timestamp
AND    t.movementdate <  '2013-01-01 0:0'::timestamp
AND    i.m_warehouse_id = '50657AE0F49A421A9F10154C8632A726'
AND    i.issotrx = 'N'
AND    a.isactive = 'Y'
GROUP BY
    a.stylename,
    a.color,
    a.size,
    b.style,
    b.ap_sizesequence_id,
    t.movementdate,
    p.pricelist,
    i.m_inout_id,
    c.value
ORDER BY
    t.movementdate,
    i.m_inout_id,
    b.style,
    a.color,
    a.size

要点

  • 添加表别名以使怪物更易于阅读。

  • 删除了几个冗余的列别名,例如b.ap_sizesequence_id AS apsizesequenceid

  • 转换为小写,混合大小写标识符只是噪音。

  • 将您的联接转换为具有联接条件的ANSI 联接。使复杂查询更易于阅读和调试。

  • i.documentnoGROUP BY子句中删除。我在 Postgres 9.1 的 PK 列i.m_inout_id已经覆盖了整个表。可能有更多的冗余列。自己检查。

  • t.m_inoutline_id IS NOT NULL是多余的。覆盖: JOIN M_INOUTLINE l ON l.m_inoutline_id = t.m_inoutline_id

  • = ANY()略快于IN()

  • AND a.isactive = 'Y'将 强制转换LEFT JOIN AP_ATTRIBUTE为常规JOIN.

  • t.movementdate是一个timestamp(正如 EXPLAIN 输出告诉我的那样)。相应地简化表达。

  • i.issotrx并且a.isactive很可能应该是 typeboolean而不是character( bpchar)。(对此无能为力)

索引

  • 至少需要一个索引M_TRANSACTION.movementdate

    CREATE INDEX m_transaction_movementdate_idx
    ON m_transaction(movementdate)
    

    或者可能是一个多列索引,例如:

    CREATE INDEX m_transaction_movementdate_idx
    ON m_transaction(ad_client_id, ad_org_id, movementdate)
    

    取决于您的实际数据分布和其他情况。

  • 你需要一个索引AP_ATTRIBUTE.m_product_id

    CREATE INDEX ap_attribute_m_product_id_idx
    ON ap_attribute (m_product_id)
    

    也许更好的部分索引

    CREATE INDEX ap_attribute_m_product_id_idx
    ON ap_attribute (m_product_id)
    WHERE isactive = 'Y'
    

    我发布了很多关于部分索引的答案。尝试在 SO 上搜索

  • 另一个索引可能会有所帮助:

    CREATE INDEX m_inout_m_warehouse_id_idx ON m_inout (m_warehouse_id)
    

    同样,可能偏向于

    ...
    WHERE issotrx = 'N'
    
于 2013-01-04T15:14:00.047 回答