2013-10-19 101 views
2

我已用於選擇一些數據的查詢時,我通過一些參數在它:SQL Server的慢結果

DECLARE @FromAccDocNo  INT = 1, 
     @ToAccDocNo  INT = 999999999, 
     @FromDate   CHAR(10) = '1900/01/01', 
     @ToDate   CHAR(10) = '2999/12/30', 
     @L1Code   INT = 129 

SELECT ad.AccDocNo, 
     ad.AccDocDate, 
     add1.Row, 
     add1.RowComment, 
     add1.Debit, 
     add1.Credit 
FROM AccDoc ad 
     INNER JOIN AccDocDetail add1 
      ON add1.AccDocNo = ad.AccDocNo 
     INNER JOIN Topic t 
      ON t.TopicCode = add1.TopicCode 
WHERE t.L1Code = @L1Code -- here is the difference 
     AND add1.AccDocNo BETWEEN @FromAccDocNo AND @ToAccDocNo 
     AND ad.EffectiveDate BETWEEN @FromDate AND @ToDate 
ORDER BY 
     ad.AccDocNo 

在第一,我寫入值129顯式地作爲@L1Code(它需要0.010秒)

在第二種形式我通過@L1Code進入查詢(它需要2.500秒)

任何人都可以解釋發生了什麼?

+1

它們是變量而不是參數。兩者的行爲不同。參數值可以被「嗅探」。除非使用'OPTION(RECOMPILE)',否則變量值永遠不會被使用 –

回答