Query for merge requests joins a lot of tables for what appears to be an existence check

Our Crunchy consultant found the following query in our statements history:

SELECT  ? AS one 
FROM "merge_requests" 
INNER JOIN "label_links" ON "label_links"."target_id" = "merge_requests"."id" 
AND "label_links"."target_type" = ? 
INNER JOIN "labels" ON "labels"."id" = "label_links"."label_id" 
LEFT OUTER JOIN milestones ON merge_requests.milestone_id = milestones.id 
WHERE "merge_requests"."deleted_at" IS NULL 
AND "milestones"."title" = ? 
AND "milestones"."project_id" = ?
 AND "labels"."title" = ? AND "labels"."id" IN (SELECT "labels"."id" FROM "labels" 
WHERE (labels.id IN (SELECT "labels"."id" FROM "labels" WHERE "labels"."group_id" IN (SELECT "projects"."namespace_id" FROM "projects" INNER JOIN "namespaces" ON "namespaces"."id" = "projects"."namespace_id" AND "namespaces"."deleted_at" IS NULL WHERE "projects"."pending_delete" = ? AND "projects"."id" = ? AND "namespaces"."type" = ?)

We couldn't quite figure out what it's used for, but we suspect it's used to check if something exists or not (note the ? AS one part). For an existence check all of the JOINs and conditions seem to be a bit of an overkill. On average this query takes around 720 milliseconds to execute.

Assignee Loading
Time tracking Loading