简体   繁体   中英

ef core 6/mysql performance issue

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.

  1. Ef core 6/ Mysql

  2. Table per Hierarchy approach. Here's the hierarchy:

     public class RealEstate : Property{ }
  3. Repository pattern with UnitOfWork. Here it is:

     public interface IUnitOfWork { IDataAccessLayer<Property> PropertyRepository { get; } IDataAccessLayer<RealEstate> RealEstateRepository { get; } }
  4. 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; } }
  5. I have the following index defined on the "property" table

在此处输入图像描述

  1. I have 1 million records in the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM