HotChocolate UsePaging 或 UseOffsetPaging 将查询速度减慢至 30 秒以上

发布于 2025-01-16 09:09:29 字数 1457 浏览 1 评论 0原文

我是 HotChocolate 和 graphql 的新手,但目前 HotChocolate 遇到的这个问题有点奇怪。

所以我在 dotnet 6.0 webapi 项目中使用以下包:

  1. HotChocolate.AspNetCore @ 12.5.0
  2. HotChocolate.AspNetCore.Authorization @ 12.5.0
  3. HotChocolate.Data @ 12.5.0
  4. HotChocolate.Types.Scalars @ 12.5.0

我有一个像下面这样查询

    public IQueryable<EmploymentContract> GetEmploymentContracts([Service] ApplicationDbContext dbContext)
        => dbContext.EmploymentContracts
            .Include(x => x.Person)
            .Include(x => x.Organisation)
            .Include(x => x.Department);

,然后像下面这样查询它的描述符

        descriptor.Field(x => x.GetEmploymentContracts(_dbContext))
            .UsePaging(options: new PagingOptions { DefaultPageSize = PAGE_SIZE, IncludeTotalCount = true })
            .UseFiltering()
            .UseSorting();

现在的问题是如果我运行如下查询,有时甚至需要 30 秒以上才能返回结果。我检查了 ef core 生成的 sql 查询,并在 200ms 内以 mssql 运行。无论是什么减慢了请求速度,都是在 ef core 运行查询并从数据库获取结果之后发生的。

{
    employmentContracts {
        nodes {
            personnelNumber
        }
    }
}

如您所见,上面的查询非常简单。

当我删除 UsePaging 时,它工作正常,我立即得到结果。

但如果没有,它要么会超时,要么会运行 22 秒左右...

编辑

问题已解决,问题不在于 GraphQL。 该模型在发生查询的地方存储了一些图像。模型已更改,问题不再发生。

I'm new to HotChocolate and graphql but this issue that I have at the moment with HotChocolate is a bit strange.

So I'm using the following packages in a dotnet 6.0 webapi project:

  1. HotChocolate.AspNetCore @ 12.5.0
  2. HotChocolate.AspNetCore.Authorization @ 12.5.0
  3. HotChocolate.Data @ 12.5.0
  4. HotChocolate.Types.Scalars @ 12.5.0

I have a query like beloew

    public IQueryable<EmploymentContract> GetEmploymentContracts([Service] ApplicationDbContext dbContext)
        => dbContext.EmploymentContracts
            .Include(x => x.Person)
            .Include(x => x.Organisation)
            .Include(x => x.Department);

and then a descriptor for it like

        descriptor.Field(x => x.GetEmploymentContracts(_dbContext))
            .UsePaging(options: new PagingOptions { DefaultPageSize = PAGE_SIZE, IncludeTotalCount = true })
            .UseFiltering()
            .UseSorting();

Now the issue is that if I run a query like below, it takes even sometimes more than 30 seconds to give back the results. I checked the ef core generated sql query and that runs in mssql in 200ms. Whatever is slowing down the request, is happening after ef core runs the query and gets the result from the database.

{
    employmentContracts {
        nodes {
            personnelNumber
        }
    }
}

As you see, the above query is really simple.

When I remove the UsePaging it works fine and I get the results immediately.

But if not, it either will give a timeout or will run for 22s or so...

Edit

The problem is solved and the issue was not with GraphQL.
The model had some images stored in places where the query was happening. The model is changed and the issue is not happening anymore.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文