Skip to content

Optimize container repository query

Adam Hegyi requested to merge 220209-optimize-container-repository-query into master

What does this MR do?

Relevant comment: #220209 (comment 358848874)

Original query:

SELECT "container_repositories".*
FROM   "container_repositories"
WHERE  "container_repositories"."project_id" IN
       (
              SELECT "projects"."id"
              FROM   "projects"
              WHERE  "projects"."namespace_id" IN (WITH recursive "base_and_descendants" AS (
                                                           (
                                                                  SELECT "namespaces".*
                                                                  FROM   "namespaces"
                                                                  WHERE  "namespaces"."type" = 'group'
                                                                  AND    "namespaces"."id" = 28)
                                                    UNION
                                                          (
                                                                 SELECT "namespaces".*
                                                                 FROM   "namespaces",
                                                                        "base_and_descendants"
                                                                 WHERE  "namespaces"."type" = 'group'
                                                                 AND    "namespaces"."parent_id" = "base_and_descendants"."id"))SELECT "id"
                                                     FROM   "base_and_descendants" AS "namespaces")
              AND    "projects"."container_registry_enabled" = true) limit 1;

Plan

New query:

SELECT 1 AS one
FROM "container_repositories"
INNER JOIN
  (SELECT "projects"."id"
   FROM "projects"
   WHERE "projects"."namespace_id" IN
       (WITH RECURSIVE "base_and_descendants" AS (
                                                    (SELECT "namespaces".*
                                                     FROM "namespaces"
                                                     WHERE "namespaces"."type" = 'Group'
                                                       AND "namespaces"."id" = 9970)
                                                  UNION
                                                    (SELECT "namespaces".*
                                                     FROM "namespaces",
                                                          "base_and_descendants"
                                                     WHERE "namespaces"."type" = 'Group'
                                                       AND "namespaces"."parent_id" = "base_and_descendants"."id")) SELECT "id"
        FROM "base_and_descendants" AS "namespaces")
     AND "projects"."container_registry_enabled" = TRUE) projects ON projects.id=container_repositories.project_id
LIMIT 1

Plan

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team

Related to #220209 (closed)

Edited by Yorick Peterse

Merge request reports