查询调整postgres

时间:2018-11-14 12:13:57

标签: postgresql

我正尝试将以下postgresql查询的响应时间从当前的5秒减少到1 ...也为该查询附加了解释计划..请帮助...

(
SELECT 
      1 AS RowNumber
     ,'Total Countries' AS RowLabel
     ,COUNT(DISTINCT ITS.abc CountryTrading) AS Aggregation
FROM ObjectViews.abc InstnTradeSummary AS ITS  
WHERE ITS.KeyInstn = 7402194
      AND ITS.TradeDataMonthYearPublish >= date_trunc('month', current_date) + interval '-5 years'
      AND ITS.TradeDataMonthYearPublish <= date_trunc('month', current_date)
      AND ITS.abc CountryTrading IS NOT NULL
GROUP BY ITS.KeyInstn

UNION

SELECT 
      2 AS RowNumber
     ,'Total Shipments' AS RowLabel
     ,SUM(ITS.ShipmentCount) AS TotalShipments
FROM ObjectViews.abc InstnTradeSummary AS ITS  
WHERE ITS.KeyInstn = 7402194
      AND ITS.TradeDataMonthYearPublish >= date_trunc('month', current_date) + interval '-5 years'
      AND ITS.TradeDataMonthYearPublish <= date_trunc('month', current_date)
GROUP BY ITS.KeyInstn

UNION

SELECT 
      3 AS RowNumber
     ,'Total Weight in kg' AS RowLabel
     ,SUM(COALESCE(ITS.ShipmentWeightAR, ITS.ShipmentWeightEst)) AS TotalShipmentWeight
FROM ObjectViews.abc InstnTradeSummary AS ITS  
WHERE ITS.KeyInstn = 7402194
      AND ITS.TradeDataMonthYearPublish >= date_trunc('month', current_date) + interval '-5 years'
      AND ITS.TradeDataMonthYearPublish <= date_trunc('month', current_date)
GROUP BY ITS.KeyInstn

UNION

SELECT 
      4 AS RowNumber
     ,'Total Volume in TEU' AS RowLabel
     ,SUM(COALESCE(ITS.ShipmentVolumeAR, ITS.ShipmentVolumeEst)) AS TotalShipmentVolume
FROM ObjectViews.abc InstnTradeSummary AS ITS  
WHERE ITS.KeyInstn = 7402194
      AND ITS.TradeDataMonthYearPublish >= date_trunc('month', current_date) + interval '-5 years'
      AND ITS.TradeDataMonthYearPublish <= date_trunc('month', current_date)
GROUP BY ITS.KeyInstn

                   ) ORDER BY RowNumber

下面是查询的解释计划...

https://explain.depesz.com/s/xgC2

1 个答案:

答案 0 :(得分:2)

读取表格一次,之后进行格式化:

SELECT
    v.row_number,
    v.row_label,
    CASE v.row_number
        WHEN 1 THEN s.total_countries
        WHEN 2 THEN s.total_shipments
        WHEN 3 THEN s.total_shipment_weight
        ELSE        s.total_shipment_volume
    END AS total
FROM (
    VALUES
    (1, 'Total Countries'),
    (2, 'Total Shipments'),
    (3, 'Total Weight in kg'),
    (4, 'Total Volume in TEU')
) AS v(row_number, row_label)
LEFT JOIN (
    SELECT
        COUNT(DISTINCT ITS.abc CountryTrading) FILTER (WHERE ITS.abc CountryTrading IS NOT NULL) AS total_countries,
        SUM(ITS.ShipmentCount) AS total_shipments,
        SUM(COALESCE(ITS.ShipmentWeightAR, ITS.ShipmentWeightEst)) AS total_shipment_weight,
        SUM(COALESCE(ITS.ShipmentVolumeAR, ITS.ShipmentVolumeEst)) AS total_shipment_volume
    FROM ObjectViews.abc InstnTradeSummary AS ITS  
    WHERE ITS.KeyInstn = 7402194
          AND ITS.TradeDataMonthYearPublish >= date_trunc('month', current_date) + interval '-5 years'
          AND ITS.TradeDataMonthYearPublish <= date_trunc('month', current_date)
    GROUP BY ITS.KeyInstn
) AS s ON TRUE
ORDER BY v.row_number