Glimpse HUD中数据库查询计数之间的差异& SQL选项卡

时间:2015-02-02 21:52:58

标签: asp.net-mvc-3 glimpse

这个问题涉及Glimpse.MVC3和Glimpse.EF5包。我正在尝试在旧的MVC3站点上调试性能问题。根据Glimpse的 HUD ,特定的GET请求在 28ms 总计的 12 查询 - 但是当我展开以打开主面板,然后点击 SQL标签,它表示 10.41ms 总计只有 6 查询。顶部的计数都是6,当我计算它列出的查询时,有6个。当我看到编写的代码时,这也是有意义的。 (无论哪种方式,我都可以看到太多是加载延迟而且需要修复。)

没有来自Glimpse的指标,相同的6个查询被执行了两次(当我看到HUD显示的面板数量是面板的两倍时,我的大脑就在这里)。

此外,HUD显示 0 Ajax请求,但历史记录部分实际显示 1 (这绝对是准确的)。

为什么会出现差异? (请记住,我更关心与查询的差异。)



编辑 - 根据要求从文件中获取JSON:

glimpse_sql:
{
  data:
  {
    "SQL Statistics":
    [
      {
        connectionCount: 6
        queryCount: 6
        transactionCount: 0
        queryExecutionTime: 6.91
        connectionOpenTime: 116.08
      }
    ]
    Queries:
    [
      [
        Commands per Connection
        Duration
      ]

hud:
  {
    sql:
    {
      data:
      {
        queryCount: 12
        connectionCount: 12
        transactionCount: 0
        queryExecutionTime: 41.87
        connectionOpenTime: 242.96
      }
      name: sql
    }

编辑2 - 查询

"Queries":[["Commands per Connection","Duration"],

[[["Transaction Start","Ordinal","Command","Parameters","Records","Duration","Offset","Async","Transaction End","Errors"],[null,"1","SELECT TOP (2) \r\n[Extent1].[TrxnID] AS [TrxnID], \r\n[Extent1].[StartTime] AS [StartTime], \r\n[Extent1].[Lane] AS [Lane], \r\n[Extent1].[EmployeeID] AS [EmployeeID], \r\n[Extent1].[OptionsCompleted] AS [OptionsCompleted], \r\n[Extent1].[StoreID] AS [StoreID], \r\n[Extent1].[NoVehicleTireCheck] AS [NoVehicleTireCheck], \r\nFROM [Activity].[Trxn] AS [Extent1]\r\nWHERE [Extent1].[TrxnID] = 353 /* @p__linq__0 */",[["Name","Value","Type","Size"],["@p__linq__0",353,"Int32",0]],1,1.12,76.67,false,null,null,""]],5.85],

[[["Transaction Start","Ordinal","Command","Parameters","Records","Duration","Offset","Async","Transaction End","Errors"],[null,"1","SELECT \r\n[Extent1].[TrxnID] AS [TrxnID], \r\n[Extent1].[CustomerID] AS [CustomerID], \r\n[Extent1].[FirstName] AS [FirstName], \r\n[Extent1].[LastName] AS [LastName], \r\n[Extent1].[RewardAccountID] AS [RewardAccountID], \r\n[Extent1].[CustomerEmail] AS [CustomerEmail], \r\n[Extent1].[HomePhone] AS [HomePhone]\r\nFROM [Activity].[Trxn_Customers] AS [Extent1]\r\nWHERE [Extent1].[TrxnID] = 353 /* @EntityKeyValue1 */",[["Name","Value","Type","Size"],["@EntityKeyValue1",353,"Int32",0]],0,1.18,102.7,false,null,null,""]],21.7],

[[["Transaction Start","Ordinal","Command","Parameters","Records","Duration","Offset","Async","Transaction End","Errors"],[null,"1","SELECT \r\n[Extent1].[TrxnID] AS [TrxnID], \r\n[Extent1].[VehicleID] AS [VehicleID], \r\n[Extent1].[VehicleVIN] AS [VehicleVIN], \r\n[Extent1].[VehicleOdometer] AS [VehicleOdometer], \r\n[Extent1].[VehicleEngineID] AS [VehicleEngineID], \r\n[Extent1].[VehicleMakeID] AS [VehicleMakeID], \r\n[Extent1].[ModelYear] AS [ModelYear]\r\nFROM [Activity].[Trxn_Vehicles] AS [Extent1]\r\nWHERE [Extent1].[TrxnID] = 353 /* @EntityKeyValue1 */",[["Name","Value","Type","Size"],["@EntityKeyValue1",353,"Int32",0]],0,1.26,2301.56,false,null,null,""]],27.72],

[[["Transaction Start","Ordinal","Command","Parameters","Records","Duration","Offset","Async","Transaction End","Errors"],[null,"1","SELECT \r\n[Extent1].[TrxnID] AS [TrxnID], \r\n[Extent1].[SecondaryVehicleID] AS [SecondaryVehicleID], \r\n[Extent1].[SecondaryVehicleVIN] AS [SecondaryVehicleVIN], \r\n[Extent1].[SecondaryVehicleTypeID] AS [SecondaryVehicleTypeID], \r\n FROM [Activity].[Trxn_SecondaryVehicles] AS [Extent1]\r\n WHERE [Extent1].[TrxnID] = 353 /* @EntityKeyValue1 */",[["Name","Value","Type","Size"],["@EntityKeyValue1",353,"Int32",0]],0,1.15,2325.95,false,null,null,""]],23.15],

[[["Transaction Start","Ordinal","Command","Parameters","Records","Duration","Offset","Async","Transaction End","Errors"],[null,"1","SELECT \r\n[Extent1].[TrxnServiceID] AS [TrxnServiceID], \r\n[Extent1].[TrxnID] AS [TrxnID], \r\n[Extent1].[PackageID] AS [PackageID], \r\n[Extent1].[PartID] AS [PartID], \r\n[Extent1].[Qty] AS [Qty]\r\nFROM [Activity].[Trxn_Services] AS [Extent1]\r\nWHERE [Extent1].[TrxnID] = 353 /* @EntityKeyValue1 */",[["Name","Value","Type","Size"],["@EntityKeyValue1",353,"Int32",0]],0,1.02,2342.92,false,null,null,""]],15.74],

[[["Transaction Start","Ordinal","Command","Parameters","Records","Duration","Offset","Async","Transaction End","Errors"],[null,"1","SELECT \r\n[Extent1].[TrxnNoteID] AS [TrxnNoteID], \r\n[Extent1].[TrxnID] AS [TrxnID], \r\n[Extent1].[NoteText] AS [NoteText], \r\n[Extent1].[NoteNumber] AS [NoteNumber], \r\n[Extent1].[SendToInvoice] AS [SendToInvoice]\r\nFROM [Activity].[Trxn_Notes] AS [Extent1]\r\nWHERE [Extent1].[TrxnID] = 353 /* @EntityKeyValue1 */",[["Name","Value","Type","Size"],["@EntityKeyValue1",353,"Int32",0]],0,1.19,4689.34,false,null,null,""]],21.92]

]},"name":"SQL"}

1 个答案:

答案 0 :(得分:5)

好吧,我明白了。但首先,请看一下为解决问题需要采用的确切使用方案。

使用场景
只有在以下情况下才会出现此问题:
一个。为上下文启用了延迟加载。
湾直接将您的EF模型传递给视图,不要使用viewmodel(这不是好的做法无论如何,但这是遗留代码所做的事情)。

我学到了什么
1)事实证明,HUD显示的更高的查询数是正确的 - SQL Server Profiler揭示了这一点。

2)Glimpse本身似乎在触发额外的查询。当我打开元数据选项卡时,它似乎以某种方式访问​​导航属性,导致延迟加载。如果我关闭Glimpse,或者打开Glimpse但禁用Metadata选项卡,则不会发生额外的查询。

