ExecuteReader 比 SSMS 慢得多

Yis*_*ski 5 sql-server ssms sqlconnection executereader

我的问题始于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]
Run Code Online (Sandbox Code Playgroud)

当我运行它时,SSMS它会在 0-1 秒内执行并返回大约 30k 行

同样的精确查询在 .net 中运行ExecuteReader大约 100 秒!

在线研究基本上指出了 2 个解决方案:ARITHABORT 和参数嗅探,所以为了好玩,我将所有这些添加到 SSMS 中

DBCC FREESESSIONCACHE
DBCC FREEPROCCACHE
SET ARITHABORT OFF
Run Code Online (Sandbox Code Playgroud)

还有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
Run Code Online (Sandbox Code Playgroud)

我认为参数嗅探在这里无关紧要,因为我没有发送任何参数

这也不是一个阻塞问题,因为我正在同时测试它们(尽可能多地......),所以如果这是一个时间问题,两者都应该很慢

那么还有什么问题呢?

非常感谢