MS Access功能需要很长时间才能完成存储过程

时间:2013-12-06 01:11:48

标签: sql sql-server ms-access

create procedure _update_ticket
  @date datetime
as
begin
  update t set
    I_S1_O = (select count(*) from W_Data w where [Priority] = 'S1' and [Type] <> 'R' and [A_Group] <> '1' and [C_Date]< @date and w.[Product_Name] = t.[Product_Name]), 
    I_S1_R = (select count(*) from W_Data w where [Priority] = 'S1' and [Type] <> 'R' and [A_Group] <> '1' and [C_Date]>= @date and w.[Product_Name] = t.[Product_Name]), 
    I_S1_Re = (select count(*) from W_Data w where [Priority] = 'S1' and [Type] <> 'R' and [R_Group] <> '1' and [R_Group] is not NULL and w.[Product_Name] = t.[Product_Name]), 
    .....
  from ATicket t
end

当我使用Access表单中的以下函数执行上述存储过程时,需要很长时间...等待超过半小时。如果我打开传递查询,它将在几秒内更新。请指教

Function AppCount()
Dim cdb As DAO.Database, qdf As DAO.QueryDef
On Error Resume Next
Set cdb = CurrentDb
Set qdf = cdb.QueryDefs("Get_A_Ticket")
qdf.SQL = "EXEC dbo.Update_A_Ticket_Count '" & CDate(Forms!Home!Txt_StDate) & "'"
qdf.Execute
Set qdf = Nothing
Set cdb = Nothing
End Function

1 个答案:

答案 0 :(得分:0)

问题很可能是update语句中的所有子查询。有时,您可以通过创建正确的索引来解决此问题。但在这种情况下,这很难,因为条件中的所有不平等。

相反,您可以预先计算W_Data中的数据,然后将这些数据加入以进行更新:

update t set
    I_S1_O = sumt.I_S1_O, 
    I_S1_R = sumt.I_S1_R,
    . . .
from ATicket t join
     (select Product_Name,
             sum(case when [Priority] = 'S1' and [Type] <> 'R' and [A_Group] <> '1' and [C_Date]< @date
                      then 1 else 0
                 end) I_S1_O,
             sum(case when [Priority] = 'S1' and [Type] <> 'R' and [A_Group] <> '1' and [C_Date]>= @date                          then 1 else 0
                 end) I_S1_R
      from W_Data w
      group by Product_Name
     ) sumt
     on t.Product_Name = sumt.Product_Name