Skip to content

GitLab Next

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • See what's new at GitLab
    • Help
    • Support
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
GitLab FOSS
GitLab FOSS
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • Requirements
    • Requirements
    • List
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • Code Review
    • Insights
    • Issues
    • Repository
    • Value Stream
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
  • GitLab.org
  • GitLab FOSSGitLab FOSS
  • Issues
  • #49651

Closed
Open
Opened Jul 26, 2018 by Andreas Brandl@abrandl🌴
  • Report abuse
  • New issue
Report abuse New issue

Filtering issues by label produces complicated query with GROUP BY

Filtering issues by label seems overly complex. For example, filtering by bug and P4 produces the below query. Let's look into how this complexity can be lowered, maybe we can even remove the GROUP BY here.

See https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/6218#note_90247101 for the motivation.



SELECT issues.*
	,(
		SELECT MIN("label_priorities"."priority")
		FROM "labels"
		INNER JOIN "label_links" ON "label_links"."label_id" = "labels"."id"
		LEFT JOIN "label_priorities" ON "labels"."id" = "label_priorities"."label_id"
		WHERE (label_priorities.project_id = issues.project_id)
			AND (label_links.target_id = issues.id)
			AND "label_links"."target_type" = 'Issue'
		) AS highest_priority
FROM "issues"
INNER JOIN "projects" ON "projects"."id" = "issues"."project_id"
INNER JOIN "label_links" ON "label_links"."target_id" = "issues"."id"
	AND "label_links"."target_type" = 'Issue'
INNER JOIN "labels" ON "labels"."id" = "label_links"."label_id"
LEFT JOIN project_features ON projects.id = project_features.project_id
WHERE (
		issues.confidential IS NOT TRUE
		OR (
			issues.confidential = TRUE
			AND (
				issues.author_id = 443319
				OR EXISTS (
					SELECT TRUE
					FROM issue_assignees
					WHERE user_id = 443319
						AND issue_id = issues.id
					)
				OR issues.project_id IN (
					SELECT "projects"."id"
					FROM "projects"
					INNER JOIN "project_authorizations" ON "projects"."id" = "project_authorizations"."project_id"
					WHERE "project_authorizations"."user_id" = 443319
						AND (project_authorizations.access_level >= 20)
					)
				)
			)
		)
	AND "projects"."namespace_id" IN (
		WITH RECURSIVE "base_and_descendants" AS (
				SELECT "namespaces".*
				FROM "namespaces"
				WHERE "namespaces"."type" IN ('Group')
					AND "namespaces"."id" = 9970
				
				UNION
				
				SELECT "namespaces".*
				FROM "namespaces"
					,"base_and_descendants"
				WHERE "namespaces"."type" IN ('Group')
					AND "namespaces"."parent_id" = "base_and_descendants"."id"
				)
		SELECT "id"
		FROM "base_and_descendants" AS "namespaces"
		)
	AND (
		EXISTS (
			SELECT 1
			FROM "project_authorizations"
			WHERE "project_authorizations"."user_id" = 443319
				AND (project_authorizations.project_id = projects.id)
			)
		OR projects.visibility_level IN (
			10
			,20
			)
		)
	AND (
		"project_features"."issues_access_level" IN (
			NULL
			,20
			)
		OR (
			"project_features"."issues_access_level" = 10
			AND EXISTS (
				SELECT 1
				FROM "project_authorizations"
				WHERE "project_authorizations"."user_id" = 443319
					AND (project_authorizations.project_id = projects.id)
				)
			)
		)
	AND ("issues"."state" IN ('opened'))
	AND "labels"."title" IN (
		'bug'
		,'P4'
		)
	AND (
		EXISTS (
			SELECT "label_links".*
			FROM "label_links"
			WHERE (
					label_links.target_type = 'Issue'
					AND label_links.target_id = issues.id
					)
				AND (label_links.label_id = 3412464) LIMIT 1
			)
		)
GROUP BY "issues"."id"
HAVING COUNT(DISTINCT labels.title) = 2
ORDER BY relative_position ASC NULLS LAST
	,highest_priority ASC NULLS LAST
	,id DESC LIMIT 20 OFFSET 0

Linked issues

  • Discussion
  • Designs
Assignee
Assign to
12.2
Milestone
12.2
Assign milestone
Time tracking
None
Due date
None
6
Labels
Plan [DEPRECATED] backend database devopsplan issues technical debt
Assign labels
  • View project labels
Reference: gitlab-org/gitlab-foss#49651