我对数据库的查询有问题。 当我尝试下面的代码时,它工作75毫秒并返回空列表。一切都很好。
var test1 = unit
.PersonQualities.GetAll()
.Where(qp => qp.QualityId == qualityId && qp.PersonId == targetPersonId)
.ToList();
但是当我使用它时,它工作30秒并返回超时过期异常:
var test2 = unit
.PersonQualities.GetAll()
.FirstOrDefault(qp => qp.QualityId == qualityId && qp.PersonId == targetPersonId);
我的GetAll方法:
public virtual IQueryable<T> GetAll()
{
return Entities.AsQueryable();
}
请解释为什么会这样。我能做些什么来解决这个问题。谢谢。 附:我正在使用SQL Server 2016 Developer。这两个查询都适用于SSMS。
Profiler中的FirstOrDefault查询:
exec sp_executesql N'SELECT TOP (1)
[Extent1].[Id] AS [Id],
[Extent1].[Value] AS [Value],
[Extent1].[Percent] AS [Percent],
[Extent1].[IsLocked] AS [IsLocked],
[Extent1].[PersonId] AS [PersonId],
[Extent1].[QualityId] AS [QualityId]
FROM [dbo].[PersonQuality] AS [Extent1]
WHERE ([Extent1].[QualityId] = @p__linq__0) AND ([Extent1].[PersonId] = @p__linq__1)',N'@p__linq__0 bigint,@p__linq__1 bigint',@p__linq__0=110207,@p__linq__1=43257113
的SingleOrDefault:
exec sp_executesql N'SELECT TOP (2)
[Extent1].[Id] AS [Id],
[Extent1].[Value] AS [Value],
[Extent1].[Percent] AS [Percent],
[Extent1].[IsLocked] AS [IsLocked],
[Extent1].[PersonId] AS [PersonId],
[Extent1].[QualityId] AS [QualityId]
FROM [dbo].[PersonQuality] AS [Extent1]
WHERE ([Extent1].[QualityId] = @p__linq__0) AND ([Extent1].[PersonId] = @p__linq__1)',N'@p__linq__0 bigint,@p__linq__1 bigint',@p__linq__0=110207,@p__linq__1=43257113
答案 0 :(得分:0)
您可能需要更新统计信息并刷新查询缓存。
DBCC FLUSHPROCINDB
和
UPDATE STATISTICS
可能会有所帮助。