2017-03-13 51 views
0

我想創建一個查詢從表X.在同一個查詢,以優化

我currenly處理一個數據庫動態更新從數據表Y一些值選擇和更新的PostgreSQL是主機10kk行,我想優化處理的時間量。

選擇:

SELECT cycle_completa.numcycle AS actual_cycle, 
    min(cycle_completa.pump1pre) AS min_pump1pre, 
    min(cycle_completa.pump2pre) AS min_pump2pre, 
    min(cycle_completa.pump3pre) AS min_pump3pre, 
    min(cycle_completa.slidevel) AS min_slidevel, 
    min(cycle_completa.slidepos) AS min_slidepos, 
    min(cycle_completa.slideforce) AS min_slideforce, 
    min(cycle_completa.slideservopilotpre) AS min_slideservopilotpre, 
    min(cycle_completa.slidecentralpre) AS min_slidecentralpre, 
    min(cycle_completa.slidelateralpre) AS min_slidelateralpre, 
    min(cycle_completa.slideringpre) AS min_slideringpre, 
    min(cycle_completa.slidepistonpre) AS min_slidepistonpre, 
    min(cycle_completa.slidey42pre) AS min_slidey42pre, 
    min(cycle_completa.cushionforce) AS min_cushionforce, 
    min(cycle_completa.cushionservopilotpre) AS min_cushionservopilotpre, 
    min(cycle_completa.cushionpistonpre) AS min_cushionpistonpre, 
    min(cycle_completa.cushionringpre) AS min_cushionringpre, 
    min(cycle_completa.pfillaccumpre) AS min_pfillaccumpre, 
    min(cycle_completa.pfillcentralpre) AS min_pfillcentralpre, 
    min(cycle_completa.pfilllateralpre) AS min_pfilllateralpre, 
    min(cycle_completa.pfillunlockingpre) AS min_pfillunlockingpre, 
    min(cycle_completa.reducedauxpumppre) AS min_reducedauxpumppre, 
    min(cycle_completa.tankoiltemp) AS min_tankoiltemp 
FROM cycle_completa 
WHERE slidestage = 17 
GROUP BY cycle_completa.numcycle 

UPDATE:

UPDATE cycle_parametros 
SET seg3_min_valor_fmant_pump1pre = min_pump1pre, 
    seg3_min_valor_fmant_pump2pre = min_pump2pre, 
    seg3_min_valor_fmant_pump3pre = min_pump3pre, 
    seg3_min_valor_fmant_slidevel = min_slidevel, 
    seg3_min_valor_fmant_slidepos = min_slidepos, 
    seg3_min_valor_fmant_slideforce = min_slideforce, 
    seg3_min_valor_fmant_slideservopilotpre = min_slideservopilotpre, 
    seg3_min_valor_fmant_slidecentralpre = min_slidecentralpre, 
    seg3_min_valor_fmant_slidelateralpre = min_slidelateralpre, 
    seg3_min_valor_fmant_slideringpre = min_slideringpre, 
    seg3_min_valor_fmant_slidepistonpre = min_slidepistonpre, 
    seg3_min_valor_fmant_slidey42pre = min_slidey42pre, 
    seg3_min_valor_fmant_cushionforce = min_cushionforce, 
    seg3_min_valor_fmant_cushionservopilotpre = min_cushionservopilotpre, 
    seg3_min_valor_fmant_cushionpistonpre = min_cushionpistonpre, 
    seg3_min_valor_fmant_cushionringpre = min_cushionringpre, 
    seg3_min_valor_fmant_pfillaccumpre = min_pfillaccumpre, 
    seg3_min_valor_fmant_pfillcentralpre = min_pfillcentralpre, 
    seg3_min_valor_fmant_pfilllateralpre = min_pfilllateralpre, 
    seg3_min_valor_fmant_pfillunlockingpre = min_pfillunlockingpre, 
    seg3_min_valor_fmant_reducedauxpumppre = min_reducedauxpumppre, 
    seg3_min_valor_fmant_tankoiltemp = min_tankoiltemp 
WHERE numcycle = actual_cycle 

謝謝!

回答

1

下面的查詢應該工作:

