SQL语句导致超时

时间:2016-04-15 10:53:15

标签: sql sql-server

这是我真正需要修改的代码。我以一种简单的方式做到了,因为我不是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()))
    )

我需要这个尽快工作。

2 个答案:

答案 0 :(得分:1)

This is your query:

SELECT Inv1.AutoIndex, Inv1.DocState, Inv1.OrderNum,      
       Inv1.ExtOrderNum, c.ubARSMS, c.Fax1
FROM dbo.InvNum Inv1 INNER JOIN
     dbo.Client c
     ON Inv1.AccountID = c.DCLink AND Inv1.OrderDate >= c.udARSMSACTDATE
WHERE (c.ubARSMS = 1) 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
                            ) OR  
      (Inv1.InvDate >= CAST(DATEADD(day, -1 GETDATE()) as date) AND
       Inv1.InvDate < CAST(GETDATE() as date) 
      )

This is really two queries, which you can combine using UNION ALL. The first is:

SELECT Inv1.AutoIndex, Inv1.DocState, Inv1.OrderNum,      
       Inv1.ExtOrderNum, c.ubARSMS, c.Fax1
FROM dbo.InvNum Inv1 INNER JOIN
     dbo.Client c
     ON Inv1.AccountID = c.DCLink AND Inv1.OrderDate >= c.udARSMSACTDATE
WHERE (c.ubARSMS = 1) 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
                            )

For this, I would suggest indexes on Client(ubARSMS, DCLink, udARSMACTDate), InvNum(ClientId, OrderNum, AutoIndex), InvNum(OrderNum, OrigDocId), and net_OrderSMSLog(DocState, OrderNum).

For the second query:

SELECT Inv1.AutoIndex, Inv1.DocState, Inv1.OrderNum,      
       Inv1.ExtOrderNum, c.ubARSMS, c.Fax1
FROM dbo.InvNum Inv1 INNER JOIN
     dbo.Client c
     ON Inv1.AccountID = c.DCLink AND Inv1.OrderDate >= c.udARSMSACTDATE
WHERE (Inv1.InvDate >= CAST(DATEADD(day, -1 GETDATE()) as date) AND
       Inv1.InvDate < CAST(GETDATE() as date) 
      )

You want an index on InvNum(InvDate, AccountId, OrderDate) and Client(DCLink, udARSMACTDate).

答案 1 :(得分:0)

由于上面的查询在服务器上的负载很小并且需要提取的记录较少时才起作用。

超时是我需要处理的唯一事情,而无需在客户端主数据库上创建索引。

所以我只创建了第二个Windows服务,检查第一个Windows服务是否因超时而崩溃/停止,然后重新启动它。

解决了我的超时崩溃问题。