2012-10-22 46 views
0

我最近繼承了一些有偶爾超時問題的代碼。我大多熟悉ORM,所以我無法確定這段代碼中是否有任何錯誤。當它不超時時,它會在幾秒鐘內運行。暫停需要一分鐘左右。SqlParameter和ExecuteNonQuery導致不可重複的超時

下面是代碼:

Dim sql As String = "UPDATE VendorInfo SET " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected]_Ship, " & _ 
      "[email protected], [email protected], [email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected]_Ship, [email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], [email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [SMP FAX][email protected]_FAX, " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected]_Ship, [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], [email protected], " & _ 
      "[email protected], Vendor[email protected], " & _ 
      "[email protected], [email protected], [email protected], [email protected], " & _ 
      "[email protected]_Ship WHERE [email protected]" 
     Dim p() As SqlParameter = New SqlParameter(74) {} 
     p(0) = MakeSQLParam("@AbsInkUpdateStatus", SqlDbType.VarChar, 50, SS(ht("AbsInkUpdateStatus"))) 
     p(1) = MakeSQLParam("@AccountingNotes", SqlDbType.VarChar, 500, SS(ht("AccountingNotes"))) 
     p(2) = MakeSQLParam("@AccountNumber", SqlDbType.NVarChar, 35, SS(ht("AccountNumber"))) 
     p(3) = MakeSQLParam("@Address_Ship", SqlDbType.NVarChar, 50, SS(ht("Address_Ship"))) 
     p(68) = MakeSQLParam("@Address0", SqlDbType.VarChar, 100, SS(ht("Address0"))) 
     p(4) = MakeSQLParam("@Address1", SqlDbType.NVarChar, 50, SS(ht("Address1"))) 
     p(69) = MakeSQLParam("@Address2", SqlDbType.VarChar, 50, SS(ht("Address2"))) 
     p(5) = MakeSQLParam("@ArtEmail", SqlDbType.NVarChar, 100, SS(ht("ArtEmail"))) 
     p(6) = MakeSQLParam("@ArtRequirements", SqlDbType.NText, SS(ht("ArtRequirements"))) 
     p(7) = MakeSQLParam("@City", SqlDbType.NVarChar, 50, SS(ht("City"))) 
     p(8) = MakeSQLParam("@City_Ship", SqlDbType.NVarChar, 50, SS(ht("City_Ship"))) 
     p(72) = MakeSQLParam("@CreditLimit", SqlDbType.Float, ht("CreditLimit")) 
     If ((Not ht.ContainsKey("EmailPO")) OrElse IsDBNull(ht("EmailPO")) OrElse String.IsNullOrEmpty(ht("EmailPO"))) Then 
      p(73) = MakeSQLParam("@EmailPO", SqlDbType.Bit, DBNull.Value) 
     Else 
      p(73) = MakeSQLParam("@EmailPO", SqlDbType.Bit, Convert.ToBoolean(ht("EmailPO"))) 
     End If 
     If ((Not ht.ContainsKey("SendPO")) OrElse IsDBNull(ht("SendPO")) OrElse String.IsNullOrEmpty(ht("SendPO"))) Then 
      p(74) = MakeSQLParam("@SendPO", SqlDbType.NVarChar, 255, DBNull.Value) 
     Else 
      p(74) = MakeSQLParam("@SendPO", SqlDbType.NVarChar, 255, SS(ht("SendPO"))) 
     End If 
     p(9) = MakeSQLParam("@DisplayName", SqlDbType.NVarChar, 255, SS(ht("DisplayName"))) 
     p(10) = MakeSQLParam("@EarlyBy", SqlDbType.VarChar, 5, SS(ht("EarlyBy"))) 
     p(11) = MakeSQLParam("@EarlyConfirm", SqlDbType.VarChar, 50, SS(ht("EarlyConfirm"))) 
     p(12) = MakeSQLParam("@EarlyHas", SqlDbType.VarChar, 10, SS(ht("EarlyHas"))) 
     p(13) = MakeSQLParam("@EarlyNet", SqlDbType.VarChar, 10, SS(ht("EarlyNet"))) 
     p(14) = MakeSQLParam("@EarlyNotes", SqlDbType.VarChar, 125, SS(ht("EarlyNotes"))) 
     p(15) = MakeSQLParam("@EarlyOn", SqlDbType.DateTime, DtoDB(ht("EarlyOn"))) 
     p(16) = MakeSQLParam("@EarlyPercent", SqlDbType.Decimal, DBS(ht("EarlyPercent"), True)) 
     p(17) = MakeSQLParam("@Email", SqlDbType.NVarChar, 75, SS(ht("Email"))) 
     p(18) = MakeSQLParam("@Fax", SqlDbType.NVarChar, 50, SS(ht("Fax"))) 
     p(19) = MakeSQLParam("@ID", SqlDbType.Int, ht("ID")) 
     p(20) = MakeSQLParam("@PContactName", SqlDbType.NVarChar, 50, SS(ht("PContactName"))) 
     p(21) = MakeSQLParam("@Phone", SqlDbType.NVarChar, 50, SS(ht("Phone"))) 
     p(22) = MakeSQLParam("@Pricing", SqlDbType.NVarChar, 255, SS(ht("Pricing"))) 
     p(23) = MakeSQLParam("@PromosBy", SqlDbType.VarChar, 5, SS(ht("PromosBy"))) 
     p(24) = MakeSQLParam("@PromosConfirm", SqlDbType.VarChar, 50, SS(ht("PromosConfirm"))) 
     p(25) = MakeSQLParam("@PromosHas", SqlDbType.VarChar, 10, SS(ht("PromosHas"))) 
     p(26) = MakeSQLParam("@PromosNotes", SqlDbType.VarChar, 125, SS(ht("PromosNotes"))) 
     p(27) = MakeSQLParam("@PromosOn", SqlDbType.DateTime, DtoDB(ht("PromosOn"))) 
     p(70) = MakeSQLParam("@QBName", SqlDbType.VarChar, 41, SS(ht("QBName"))) 
     p(71) = MakeSQLParam("@QBTerms", SqlDbType.VarChar, 31, SS(ht("QBTerms"))) 
     p(28) = MakeSQLParam("@RebateBy", SqlDbType.VarChar, 5, SS(ht("RebateBy"))) 
     p(29) = MakeSQLParam("@RebateConfirm", SqlDbType.VarChar, 50, SS(ht("RebateConfirm"))) 
     p(30) = MakeSQLParam("@RebateHas", SqlDbType.VarChar, 10, SS(ht("RebateHas"))) 
     p(31) = MakeSQLParam("@RebateNotes", SqlDbType.VarChar, 125, SS(ht("RebateNotes"))) 
     p(32) = MakeSQLParam("@RebateOn", SqlDbType.DateTime, DtoDB(ht("RebateOn"))) 
     p(33) = MakeSQLParam("@RushPolicy", SqlDbType.NVarChar, 200, SS(ht("RushPolicy"))) 
     p(34) = MakeSQLParam("@SampleBy", SqlDbType.VarChar, 5, SS(ht("SampleBy"))) 
     p(35) = MakeSQLParam("@SampleConfirm", SqlDbType.VarChar, 50, SS(ht("SampleConfirm"))) 
     p(36) = MakeSQLParam("@SampleHas", SqlDbType.VarChar, 10, SS(ht("SampleHas"))) 
     p(37) = MakeSQLParam("@SampleNotes", SqlDbType.VarChar, 125, SS(ht("SampleNotes"))) 
     p(38) = MakeSQLParam("@SampleOn", SqlDbType.DateTime, DtoDB(ht("SampleOn"))) 
     p(39) = MakeSQLParam("@SamplePolicy", SqlDbType.NVarChar, 255, SS(ht("SamplePolicy"))) 
     p(40) = MakeSQLParam("@ShippingBy", SqlDbType.VarChar, 5, SS(ht("ShippingBy"))) 
     p(41) = MakeSQLParam("@ShippingConfirm", SqlDbType.VarChar, 50, SS(ht("ShippingConfirm"))) 
     p(42) = MakeSQLParam("@ShippingHas", SqlDbType.VarChar, 10, SS(ht("ShippingHas"))) 
     p(43) = MakeSQLParam("@ShippingNotes", SqlDbType.VarChar, 125, SS(ht("ShippingNotes"))) 
     p(44) = MakeSQLParam("@ShippingOn", SqlDbType.DateTime, DtoDB(ht("ShippingOn"))) 
     p(45) = MakeSQLParam("@ShipTo", SqlDbType.NVarChar, 100, SS(ht("ShipTo"))) 
     p(46) = MakeSQLParam("@SMP_FAX", SqlDbType.NVarChar, 50, SS(ht("SMP FAX"))) 
     p(47) = MakeSQLParam("@SpecialBy", SqlDbType.VarChar, 5, SS(ht("SpecialBy"))) 
     p(48) = MakeSQLParam("@SpecialConfirm", SqlDbType.VarChar, 50, SS(ht("SpecialConfirm"))) 
     p(49) = MakeSQLParam("@SpecialHas", SqlDbType.VarChar, 10, SS(ht("SpecialHas"))) 
     p(50) = MakeSQLParam("@SpecialInstructions", SqlDbType.Text, SS(ht("SpecialInstructions"))) 
     p(51) = MakeSQLParam("@SpecialNotes", SqlDbType.VarChar, 255, SS(ht("SpecialNotes"))) 
     p(52) = MakeSQLParam("@SpecialOn", SqlDbType.DateTime, DtoDB(ht("SpecialOn"))) 
     p(53) = MakeSQLParam("@SpecialPricingShort", SqlDbType.VarChar, 80, SS(ht("SpecialPricingShort"))) 
     p(54) = MakeSQLParam("@State", SqlDbType.NVarChar, 25, SS(ht("State"))) 
     p(55) = MakeSQLParam("@State_Ship", SqlDbType.NVarChar, 25, SS(ht("State_Ship"))) 
     p(56) = MakeSQLParam("@TermsBy", SqlDbType.VarChar, 5, SS(ht("TermsBy"))) 
     p(57) = MakeSQLParam("@TermsConfirm", SqlDbType.VarChar, 50, SS(ht("TermsConfirm"))) 
     p(58) = MakeSQLParam("@TermsHas", SqlDbType.VarChar, 10, SS(ht("TermsHas"))) 
     p(59) = MakeSQLParam("@TermsNotes", SqlDbType.VarChar, 125, SS(ht("TermsNotes"))) 
     p(60) = MakeSQLParam("@TermsOn", SqlDbType.DateTime, DtoDB(ht("TermsOn"))) 
     p(61) = MakeSQLParam("@TypicalShippingCost", SqlDbType.NText, SS(ht("TypicalShippingCost"))) 
     p(62) = MakeSQLParam("@UpdateStatus", SqlDbType.VarChar, 50, SS(ht("UpdateStatus"))) 
     p(63) = MakeSQLParam("@VendorName", SqlDbType.NVarChar, 100, SS(ht("VendorName"))) 
     p(64) = MakeSQLParam("@VendorStrength", SqlDbType.NVarChar, 100, SS(ht("VendorStrength"))) 
     p(65) = MakeSQLParam("@WebPage", SqlDbType.NVarChar, 100, SS(ht("WebPage"))) 
     p(66) = MakeSQLParam("@ZipCode", SqlDbType.NVarChar, 15, SS(ht("ZipCode"))) 
     p(67) = MakeSQLParam("@ZipCode_Ship", SqlDbType.NVarChar, 15, SS(ht("ZipCode_Ship"))) 

     Dim con As SqlConnection = Nothing 
     Try 
      con = New SqlConnection(ConfigurationManager.AppSettings("LeadsConnectionString")) 
      SqlHelper.ExecuteNonQuery(con, CommandType.Text, sql, p) 
     Catch ex As Exception 
      Throw New Exception(ex.Message & " UPDATE failed for 'VendorInfo' " & ht.TableAsString, ex) 
     Finally 
      If (Not con Is Nothing) Then con.Close() 
     End Try 

