這是我真正需要修改的代碼。我做了一個簡單的方法,因爲我不是SQL中的專家。導致超時的SQL語句
SELECT Inv1.AutoIndex, Inv1.DocState, Inv1.OrderNum,
Inv1.ExtOrderNum, dbo.Client.ubARSMS, dbo.Client.Fax1
FROM dbo.InvNum Inv1 INNER JOIN
dbo.Client ON Inv1.AccountID = dbo.Client.DCLink
WHERE (dbo.Client.ubARSMS = 1)
AND (Inv1.OrderDate >= dbo.Client.udARSMSACTDATE)
AND Inv1.OrderNum NOT IN (SELECT o.OrderNum
FROM dbo.net_OrderSMSLog o
WHERE (o.DocState = 4))
AND Inv1.AutoIndex NOT IN(SELECT Inv2.OrigDocID
FROM dbo.InvNum Inv2
WHERE Inv2.OrderNum = Inv1.OrderNum)
AND
(
DATEPART(YEAR, Inv1.InvDate) = DATEPART(YEAR, GETDATE())
AND DATEPART(MONTH, Inv1.InvDate) = DATEPART(MONTH, GETDATE())
AND DATEPART(DAY, Inv1.InvDate) = DATEPART(DAY, GETDATE())
OR
DATEPART(YEAR, Inv1.InvDate) = DATEPART(YEAR,DATEADD(dd,-1,GETDATE()))
AND DATEPART(MONTH, Inv1.InvDate) = DATEPART(MONTH,DATEADD(dd,-1,GETDATE()))
AND DATEPART(DAY, Inv1.InvDate) = DATEPART(DAY,DATEADD(dd,-1,GETDATE()))
)
我需要這個工作儘可能快。
表定義,包括約束和索引。 – jarlh
您的「OR」邏輯是否正確? –
或者邏輯正確,這段代碼正確地提取記錄,它只是經常超時 – CypherNet