【发布时间】:2021-03-01 15:08:10
【问题描述】:
谁能帮我加快查询速度?我正在使用 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);
我知道这远非完美,但请注意,Skip 和 Take 是必要的,因为 RavenDB 默认不支持身份验证,所以我必须 Map 每张卡并添加IsAuthorized 字段使用以下代码 sn-p:
IsAuthorized = session.Advanced.IsOperationAllowedOnDocument("Authorization/Users/" + user.Id, "Cards/View", card.Id).IsAllowed;
基本上发生的情况是,首先查询加载一堆卡片(实际上 ServerPageSize 定义了那里的卡片数量)而不考虑安全性,然后在应用安全性时加载 command.PageSize 卡片。 (网页动态加载卡片 - 前 50 张卡片被加载,在查看之后,另外 50 张卡片被抓取,依此类推..
谁能给我一些提示如何提高我的代码的性能?
【问题讨论】: