2014-10-09 60 views
0

我有一個關於從兩天前獲取製造數據的問題。我的代碼工作,但我認爲它對我的實現效率低下。我有這個,獲取最近2天的生產數據

// YESTERDAYS FABRICATION 
      $yesterdaySql = "SELECT SUM(CURRENT_WEIGHT) AS TOTALYESTERDAYWEIGHT FROM FABRICATION_HIST 
           WHERE TO_CHAR (FAB_ENTRY_DATE,'MM/DD/YYYY') = TO_CHAR(SYSDATE - 1, 'MM/DD/YYYY') 
           AND PROJECT_NAME = :projName"; 
      $yesterdayParse = oci_parse($conn, $yesterdaySql); 
      oci_bind_by_name($yesterdayParse, ":projName", $_SESSION['cd-dropdown']);    
      oci_define_by_name($yesterdayParse, "TOTALYESTERDAYWEIGHT", $sumWeightYesterday); 
      oci_execute($yesterdayParse); 

      // TWO DAYS AGO FABRICATION 
      $twoDaysSql = "SELECT SUM(CURRENT_WEIGHT) AS TOTALTWODAYSAGOWEIGHT FROM FABRICATION_HIST 
           WHERE TO_CHAR (FAB_ENTRY_DATE,'MM/DD/YYYY') = TO_CHAR(SYSDATE - 2, 'MM/DD/YYYY') 
           AND PROJECT_NAME = :projName"; 
      $twoDaysParse = oci_parse($conn, $twoDaysSql); 
      oci_bind_by_name($twoDaysParse, ":projName", $_SESSION['cd-dropdown']);    
      oci_define_by_name($twoDaysParse, "TOTALTWODAYSAGOWEIGHT", $sumWeightTwoDays); 
      oci_execute($twoDaysParse); 

我知道這是非常低效的。任何人都可以建議我,這樣我可以在單個查詢中獲取兩天查詢?

在此先感謝

+0

通行證參數,然後只是比較'FAB_ENTRY_DATE'。您也只需要一個參數化查詢。 – fejese 2014-10-09 10:26:23

+0

如果存在性能問題,它最有可能在數據庫本身。一個單一的查詢而不是兩個在這裏不會有太大的影響(在我看來)。您最好要求調整數據庫查詢本身。爲此,我建議您在這裏發佈查詢的執行計劃。 – nop77svk 2014-10-09 10:28:31

回答

0

在這裏,你是...

SELECT 
    SUM(CASE WHEN TRUNC(FAB_ENTRY_DATE) = TRUNC(SYSDATE) - 1 THEN CURRENT_WEIGHT END) AS TOTALYESTERDAYWEIGHT, 
    SUM(CASE WHEN TRUNC(FAB_ENTRY_DATE) = TRUNC(SYSDATE) - 2 THEN CURRENT_WEIGHT END) AS TOTALTWODAYSAGOWEIGHT 
FROM FABRICATION_HIST 
WHERE FAB_ENTRY_DATE >= TRUNC(SYSDATE) - 2 
    AND FAB_ENTRY_DATE < TRUNC(SYSDATE) 
    AND PROJECT_NAME = :projName 
; 

...它通過甚至良好的可轉位:從PHP的時限爲查詢

create index i_fabrication_hist_something 
on t_fabrication_hist (project_name, fab_entry_date) 
compress 1 
compute statistics 
; 
+0

謝謝@ nop77svk。如果今天或明天是星期一,如何跳過星期日? – 2014-10-10 02:32:21