I am having a strange performance issue with ef core 6 and MySlq and hoping you can help me spot the problem. here's my setup.
Ef core 6/ Mysql
Table per Hierarchy approach. Here's the hierarchy:
public class RealEstate : Property{ }
Repository pattern with UnitOfWork. Here it is:
public interface IUnitOfWork { IDataAccessLayer<Property> PropertyRepository { get; } IDataAccessLayer<RealEstate> RealEstateRepository { get; } }
Here's my database context:
public class MeerkatContext : IdentityDbContext<AppUser> { public MeerkatContext(DbContextOptions<MeerkatContext> options) : base(options) { } public DbSet<Property> Property { get; set; } public DbSet<RealEstate> RealEstate { get; set; } }
I have the following index defined on the "property" table
Here's the issue:
The following query take less than 1 seconds:
var count1 = await this._unitOfWork.PropertyRepository.CountAsync(x =>
x.CountryId == 1 && !x.IsBlocked && x.IsPublic);
this one takes 10 seconds:
var count2 = await this._unitOfWork.RealEstateRepository.CountAsync(x =>
x.CountryId == 1 && !x.IsBlocked && x.IsPublic);
I am stomped. Any help would be really appreciated.
edited to show query excution in MySql WorkBench
Thanks
Sind you probably want to search for un-blocked items, change the logic to help with the SQL:
AND NOT IsBlocked -- inefficient
AND IsBlocked = 0 -- efficient (using "=" instead of "NOT")
AND NotBlocked -- efficient (flip the name and logic)
In general: avoid OR
and NOT
when constructing SQL. (This is an oversimplification.)
Then add this 4-column index to the table:
INDEX(CountryId, IsBlocked [or NotBlocked], IsPublic, CategoryName)
Look through the rest of the schema for similar changes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.