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.

[EFCore] AbpODataEntityController SingleResult performs full table scan, alternatives?

See original GitHub issue
  • Abp 3.9.0
  • .Net Core 2.1
  • EF Core

As the title suggests, this seems to be by design, since the AbpODataEntityController implements the Get for a single entity (e.g. http://localhost:21021/odata/Persons(11)) by calling GetAll() and then performing a Where such as :

[EnableQuery]
public virtual SingleResult<TEntity> Get([FromODataUri] TPrimaryKey key)
{
    var entity = Repository.GetAll().Where(e => e.Id.Equals(key));
    return SingleResult.Create(entity);
}

However in EfCoreRepositoryBaseOfTEntityAndTPrimaryKeys GetAll() calls GetAllIncluding() which is implemented like this:

public override IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors)
        {
            var query = Table.AsQueryable();
            ...
            return query;
        }

Table.AsQueryable() results in a full table scan whereas the desired result is a single entity.

I’m aware that we can just override the SingleResult function in our controller in the Web.Core to query single rows for example:

[EnableQuery]
public override SingleResult<Person> Get([FromODataUri] int key)
{
    var entity = new List<Person> { _repository.Single(c => c.Pin == key) }.AsQueryable();
    return SingleResult.Create(entity);
}

However this will not include the navigation properties (e.g. Addresses) unless we explicitly reference the EFCore nuget package in our Web.Core project in order to be able to use .Include().

Is this by design? Or would your recommend a better approach to avoid a full table scan for SingleResult OData queries?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
acjhcommented, Oct 22, 2018

Depends how you map Id.

public class Person : Entity<int>
{        
    [Column("Pin")]
    public override int Id { get; set; }

    // [Key]
    // public virtual int Pin { get; set; }
}
1reaction
acjhcommented, Oct 22, 2018

Try:

- var entity = Repository.GetAll().Where(e => e.Id.Equals(key));
+ var entity = Repository.GetAll().Where(e => e.Pin.Equals(key));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework 5 performing full table scan
When I check my database profile (SqlServer 2012) I can see an horrible full table scan without any "TOP" clause. The interesting part:....
Read more >
Efficient Querying - EF Core
Performance guide for efficient querying using Entity Framework Core. ... whether the query traverses the entire table, or uses an index.
Read more >
8.2.1.23 Avoiding Full Table Scans
The output from EXPLAIN shows ALL in the type column when MySQL uses a full table scan to resolve a query. This usually...
Read more >
Table.Scan method in the AWS SDK for .NET
The Scan method performs a full table scan. It provides two overloads. The only parameter required by the Scan method is the scan...
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