2014-01-20 213 views
0

我們有幾個coldfusion查詢,它們運行並比較來自兩個數據庫,一個MS SQL和一個MYSQL的數據,以執行更新我們店內數據庫的價格的任務。Coldfusion SQL查詢速度很慢

問題是這個查詢需要四分鐘才能完成。我們可以簡化這個查詢來加速它,或者我們面臨來自主機的限制嗎?

查詢 - :

<cfsetting requestTimeOut = "1440"> 

<cftry> 
    <cfset request.timing = structNew()> 

    <cfset getOffline = getTickCount()> 
    <cfquery datasource="MSSQLDATABSE" name="get_offlineproduct2" timeout="240"> 
     SELECT p.id, p.code, pl.salesPrice3 
     FROM products p with(nolock) 
      INNER JOIN productposlocations pl with(nolock) 
       ON pl.ProductID = p.ID 
     WHERE pl.posLocationID = 1 
    </cfquery> 
    <cfset request.timing.getOffline = (getTickCount() - getOffline)/1000> 

<cfquery datasource="MYSQLDATABASE" name="get_onlineproducts" timeout="240"> 
     SELECT s.SKU_ID, s.SKU_MerchSKUID, s.SKU_Price, s.SKU_ShowWeb, b.brand_Name 
     FROM tbl_skus AS s 
     INNER JOIN tbl_prdtbrand_rel r 
     ON s.SKU_ProductID = r.prdt_brand_rel_Product_ID 
     INNER JOIN tbl_prdtbrands b 
     ON r.prdt_brand_rel_Brand_ID = b.brand_ID 
     ORDER BY b.brand_Name 
    </cfquery> 

    <cfquery name="qPriceCheck" dbtype="query"> 
     SELECT * 
     FROM get_onlineproducts, get_offlineproduct2 
     WHERE SKU_MerchSKUID = code 
      AND SKU_Price <> salesPrice3 
    </cfquery> 

    <table border="1" cellspacing="2" cellpadding="2"> 
     <tr> 
      <th>SKU ID</th> 
      <th>Brand</th> 
      <th>SKU Merchant ID</th> 
      <th>SKU Price</th> 
      <th>Stock Price</th> 
      <th>Price Updated?</th> 
     </tr> 
     <cfoutput query="get_onlineproducts"> 
      <cfquery name="qPC" dbtype="query"> 
       SELECT * 
       FROM get_offlineproduct2 
       WHERE code = '#get_onlineproducts.SKU_MerchSKUID#' 
      </cfquery> 
      <tr> 
       <td>#get_onlineproducts.SKU_ID#</td> 
       <td>#get_onlineproducts.brand_Name#</td> 
       <td>#get_onlineproducts.SKU_MerchSKUID#</td> 
       <td>#get_onlineproducts.SKU_Price#</td> 
       <cfif qPC.recordCount> 
        <td>#qPC.salesPrice3#</td> 
        <cfif qPC.salesPrice3 neq get_onlineproducts.SKU_Price> 
         <td>Yes</td> 
         <cfquery datasource="MYSQLDATABASE" name="UpdateonlineproductsPrices"> 
          UPDATE tbl_skus 
          SET SKU_Price = '#qPC.SalesPrice3#' 
          WHERE SKU_MerchSKUID = '#get_onlineproducts.SKU_MerchSKUID#' 
         </cfquery> 
        </cfif> 
       </cfif> 
      </tr> 
     </cfoutput> 
    </table> 

    <cfset request.timing.totalTime = (getTickCount() - getOffline)/1000> 
    <cfdump var="#request.timing#"> 

<cfcatch type="Any" > 
    <cfdump var="#cfcatch#" label="cfcatch"> 
</cfcatch> 
</cftry> 
+0

爲什麼要發佈所有代碼.. – Mihai

+0

我認爲這可能是有益的,對不起 – matthew

+0

發佈查詢並解釋計劃 – Mihai

回答

2

至於評論,「你會如何建議刪除從CFOUTPUT查詢我不知道要做到這一點的最好辦法」,我的建議是:

<cfquery name="qPC" dbtype="query"> 
SELECT field1, field2, etc 
FROM get_offlineproduct2 
WHERE code in (<cfqueryparam 
value = #ValueList(get_onlineproducts.SKU_MerchSKUID)# list="yes") 
</cfquery> 

然後,您可以遍歷這些結果並執行您需要執行的操作。這只是一次數據庫訪問。