SS是「安全弦」功能和MakeSQLParam設置方向input,並確保大小值不受侵犯。

所以我的問題是:有沒有人看到這個代碼有可能導致呆滯的錯誤?或者是否有人知道我可能想要檢查的其他事情,以幫助解決這個問題?

謝謝!

+0

對此表是否有其他交易正在進行? –

+1

也許表'VendorInfo'有時被鎖定。這是一個Web應用程序或另一種多線程應用程序? –

+0

@TimSchmelter它是一個Web應用程序。這是可能的。我會研究這一點。 – PFranchise

回答

1

這可能是由於參數嗅探導致不適當的查詢計劃被緩存。

有了這麼多參數,您可能必須每次重新編譯查詢。

首先請嘗試重建索引,並確保統計數據是最新的:(注意在生產環境中......)

exec sp_msforeachtable "DBCC DBREINDEX('?')" 
go 

exec sp_msforeachtable "UPDATE STATISTICS ? WITH FULLSCAN, COLUMNS" 
go 

然後,檢查運行的快和慢的情況下,查詢計劃SSMS中的SQL。如果沒有吸菸槍,則檢查其他可能持有鎖的流程。

[有很多相關問題已經在StackOverflow上得到解答。]

+0

這很有道理。每次重新編譯對性能有多大影響?該查詢每小時運行兩次,性能不是主要問題。 – PFranchise

+1

這取決於... :)幾個因素。但對於每小時運行兩次的查詢,這不是一個問題。 –

+0

哈哈「這取決於」似乎永遠是comp sci問題的正確答案。謝謝(你的)信息。我已經學會了相當數量,並且剛剛開始解決這個問題。祝你有個好的一天! – PFranchise

0

這是針對SQL Server運行的嗎?超時時你可能正在陷入僵局。您會希望監控在遇到超時時正在運行的事務,和/或嘗試在Management Studio中手動運行該語句,並查看是否遇到死鎖。

如果您有任何正在運行的代理程序作業訪問相同的表或可能訪問相同表的其他進程,那可能是您的問題。