我的问题开始于Entity Framework
查询的执行非常缓慢(〜2分钟)。所以我开始调查
与此同时,该问题似乎也在标准SqlConnection
我有一个非常简单的查询
SELECT 1 AS [C1], [Extent1].[OldReleaseID] AS [OldReleaseID], [Extent1].[ProductName] AS [ProductName], [Extent1].[Price] AS [Price], [Extent1].[DiscountAmount] AS [DiscountAmount], [Extent1].[DiscountRate] AS [DiscountRate], [Extent1].[AbsorbVat] AS [AbsorbVat], [Extent1].[SerialCode] AS [SerialCode], [Extent1].[BrandName] AS [BrandName] FROM [dbo].[LocalSaleProductExts]() AS [Extent1]
当我在SSMS
中运行它时,它会在0-1秒内执行并返回约3万行
同一精确查询在.net ExecuteReader
中运行大约100秒!
在线研究基本上指出了两种解决方案:ARITHABORT和参数嗅探,因此,对于踢球,我在SSMS中添加了所有这些解决方案
DBCC FREESESSIONCACHE
DBCC FREEPROCCACHE
SET ARITHABORT OFF
仍然0-1秒。
然后在我的代码中添加了SET ARITHABORT ON
。这是我的简单代码
Using sc = New SqlClient.SqlConnection("data source=MYHOST;initial catalog=MYDB;persist security info=True;user id=MYUSER;password=MYPASS;MultipleActiveResultSets=True;")
Dim txt = "SELECT 1 AS [C1], [Extent1].[OldReleaseID] AS [OldReleaseID], [Extent1].[ProductName] AS [ProductName], [Extent1].[Price] AS [Price], [Extent1].[DiscountAmount] AS [DiscountAmount], [Extent1].[DiscountRate] AS [DiscountRate], [Extent1].[AbsorbVat] AS [AbsorbVat], [Extent1].[SerialCode] AS [SerialCode], [Extent1].[BrandName] AS [BrandName] FROM [dbo].[LocalSaleProductExts]() AS [Extent1] OPTION (RECOMPILE)"
sc.Execute("SET ARITHABORT ON")
sc.TryOpen()
Dim ret As List(Of Common.DbDataRecord)
Using cmd = sc.CreateCommand(txt)
Using dr = cmd.ExecuteReader
ret = dr.Cast(Of Common.DbDataRecord).ToList
dr.Close()
End Using
End Using
Dim a = ret
End Using
我认为参数嗅探在这里是无关紧要的,因为我没有发送任何参数
这也不是一个阻碍性的问题,因为我正在同时测试它们(尽可能多地...),因此,如果这是一个计时问题,两者都应该很慢
那问题又是什么呢?
非常感谢