Coldfusion SQL查询非常慢

时间:2014-01-20 10:18:09

标签: mysql sql-server coldfusion

我们有一些coldfusion查询运行并比较来自两个数据库的数据,一个MS SQL和一个MYSQL来执行从我们的instore数据库更新网上商店价格的任务。

问题是此查询需要花费四分钟才能完成。我们可以简化此查询以加快速度,或者我们是否面临来自主机的限制?

查询 - :

<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>

1 个答案:

答案 0 :(得分: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>

然后,您可以循环浏览这些结果并执行您需要执行的操作。这只是一次数据库之旅。