Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 查询调优postgres_Postgresql - Fatal编程技术网

Postgresql 查询调优postgres

Postgresql 查询调优postgres,postgresql,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 = 7

我正在尝试将以下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
下面是查询的解释计划


阅读表格一次,然后进行格式设置:

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

我曾经发表过一篇关于如何提高大数据分析性能的文章,也许你会在那里找到一些关于查询规划等的有用信息。你能不能将所有这些信息都打包到同一个查询中,并在一行返回结果?请不要在收到答案后将你的问题编辑成另一行。