解释
我花了一些时间来解决这个问题,因为我搜索了代码并构建了一个测试场景来隔离问题。在其中,我检索了一条记录,之后没有触及任何导航属性。单步执行它,我确信在我的代码中没有导入属性来导致它 - 但仍然存在所有这些额外的查询由应用程序生成,我对在哪里他们来自。

我终于放弃了搞清楚,禁用上下文的延迟加载(这会阻止额外的调用),但随后在其中一个导航属性上抛出NullReferenceException。这个特殊的导航属性我已经急于加载,所以我更加困惑为什么它是null。在该nav属性上设置一个断点,我发现它被点击两次 - 一次我调用它(并且在那时它不是null),但是第二次之后 em>我的View / Razor代码已经完成编译,它神秘地再次被击中。 调用堆栈似乎指向它来自Glimpse。果然,我能够继续完成异常并加载页面(没有错误或缺少数据),但Glimpse显示错误“元数据”选项卡引用该属性。我把两个和两个放在一起并尝试打开/关闭Glimpse,然后禁用Metadata选项卡。在任何一种情况下,额外的查询都会停止,并且在Glimpse打开但禁用了“元数据”选项卡的情况下,HUD和SQL选项卡查询计数会匹配。问题解决了。

现在只是要明确一点,我并不是说这是我上述性能问题的原因 - 当性能问题开始时,甚至没有安装Glimpse,据我所知,Glimpse默认情况下在Release模式下是关闭的。性能问题是由代码中发生的一些明显的延迟加载引起的。 然而,因为我修复了代码中发生的延迟加载,HUD选项卡中的查询计数不是递减,并且性能不是增加< / em> - 现在这很有道理。 对于我从代码中删除的每个延迟加载的查询,Glimpse通过访问“元数据”选项卡的导航属性在幕后生成一个。



底线/ TLDR:禁用Glimpse元数据选项卡可以解决我的问题。减少查询次数和在调试模式下性能更好,并且Glimpse的HUD与其SQL选项卡之间的查询计数没有差异。