Optimize use_elasticsearch? call
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
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
Does this MR meet the acceptance criteria?
Conformity
-
Changelog entry -
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Tested in all supported browsers -
Informed Infrastructure department of a default or new setting change, if applicable per definition of done
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