Skip to content

Optimize use_elasticsearch? call

Adam Hegyi requested to merge optimize_elasticsearch_indexes_project_query into master

What does this MR do?

Solves the 1st point of https://gitlab.com/gitlab-com/gl-infra/infrastructure/-/issues/9447#note_302066872

Original query:

SELECT 1 AS one
FROM   (
       (
              SELECT "projects".*
              FROM   "projects"
              WHERE  "projects"."namespace_id" IN (WITH recursive "base_and_descendants" AS (
                                                   (
                                                          SELECT "namespaces".*
                                                          FROM   "namespaces"
                                                          WHERE  "namespaces"."id" IN
                                                                 (
                                                                        SELECT "elasticsearch_indexed_namespaces"."namespace_id"
                                                                        FROM   "elasticsearch_indexed_namespaces"))
                                            UNION
                                                  (
                                                         SELECT "namespaces".*
                                                         FROM   "namespaces",
                                                                "base_and_descendants"
                                                         WHERE  "namespaces"."parent_id" = "base_and_descendants"."id"))SELECT "id"
                                             FROM   "base_and_descendants" AS "namespaces"))
UNION
      (
             SELECT "projects".*
             FROM   "projects"
             WHERE  "projects"."id" IN
                    (
                           SELECT "elasticsearch_indexed_projects"."project_id"
                           FROM   "elasticsearch_indexed_projects"))) projects
 WHERE "projects"."id" = 278964 limit 1

Query Plan

Problematic part:

SELECT "elasticsearch_indexed_namespaces"."namespace_id" FROM   "elasticsearch_indexed_namespaces"

When we load all the indexed namespaces, this will return many rows since we recursively load their descendants as well (6.5k records at this point).

Other approach:

Load the project's group and it's ancestors. Then join the groups with elasticsearch_indexed_namespaces to filter them (only groups where indexing is enabled remain).

SELECT 1 AS one
FROM   ( SELECT ) AS projects
WHERE  ((
                     EXISTS (WITH recursive "base_and_ancestors" AS (
                     (
                            SELECT "namespaces".*
                            FROM   "namespaces"
                            WHERE  "namespaces"."id" = 9970)
              UNION
                    (
                           SELECT "namespaces".*
                           FROM   "namespaces",
                                  "base_and_ancestors"
                           WHERE  "namespaces"."id" = "base_and_ancestors"."parent_id"))SELECT     "namespaces".*
               FROM       "base_and_ancestors" AS "namespaces"
               INNER JOIN "elasticsearch_indexed_namespaces"
               ON         "elasticsearch_indexed_namespaces"."namespace_id" = "namespaces"."id"))
   OR         (
                         EXISTS
                         (
                                SELECT "elasticsearch_indexed_projects".*
                                FROM   "elasticsearch_indexed_projects"
                                WHERE  "elasticsearch_indexed_projects"."project_id" = 278964))) limit 1

Query 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
Edited by Adam Hegyi

Merge request reports