Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use CommandBehavior.SequentialAccess because of Primary Keys #24290

Closed
panoskj opened this issue Feb 26, 2021 · 2 comments
Closed

Can't use CommandBehavior.SequentialAccess because of Primary Keys #24290

panoskj opened this issue Feb 26, 2021 · 2 comments

Comments

@panoskj
Copy link

panoskj commented Feb 26, 2021

I tried to use CommandBehavior.SequentialAccess for async operations, because the default behavior is significantly slower when there are varchar(max) columns (see dotnet/SqlClient#593 for details).

So I did this by adding the following interceptor:

public class EFInterceptor : DbCommandInterceptor
{
    public async override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
    {
        var behavior = System.Data.CommandBehavior.SequentialAccess;

        var reader = await command.ExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);

        return InterceptionResult<DbDataReader>.SuppressWithResult(reader);
    }
}

My entity looks like this:

public class Entity
{
    public int Id { get; set; } // this is the primary key
    public string Value { get; set; } // this is varchar(max) in database, about 15K characters long
}

And the code I run looks like this:

var list = dbContext.Entities.ToListAsync();

It turns out EF reads the first column twice, because it is the primary key. And this results in an exception inside SqlDataReader, because it doesn't allow reading the same column twice with SequentialAccess.

I know this because, when I added HasNoKey() for the entity, everything worked without errors, even matching the performance of the sync version.

It looks like a great opportunity for optimization and I don't understand why the "shaper" generated by EF cannot read data sequentially. Is it possible to fix this?

EF Core version: 5.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.8.6

@ajcvickers
Copy link
Member

@panoskj SequentialAccess is tracked by issue #885. Please upvote that issue.

@ErikEJ
Copy link
Contributor

ErikEJ commented Mar 1, 2021

@Wraith2 FYI

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants