1

我正在使用 Ruby on Rails 和 graphql gem 构建一个 GraphQL API。现在我有一些n:m关系,比如Projectshave manyUsersUsershave many Projects。我的模型是这样的:

# /app/models/project.rb

has_many :project_assignments
has_many :project_managers, through: :project_assignments, source: :user
# /app/models/project_assignment.rb

belongs_to :project
belongs_to :user
# /app/models/user.rb

has_many :project_assignments
has_many :projects, through: :project_assignments

现在我想用这样的查询来查询所有项目及其对应的项目经理:

query {
  projects {
    edges {
      node {
        id        
        projectManagers {
          edges {
            node {
              id
            }
          }
        }
      }
    }
  }
}

而且我的解析器基本上就像Project.all和每个Project调用一样project.projectManagers,这会导致数百个查询:

  Project Load (3.3ms)  SELECT "projects".* FROM "projects"
  User Load (1.5ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 2]]
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 3]]
  User Load (1.1ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 4]]
  User Load (1.0ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 5]]
  User Load (0.8ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 6]]
... 

我添加了子弹宝石,但没有关于缺少急切加载的警告。事实上,如果我使用Project.all.includes(:project_managers),我会得到我想要的查询 ( SELECT "project_assignments".* FROM "project_assignments" WHERE "project_assignments"."project_id" IN ($1, $2, $3, $4, $5, $6, $7, $8, ...)),但User无论如何都会触发查询:

User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN ($1, $2, $3)  [["id", 3], ["id", 2], ["id", 1]]
  User Load (0.8ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 2]]
  User Load (0.9ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 3]]
  User Load (0.8ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 4]]
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 5]]
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1  [["project_id", 6]]
...

有什么我可以做的预加载用户?

eager_load也尝试过,但结果基本相同(尝试了另一个具有正常has_many关联的示例(否:through):

  SQL (1.3ms)  SELECT "projects"."id" AS t0_r0, "projects"."title" AS t0_r1, "projects"."number" AS t0_r2, "projects"."description" AS t0_r3, "projects"."deadline" AS t0_r4, "projects"."archived" AS t0_r5, "projects"."customer_id" AS t0_r6, "projects"."rate_type" AS t0_r7, "projects"."daily_rate" AS t0_r8, "projects"."service_rates" AS t0_r9, "projects"."budget_type" AS t0_r10, "projects"."budget_rate" AS t0_r11, "projects"."created_at" AS t0_r12, "projects"."updated_at" AS t0_r13, "projects"."status" AS t0_r14, "projects"."slug" AS t0_r15, "project_labels"."id" AS t1_r0, "project_labels"."title" AS t1_r1, "project_labels"."description" AS t1_r2, "project_labels"."color" AS t1_r3, "project_labels"."project_id" AS t1_r4, "project_labels"."created_at" AS t1_r5, "project_labels"."updated_at" AS t1_r6 FROM "projects" LEFT OUTER JOIN "project_labels" ON "project_labels"."project_id" = "projects"."id" WHERE "projects"."id" IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)  [["id", 1], ["id", 2], ["id", 3], ["id", 4], ["id", 5], ["id", 6], ["id", 7], ["id", 8], ["id", 9], ["id", 10]]
  ProjectLabel Load (0.3ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 10]]
  ProjectLabel Load (0.5ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 2]]
  ProjectLabel Load (0.7ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 5]]
  ProjectLabel Load (0.5ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 8]]
  ProjectLabel Load (0.5ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 6]]
  ProjectLabel Load (0.6ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 4]]
  ProjectLabel Load (0.4ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 1]]
  ProjectLabel Load (0.3ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 3]]
  ProjectLabel Load (0.3ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 9]]
  ProjectLabel Load (0.5ms)  SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1  [["project_id", 7]]
4

2 回答 2

2

你可以做各种各样的事情,has_many包括includes哪些应该有助于 N+1 查询

has_many :project_assignments, -> { includes(:projects) }
于 2019-10-10T19:12:34.447 回答
1
Project.all.includes(:project_managers)
# Project.includes(:project_managers) # shorthand of above

... 将project_managers根据查询自动包含在 SQL 中。

还包括project_assignment.user,然后将它们合并:

Project.includes(:project_managers, project_assignments: :user)

# probably below is equivalent of above (but not sure)
# just because `project_managers` association is also going "through" :project_assignments
Project.includes(project_assignments: :user)

请参阅文档中的“加载嵌套关系”

于 2019-10-11T10:03:23.707 回答