有人能帮我加速这个查询吗?我使用的是RavenDB 3.5.8,下面的查询花费了相当多的时间(在第一次加载时尤其如此):
var query = session
.Query<Card>()
.Include(p => p.AuthorId)
.Include(p => p.CompanyId)
.Where(x => !x.Id.StartsWith("Archived"))
.Where(x => !x.IsDeleted);
do
{
var popStash = stashedResults != null && stashedResults.Count > 0;
if (popStash)
{
stashSkip = stashedResults.Count;
}
results = query
.Statistics(out stats)
.OrderByDescending(x => x.ModifiedAt)
.Skip(this.ServerPage * this.ServerPageSize)
.Take(this.ServerPageSize)
.Select(MapCard)
.Where(x => x.IsAuthorized)
.Skip(clientPage * command.PageSize)
.Take(command.PageSize - stashSkip)
.ToList();
if (popStash)
{
results = results.Union(stashedResults).ToList();
stashedResults.Clear();
}
if (results.Count < command.PageSize)
{
++this.ServerPage;
clientPage = 0;
if (results.Count > 0)
{
stashedResults = results;
}
}
} while (results.Count < command.PageSize &&
stats.TotalResults >= (this.ServerPage * this.ServerPageSize) + this.ServerPageSize);我知道这还不够完美,但请注意,由于RavenDB默认不支持身份验证,因此双Skip和Take是必要的,因此我必须Map每张卡,并使用以下代码片段将IsAuthorized字段添加到其中:
IsAuthorized = session.Advanced.IsOperationAllowedOnDocument("Authorization/Users/" + user.Id, "Cards/View", card.Id).IsAllowed;基本上,首先,查询加载一堆卡(实际上是ServerPageSize定义的卡的数量),而不考虑安全性,然后当应用安全性时,加载command.PageSize卡。(网页动态加载卡片-前50张卡片被加载,在这些卡片被查看之后,另有50张卡片被抓取,以此类推)。
有人能给我一些提示如何提高我的代码的性能吗?
发布于 2021-03-03 23:37:19
您应该能够使用流支持,以避免多次查询。
你也是在查询一个负面的东西,最好先查询一些正面的东西,然后再否定它。
请注意,这意味着您正在运行一个非常昂贵的SELECT N+1,对于每个结果,您将得到一个查询。
为什么不使用授权捆绑包查询过滤功能?
请参阅此处的文档:
https://ravendb.net/docs/article-page/3.5/Csharp/server/bundles/authorization#operations
您错过了对SecureFor的调用
您可以使用:
session.SecureFor("Authorization/Users/" + user.Id, "Cards/View");现在RavenDB将负责过滤服务器端。
https://stackoverflow.com/questions/66424571
复制相似问题