8

主要问题 是否存在一些已知的限制、snafu、配置问题等等,这可以解释所有事情都是平等的,从 C# linq 运行的查询可能需要比以任何其他方式运行时长一个数量级的时间才能完成?

这是 linq 中的缩写查询。这是视图和表之间非常直接的连接。

var query = (
    from content in context.ApprovedContentView
                where content.BucketId == 13098 && content.ContentTypeId == 5220 
    join item in context.ActiveContent
                on content.ContentId equals item.ItemId
        where
                item.IsSuchAndSuch == true && item.SomeOtherProperty == 5000 
        select new 
        {
            ItemId = item.ItemId,
            Title = item.Title,
            SubTitle = item.SubTitle,
            DescriptionText = item.DescriptionText,
            /* about 10 other scalar fields */

        });

int count = query.Count();
var data = query.OrderByDescending(item => item.ItemId).Skip(5).Take(3);

这是它生成的(缩写/格式化)SQL

SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    COUNT(1) AS [A1]
    FROM       [SchemaX].[ApprovedContentView] AS [Extent1]
    INNER JOIN [SchemaX].[ActiveContent] AS [Extent2] ON [Extent1].[ContentId] = [Extent2].[ItemId]
    WHERE (13098 = [Extent1].[BucketId]) AND (5220 = [Extent1].[ContentTypeId ]) AND 
    (1 = [Extent2].[IsSuchAndSuch]) AND (5000 = [Extent2].[SomeOtherProperty ])
)  AS [GroupBy1]
GO

SELECT TOP (3) 
[Filter1].[BucketId] AS [BucketId], 
[Filter1].[ItemId] AS [ItemId], 
[Filter1].[Title] AS [Title], 
[Filter1].[SubTitle] AS [SubTitle], 
[Filter1].[DescriptionText] AS [DescriptionText], 
/* other fields */
FROM ( SELECT 
            [Extent1].[BucketId] AS [BucketId], 
            [Extent2].[ItemId] AS [ItemId], 
            [Extent2].[Title] AS [Title], 
            [Extent2].[SubTitle] AS [SubTitle], 
            [Extent2].[DescriptionText] AS [DescriptionText], 
            /* other fields */
            row_number() OVER (ORDER BY [Extent2].[DealId] DESC) AS [row_number]
    FROM  [SchemaX].[ApprovedContentView] AS [Extent1]
    INNER JOIN [SchemaX].[ActiveContent] AS [Extent2] ON [Extent1].[ContentId] = [Extent2].[ItemId]
    WHERE (13098 = [Extent1].[BucketId]) AND (5220 = [Extent1].[ContentTypeId ]) AND 
            (1 = [Extent2].[IsSuchAndSuch]) AND (5000 = [Extent2].[SomeOtherProperty ])
)  AS [Filter1]
WHERE [Filter1].[row_number] > 5
ORDER BY [Filter1].[DealId] DESC

不同的场景我的速度测试基于观察使用 sql profiler 运行的查询

当这个linq 查询在我的 c# 应用程序中作为其正常操作的一部分执行时,我观察到在 sql profiler 中,选择计数需要整整 3 秒才能完成,奇怪的是,产生投影的查询只需要 200 毫秒,并且时间是可重复的,这似乎排除了查询执行计划缓存问题。(使用实体框架 5、sql server 2008 r2 运行)

在 LINQPAD 中 ,当我使用 C# 应用程序的 dll 的数据上下文通过 LinqPad 执行 linq 语句时,计数和投影均在四分之一秒内完成(约 224 毫秒,总运行时间约 450 毫秒)。

在 SSMS 中,无论 sql 的来源如何,当我复制 sql 配置文件报告它执行并将其粘贴到管理工作室窗口并执行的实际代码时,大约需要 224 毫秒。

数据库调整 在 SSMS 中,当我评估从分析器(从代码或从 linqpad)复制的 sql 的实际执行计划时,我发现 sql 正在使用所有正确的索引,并且只报告索引搜索——没有表扫描,没有摆脱查找。

那么,什么给了?有人见过这样的吗?

4

2 回答 2

8

我会确保您没有为您的应用程序缓存的错误执行计划。在对已使用的数据库进行架构工作时,我经常会遇到这种情况。您可能有一个已为应用程序执行上下文缓存的执行计划,该执行计划由于架构更改而效率低下,而为您的 SSMS 查询生成的执行计划是最新的,并且没有看到这些性能问题。

我会尝试使用DBCC FREEPROCCACHE来强制更新您的执行计划,看看是否能解决问题。

于 2013-03-11T16:44:41.487 回答
2

ARITHABORT在 SSMS 中默认为 ON,在 SqlClient 连接中默认为 OFF。

如果问题再次出现,请添加:

new SqlCommand("SET ARITHABORT ON", connection).ExecuteNonQuery();
于 2013-04-15T16:24:19.247 回答