select Max(ma.Coloumn2), mcp.column2 from Entity1 mr
JOIN Entity2 mcp on mr.column1 = mcp.column1
JOIN Entity3 ma on mcp.column1 = ma.column1
where mr.column3 = XXX GROUP by mcp.column2 我有一个上面提到的问题。在我的Entity1中大约有200,000行,在实体3中有20k行,在实体2中只有40行。
这个查询在SQL中大约在4分钟内执行,在LINQ中它给出超时异常。但是当我重建entity2的索引时,它会在微秒内给出结果,2-3天后问题再次出现,然后我重建索引,然后问题就消失了。
我无法确定此问题的根本原因。有人能帮我吗?
发布于 2018-08-08 22:57:25
别忘了通知Entity Framework你的索引。在你的DbContext.OnModelCreating中
modelBuilder.Entity<Person>()
.Property(person => person.Name)
.IsRequired()
.HasColumnAnnotation(IndexAnnotation.AnnotationName,
new IndexAnnotation(
new IndexAttribute("IndexPersonNames", 0)));这是相当不可读的。因为我必须对许多属性执行此操作,所以我决定创建一个扩展函数:
static PrimitivePropertyConfiguration HasIndex(
this PrimitivePropertyConfiguration property,
string indexName,
int columnOrder,
bool uniqueIndexValues = false)
{
var indexAttribute = new IndexAttribute(indexName, columnOrder)
{
IsUnique = uniqueIndexValues,
};
var indexAnnotation = new IndexAnnotation(indexAttribute);
return property.hasColumnAnnotation(IndexAnnotation.AnnotationName, indexAnnotation);
}上面的modelBuilder语句将是:
modelBuilder.Entity<Person>()
.Property(person => person.Name)
.IsRequired()
.HasIndex("indexPersonNames", 0)));或者,如果您想在两列上建立索引:
.IsRequired()
.HasIndex("indexSurName", 0) // first index by SurName
.HasIndex("indexFirstName", 1) // then by FirstNamehttps://stackoverflow.com/questions/51742548
复制相似问题