efcore icon indicating copy to clipboard operation
efcore copied to clipboard

EF Core reads entire table (all columns for all rows)

Open mfields3 opened this issue 6 years ago • 8 comments

EF Core is reading the entire contents of tables, despite no code in the application requesting the data. When a table is sufficiently large, as it was in our case, the dotnet application consumes all RAM of the web-server as EF Core saves the table contents to memory. This took down our entire application across multiple web-servers.

Logs of SQL queries are from the built-in logging of EF Core, viewable to us in Seq via Serilog, show EF Core executed a query for the entire table contents: SELECT [m].[col1name], ... [m.][colxname] FROM [mytable] AS [m] This happened to be a very large table in our DB, well in excess of the RAM on the web-server, leading to the process consuming all RAM on the server and grinding to a halt.

This was originally a suspected memory leak in our .NET Core 3.1 web API, until we saw the logs showing the queries executed by EF Core. We see EF Core is reading the entire table contents from other tables too, so it's not just this very large table where code only writes entries to it.

Steps to reproduce

This has happened around 24 times across 3 different web-servers hosting the same application in the past week. Have been unable to determine why or when it occurs, nor reliably reproduce.

The only code across the application which references the table in question is the below, which is within the Invoke method of middleware on the DbContext with interface IPipelineMiddleware:

context.MyTable.AddRange(entriesToAdd);
await context.SaveChangesAsyncBase();

Further technical details

EF Core version: 3.1.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET Core 3.1 Operating system: Windows Server 2019 IDE: Visual Studio 2019

mfields3 avatar Jun 05 '20 14:06 mfields3

@mfields3 SaveChangesAsyncBase isn't a method EF Core provides, are you using some sort of 3rd-party extension?

In any case, we can't do much without more details - EF Core certainly doesn't generate random SELECT queries to fetch the entire table contents. Try investigating where that method comes from and what it does, and to search for other references to the table in question.

roji avatar Jun 05 '20 15:06 roji

Are you using a non-updated version of Serilog?

ErikEJ avatar Jun 05 '20 15:06 ErikEJ

@roji SaveChangesAsyncBase is defined in our Context simply as:

public Task<int> SaveChangesAsyncBase(CancellationToken cancellationToken = default) => base.SaveChangesAsync(cancellationToken);

There are absolutely no other references to this table other than in the Context:

public virtual DbSet<MyTable> MyTable { get; set; }
...
modelBuilder.Entity<MyTable>(entity =>
            {
                entity.ToTable("mytable");
            });

mfields3 avatar Jun 05 '20 16:06 mfields3

@ErikEJ We are using the latest Serilog.AspNetCore v3.2.0 with Serilog.Sinks.Seq v4.0.0

mfields3 avatar Jun 05 '20 16:06 mfields3

Duplicate of #15214

smitpatel avatar Jun 05 '20 16:06 smitpatel

Interesting. #15214 points to Serilog.Exceptions which has a disclaimer about EF Core and adding another package as a workaround: https://github.com/RehanSaeed/Serilog.Exceptions#serilogexceptionsentityframeworkcore

However, we don't use Serilog.Exceptions. All packages we use for logging is as follows:

    <PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
    <PackageReference Include="Serilog.Extensions.Hosting" Version="3.0.0" />
    <PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />

mfields3 avatar Jun 05 '20 19:06 mfields3

@mfields3 We discussed this again, and we still believe it is something that Serilog is doing with the exception instance. However, it's hard to be sure without being able to reproduce the issue. Would it be possible for you to narrow down where this is happening and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. I suspect that getting your code to throw DbUpdateExceptions (for database errors) with your Serilog logging configuration might be enough.

ajcvickers avatar Jun 08 '20 18:06 ajcvickers

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

ajcvickers avatar Jun 22 '20 16:06 ajcvickers