WITH selected as (
    SELECT cycle_completa.numcycle AS actual_cycle, 
     min(cycle_completa.pump1pre) AS min_pump1pre, 
     min(cycle_completa.pump2pre) AS min_pump2pre, 
     min(cycle_completa.pump3pre) AS min_pump3pre, 
     min(cycle_completa.slidevel) AS min_slidevel, 
     min(cycle_completa.slidepos) AS min_slidepos, 
     min(cycle_completa.slideforce) AS min_slideforce, 
     min(cycle_completa.slideservopilotpre) AS min_slideservopilotpre, 
     min(cycle_completa.slidecentralpre) AS min_slidecentralpre, 
     min(cycle_completa.slidelateralpre) AS min_slidelateralpre, 
     min(cycle_completa.slideringpre) AS min_slideringpre, 
     min(cycle_completa.slidepistonpre) AS min_slidepistonpre, 
     min(cycle_completa.slidey42pre) AS min_slidey42pre, 
     min(cycle_completa.cushionforce) AS min_cushionforce, 
     min(cycle_completa.cushionservopilotpre) AS min_cushionservopilotpre, 
     min(cycle_completa.cushionpistonpre) AS min_cushionpistonpre, 
     min(cycle_completa.cushionringpre) AS min_cushionringpre, 
     min(cycle_completa.pfillaccumpre) AS min_pfillaccumpre, 
     min(cycle_completa.pfillcentralpre) AS min_pfillcentralpre, 
     min(cycle_completa.pfilllateralpre) AS min_pfilllateralpre, 
     min(cycle_completa.pfillunlockingpre) AS min_pfillunlockingpre, 
     min(cycle_completa.reducedauxpumppre) AS min_reducedauxpumppre, 
     min(cycle_completa.tankoiltemp) AS min_tankoiltemp 
    FROM cycle_completa 
    WHERE slidestage = 17 
    GROUP BY cycle_completa.numcycle 
) 
UPDATE cycle_parametros cp 
SET cp.seg3_min_valor_fmant_pump1pre = s.min_pump1pre, 
    cp.seg3_min_valor_fmant_pump2pre = s.min_pump2pre, 
    cp.seg3_min_valor_fmant_pump3pre = s.min_pump3pre, 
    cp.seg3_min_valor_fmant_slidevel = s.min_slidevel, 
    cp.seg3_min_valor_fmant_slidepos = s.min_slidepos, 
    cp.seg3_min_valor_fmant_slideforce = s.min_slideforce, 
    cp.seg3_min_valor_fmant_slideservopilotpre = s.min_slideservopilotpre, 
    cp.seg3_min_valor_fmant_slidecentralpre = s.min_slidecentralpre, 
    cp.seg3_min_valor_fmant_slidelateralpre = s.min_slidelateralpre, 
    cp.seg3_min_valor_fmant_slideringpre = s.min_slideringpre, 
    cp.seg3_min_valor_fmant_slidepistonpre = s.min_slidepistonpre, 
    cp.seg3_min_valor_fmant_slidey42pre = s.min_slidey42pre, 
    cp.seg3_min_valor_fmant_cushionforce = s.min_cushionforce, 
    cp.seg3_min_valor_fmant_cushionservopilotpre = s.min_cushionservopilotpre, 
    cp.seg3_min_valor_fmant_cushionpistonpre = s.min_cushionpistonpre, 
    cp.seg3_min_valor_fmant_cushionringpre = s.min_cushionringpre, 
    cp.seg3_min_valor_fmant_pfillaccumpre = s.min_pfillaccumpre, 
    cp.seg3_min_valor_fmant_pfillcentralpre = s.min_pfillcentralpre, 
    cp.seg3_min_valor_fmant_pfilllateralpre = s.min_pfilllateralpre, 
    cp.seg3_min_valor_fmant_pfillunlockingpre = s.min_pfillunlockingpre, 
    cp.seg3_min_valor_fmant_reducedauxpumppre = s.min_reducedauxpumppre, 
    cp.seg3_min_valor_fmant_tankoiltemp = s.min_tankoiltemp 
FROM selected AS s 
WHERE cp.numcycle = s.actual_cycle 
+0

這就是它!我只需要更改UPDATE cycle_parametros cp,而不用「cp」,並且更改所有名稱,因爲PostgreSQL給我一個錯誤 –