系統上線完,性能問題往往是Warranty和後期維護的一個重要問題。
這些天,客戶又來反映, 有一個查詢非常慢。這個查詢用的是主關鍵字查詢,由於主鍵是聚集索引,而且又做了碎片處理。應該是 非常快。但是看到的現象就是很慢(10秒左右,最差有18秒之多)。排除了硬件、資源鎖定等問題,還不 用到達Database端的Tunning級別。基本判斷和SQL文有關,要細看SQL文的執行計劃。
首先把SQL 文找出來
SELECT a.AWB_NO,
a.BWB_NO,
a.CWB_NO,
a.ORIGIN,
a.DEST,
a.MODIFY_ON,
a.CREATED_ON,
a.CONSIGNOR_CUSTOMER_CODE,
a.CONSIGNOR_CODE,
a.CONSIGNOR_NAME,
a.CONSIGNEE_NAME,
a.CWB_STATUS,
a.CWB_TYPE,
ISNULL(a.PCS, 0) AS PCS,
a.BWBLIST,
b.PWEIGHT
FROM TB_CWB AS a
LEFT JOIN TB_CWBWEIGHT AS b
ON a.CWB_NO = b.CWB_NO
AND b.AVAILABLE = 'Y'
WHERE a.AVAILABLE = 'Y'
AND (a.CWB_NO = @CWB_NO OR
(( @CWB_NO IS NULL)
AND (a.AWB_NO = @AWB_NO OR @AWB_NO IS NULL)
AND (a.BWB_NO = @BWB_NO OR @BWB_NO IS NULL)
AND (a.IE_TYPE = @IE_TYPE OR @IE_TYPE IS NULL)
AND (a.CREATED_ON >= @DateFrom OR @DateFrom IS NULL)
AND (a.CREATED_ON <= @DateTo OR @DateTo IS NULL)
AND (a.PAYMENT = @PAYMENT OR @PAYMENT IS NULL)
AND (a.ORIGIN = @ORIGIN OR @ORIGIN IS NULL)
AND (a.DEST = @DEST OR @DEST IS NULL)
AND (a.CONSIGNOR_CUSTOMER_CODE = @CONSIGNOR_CUSTOMER_CODE OR @CONSIGNOR_CUSTOMER_CODE IS NULL)
AND (a.CONSIGNOR_NAME LIKE '%' + @CONSIGNOR_NAME + '% ' OR @CONSIGNOR_NAME IS NULL)
AND (a.CONSIGNEE_NAME LIKE '%' + @CONSIGNEE_NAME + '%' OR @CONSIGNEE_NAME IS NULL)
AND (a.CWB_TYPE = @CWB_TYPE OR @CWB_TYPE IS NULL)))