使用SQL Server 2008 R2,以下選擇在3秒內使用IN和20分鐘在NOT IN中工作。在SQL Server 2012上執行相同的選擇,IN和NOT IN都需要3秒鐘。有沒有人有過這種情況發生過,如果有的話,是否有不同的查詢可以減少這種延遲?SQL查詢選擇,執行超時
背景:總MessageIndentifier記錄是9000〜。 IN
返回〜1400,NOT IN
返回〜7600。我正在使用SQL Server 2008 R2。
SELECT
AA.[MessageIdentifier]
FROM
(SELECT
CAST(AD.[Contract Number] AS VARCHAR) + '.' +
CAST(AD.[Installation ID] AS VARCHAR) + '.'+
CAST(AD.[System ID] AS VARCHAR) AS MessageIdentifier
FROM
CDCS_FAMS.dbo.SiteParamBaseTable AD) AS AA
WHERE
AA.[MessageIdentifier] IN (SELECT DISTINCT AB.[MessageIdentifier]
FROM
(SELECT cast(AC.[Contract Number] as varchar) + '.' + cast(AC.[Installation ID] as varchar) + '.'+ cast(AC.[System ID] as varchar) AS MessageIdentifier
FROM CDCS_FAMS.dbo.SiteParamBaseTable AC) AS AB,
CDCS_FAMS.dbo.EventTable as DA
WHERE
(DA.[Message] LIKE '%ISO MODE:%'
OR DA.[Message] LIKE '%TEST MODE:%')
AND DA.[Message] LIKE '%Alarm%'
AND DA.[Event time] > DATEADD(DAY,-10, GETDATE())
AND DA.[Message] LIKE '%' + AB.[MessageIdentifier] + '%')
您的幫助將不勝感激。
嘗試使用存在而不是IN /不在 –