postgresql优化案例一

文章目录

  • Case 1
    • sql语句
    • 改善前执行计划:
    • 解决方案
    • 改善后执行计划

Case 1

sql语句

explain (analyze,format json)
SELECT
    PLNBEZ,
    to_char(createon, 'YYYY/MM/DD') AS CreateYMD,
    to_char(createon, 'HH24:MI:SS') AS CreateHHMMSS,
    SUM(PrimaryQuantity) AS Qty,
    PRODBATCH,
    AUFNR,
    MAKTX
FROM
    outboundsoleprod
WHERE
    1 = 1
    AND action = 'StockIn'
    AND (('HP' <> '' and productkind =  'HP' ) or ( 'HP' = ''))
    AND (('PGT_IDse' <> '' and facility =  'PGT_IDse' ) or ( 'PGT_IDse' = ''))
    AND (('FGTIn' <> '' and stepid =  'FGTIn' ) or ( 'FGTIn'  = ''))
    AND ((CAST(to_char(createon, 'YYYYMMDDHH24MISS') AS text) >= CAST('20230606080000' AS text)
            AND '20230606080000' <> '' ) or ( '20230606080000' = ''))
    AND ((CAST(to_char(createon, 'YYYYMMDDHH24MISS') AS text) <= CAST('20230607080000' AS text)
            AND '20230607080000' <> '' ) or ( '20230607080000' = ''))
    AND ((''   <> ''
            AND WorkNo = '' ) or ( '' = '')) AND (( '' <> '' and AUFNR =  '')
        OR ('' = ''))
    AND ((''   <> ''
            AND PLNBEZ = '' ) or ( '' = '')) AND (( '' <> '' and ZZSTYLCODE =  '')
        OR ('' = ''))
    AND ((''   <> ''
            AND ZZATCOLR = '' ) or ( '' = '')) AND (( '' <> '' and (ZZOUTSOLMOD =  ''
            OR ZZMIDSOLMOD = '' )) or ( '' = ''))
GROUP BY
    PLNBEZ,
    to_char(createon, 'YYYY/MM/DD'),
    to_char(createon, 'HH24:MI:SS'),
    PRODBATCH,
    AUFNR,
    MAKTX
ORDER BY
    PLNBEZ,
    to_char(createon, 'YYYY/MM/DD'),
    to_char(createon, 'HH24:MI:SS'),
    PRODBATCH,
    AUFNR,
    MAKTX ASC;

改善前执行计划:

postgresql优化案例一_第1张图片

解决方案

从上面的执行计划输出,可以看出针对有几百万数据的大表选择了全表扫描,因此创建合适的索引是好的选择之一

create index idx_outboundsoleprod_02  on outboundsoleprod(action,productkind,facility,stepid,
to_char(createon, 'YYYYMMDDHH24MISS'::text),to_char(createon,'YYYYMMDDHH24MISS'::text))

改善后执行计划

postgresql优化案例一_第2张图片

你可能感兴趣的:(Postgresql,数学建模,matlab,开发语言)