[英]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.我在使用 ef core 6 和 MySlq 时遇到了一个奇怪的性能问题,希望您能帮助我发现问题。 here's my setup.
这是我的设置。
Ef core 6/ Mysql EF Core 6/Mysql
Table per Hierarchy approach.每个层次结构的表方法。 Here's the hierarchy:
这是层次结构:
public class RealEstate : Property{ }
Repository pattern with UnitOfWork.使用 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:以下查询耗时不到 1 秒:
var count1 = await this._unitOfWork.PropertyRepository.CountAsync(x =>
x.CountryId == 1 && !x.IsBlocked && x.IsPublic);
this one takes 10 seconds:这需要 10 秒:
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编辑以在 MySql WorkBench 中显示查询执行
Thanks谢谢
Sind you probably want to search for un-blocked items, change the logic to help with the SQL: Sind 您可能想要搜索未阻止的项目,更改逻辑以帮助使用 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.一般来说:在构造 SQL 时避免使用
OR
和NOT
。 (This is an oversimplification.) (这是过于简单化了。)
Then add this 4-column index to the table:然后将此 4 列索引添加到表中:
INDEX(CountryId, IsBlocked [or NotBlocked], IsPublic, CategoryName)
Look through the rest of the schema for similar changes.查看架构的其余部分是否有类似的更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.