How to determine if the Entity Framework is waitin

2019-07-16 04:02发布

问题:

I’m seeing some intermittent slowdown \ sql timeout errors that I’m having trouble determining the cause. I’ve pieced together some clues but I need some help figuring out possible next steps.

The Problem

We have a table that has 10+ million records that we run an async count from our web application. The table is written to often. Occasionally the count takes 2 minutes, sometime it timeouts, and sometimes it returns under a second all from the same generated sql.

I have a theory that the all the connections in the connection pool are busy so it's waiting... but I don't know of a way to prove it.

Our Setup

  • SQL 2012, Entity Framework 5.0, and the MVC Web API.

Clues

  • During the slow response times I can run the same sql via sql management studio and it returns under a second.
  • During the slow response times all other functionality of the site performs fine, slowness is confined to this area.
  • Problem only occurs on Production database. I've tried to reproduce it on development without any luck.

What I've tried so far

  • We had a problem where the count was locking the table and timing
    out. Because the table was locked for such a long time it caused
    havoc with our application. We changed the scope to “read
    un-committed” and that seems to have fixed locking problem but not
    the timeout.

  • Profiled the queries generated by entity framework

Details

Looking at SQL Profiler here are the queries run.

-- network protocol: LPC
set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read uncommitted



 exec sp_executesql N'SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[CampaignSendResults] AS [Extent1]
        WHERE ([Extent1].[CampaignId] = @p__linq__0) AND (0 =  CAST( [Extent1].[ResultType] AS int))
    )  AS [GroupBy1]',N'@p__linq__0 uniqueidentifier',@p__linq__0='0BACD499-A665-4721-902A-37EAB49A8A8C'


exec sp_executesql N'SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    COUNT(1) AS [A1]
    FROM [dbo].[CampaignSendResults] AS [Extent1]
    WHERE ([Extent1].[CampaignId] = @p__linq__0) AND (0 =  CAST( [Extent1].[ResultType] AS int)) AND ( CAST( [Extent1].[SendDate] AS datetime2) >= @p__linq__1)
)  AS [GroupBy1]',N'@p__linq__0 uniqueidentifier,@p__linq__1 datetime2(7)',@p__linq__0='0BACD499-A665-4721-902A-37EAB49A8A8C',@p__linq__1='2013-07-23 00:35:12.6930000'

回答1:

The problem could be

set arithabort off

it should be

set arithabort on