question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Performance issue when querying varbinary(MAX), varchar(MAX), nvarchar(MAX) or XML with Async

See original GitHub issue

Description

I would like to resurrect the following EF6 issue: https://github.com/aspnet/EntityFramework6/issues/88 because it’s still present in EF Core 2.2 and 3.0 also.

There’s a huge performance issue when querying a table containing varbinary(MAX) columns with .ToListAsync(). The problem is EF, despite the presence of varbinary(max) column, uses CommandBehavior.Default with ExecuteReaderAsync(). (instead of CommandBehavior.SequentialAccess)

Related SO question and answer: https://stackoverflow.com/questions/28543293/entity-framework-async-operation-takes-ten-times-as-long-to-complete

This is a huge issue, we cannot use ToList as a workaround because this requires impossible checks at development time.

Steps to reproduce

I have created a small repro codebase and benchmarks.

public class AppDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=EFCore-repro-ToListAsync;Trusted_Connection=True;");
    }

    public DbSet<TestEntity> Blobs { get; set; }
}

public class TestEntity
{
    public int Id { get; set; }
    public string LargeTextBlob { get; set; }
}

public class Program
{
    static void Main(string[] args)
    {
        var summary = BenchmarkRunner.Run<Benchmarks>();
        Console.ReadKey();
    }
}

public class Benchmarks
{
    private int id;

    public Benchmarks()
    {
        using (var context = new AppDbContext())
        {
            var array = new char[5 * 1024 * 1024];
            var random = new Random();
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = (char)random.Next(32, 126);
            }

            var entity = new TestEntity { LargeTextBlob = new string(array) };
            context.Blobs.Add(entity);
            context.SaveChanges();

            id = entity.Id;
        }
    }

    [Benchmark]
    public void GetWithToList()
    {
        using (var context = new AppDbContext())
        {
            var entity = context.Blobs.Where(b => b.Id == id).ToList();
        }
    }

    [Benchmark]
    public async Task GetWithToListAsync()
    {
        using (var context = new AppDbContext())
        {
            var entity = await context.Blobs.Where(b => b.Id == id).ToListAsync();
        }
    }
}

Benckmarks

Method Mean Error StdDev
GetWithToList 45.63 ms 0.4871 ms 0.4318 ms
GetWithToListAsync 15,088.37 ms 178.2588 ms 158.0218 ms

Further technical details

EF Core version: 2.2, 3.0 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET Core 3.0, .NET Core 2.2) Operating system: Windows 10. IDE: (e.g. Visual Studio 2019 16.3)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
rojicommented, Feb 23, 2023

@SokoFromNZ and others, this has nothing to do with EF Core; this issue tracking the problem is https://github.com/dotnet/SqlClient/issues/593.

2reactions
rojicommented, Feb 25, 2021

@rogerfar the problem really is in the SqlClient provider - I’d advise upvoting the issue there to help them prioritize it more.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Be afraid of varchar(max) with async EF or ADO.NET
EF GitHub Issue: Performance issue when querying varbinary(MAX), varchar(MAX), nvarchar(MAX) or XML with Async · Stackoverflow answer. Written ...
Read more >
Entity Framework async operation takes ten times as long ...
Here's the model to create the table I benchmarked, with 3500 lines inside of it, and 256 Kb random data in each varbinary(MAX)...
Read more >
NVARCHAR(MAX) performance issues and alternatives
Hi,. We have a table that contains json values. Its set to nvarchar(max). we are having large performance issues as when we select...
Read more >
sql server - Do varchar(max), nvarchar(max) and varbinary ...
So, if you are scanning a heap or index, then the presence of an NVARCHAR(MAX) column can impact performance, even if not selecting...
Read more >
CREATE INDEX (Transact-SQL) - SQL Server
Computed columns derived from image, ntext, text, varchar(max), nvarchar(max), varbinary(max), and xml data types can be included in non-key ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found