我有一个使用 RavenDB 的 WPF 应用程序。在其中一个窗口上,我可以反复点击刷新按钮来更新网格。每次刷新都会导致 RavenDB 查询。奇怪的是,查询会时不时地花费很长时间。例如,我可以连续七次点击刷新按钮,网格将在不到一秒的时间内刷新。然后,在第 8 次尝试时,需要 12 秒。然后,我可以再点击 Refresh 11 次,它又跑得很快,但第 12 次,又需要 12 秒。这种方式非常一致。每次出现延迟似乎总是需要 12 秒。那是什么?
这是查询:
IQueryable<EntityBase> installationSummaries =
QueryAndSetEtags(session => session.Query<InstallationSummary>()
.Include(x => x.ApplicationServerId)
.Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
.Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
.Customize(x => x.WaitForNonStaleResults())
.OrderByDescending(summary => summary.InstallationStartUtc)
.Take(numberToRetrieve)
);
HydrateInstallationSummaries(installationSummaries);
return installationSummaries.AsEnumerable().Cast<InstallationSummary>();
延迟总是在这行代码上(entityBases 是上述查询的返回值):
List<EntityBase> entityBaseList = entityBases.ToList();
在 RavenDB 中解决此类问题的正确方法是什么?我不确定下一步该尝试什么。
注意:此代码已很久没有更改。我现在使用的是 RavenDB 960。RavenDB 573 从未发生过这种情况。
编辑——附加代码以显示围绕此查询发生的其他情况。
private static void HydrateInstallationSummaries(IQueryable<EntityBase> installationSummaries)
{
// Note: We use session.Load() below so that we get the information from the session, and not another trip to the DB.
foreach (InstallationSummary summary in installationSummaries)
{
HydrateInstallationSummary(summary);
}
}
private static void HydrateInstallationSummary(InstallationSummary summary)
{
if (summary == null) { return; }
summary.ApplicationServer =
QuerySingleResultAndSetEtag(session => session.Load<ApplicationServer>(summary.ApplicationServerId))
as ApplicationServer;
summary.ApplicationWithOverrideVariableGroup.Application =
QuerySingleResultAndSetEtag(session => session.Load<Application>(summary.ApplicationWithOverrideVariableGroup.ApplicationId))
as Application;
if (summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId == null) { return; }
summary.ApplicationWithOverrideVariableGroup.CustomVariableGroup =
QuerySingleResultAndSetEtag(session =>
{
if (session.Advanced.IsLoaded(summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId))
{
return session.Load<CustomVariableGroup>(summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId);
}
return null; // Note: We can be missing a custom variable in the session because someone deleted it.
;
})
as CustomVariableGroup;
}
这里是 QueryAndSetEtags():
protected static IQueryable<EntityBase> QueryAndSetEtags(Func<IDocumentSession, IQueryable<EntityBase>> func)
{
if (func == null) { throw new ArgumentNullException("func"); }
IQueryable<EntityBase> entities = func.Invoke(_session);
SetEtags(entities, _session);
return entities;
}
protected static void SetEtags(IEnumerable<EntityBase> entityBases, IDocumentSession session)
{
foreach (EntityBase entityBase in entityBases)
{
SetEtag(entityBase, session);
}
}
protected static void SetEtag(EntityBase entityBase, IDocumentSession session)
{
entityBase.Etag = (Guid)session.Advanced.GetEtagFor(entityBase);
}