Skip to content

feat: Update Occurrence group scope to rely off traversal_ids

What does this MR do and why?

feat: Update Sbom::Occurrence group scope to rely off traversal_ids

Previous scope joined across projects/namespaces but we can perform this query using denormalized traversal_ids instead

Relates to Fix cross-join between Project.filter_by_search... (#498735 - closed)

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Database review

Before

includes(:namespace)
 SELECT "namespaces"."id",
       "namespaces"."name",
       "namespaces"."path",
       "namespaces"."owner_id",
       "namespaces"."created_at",
       "namespaces"."updated_at",
       "namespaces"."type",
       "namespaces"."description",
       "namespaces"."avatar",
       "namespaces"."membership_lock",
       "namespaces"."share_with_group_lock",
       "namespaces"."visibility_level",
       "namespaces"."request_access_enabled",
       "namespaces"."ldap_sync_status",
       "namespaces"."ldap_sync_error",
       "namespaces"."ldap_sync_last_update_at",
       "namespaces"."ldap_sync_last_successful_update_at",
       "namespaces"."ldap_sync_last_sync_at",
       "namespaces"."description_html",
       "namespaces"."lfs_enabled",
       "namespaces"."parent_id",
       "namespaces"."shared_runners_minutes_limit",
       "namespaces"."repository_size_limit",
       "namespaces"."require_two_factor_authentication",
       "namespaces"."two_factor_grace_period",
       "namespaces"."cached_markdown_version",
       "namespaces"."project_creation_level",
       "namespaces"."runners_token",
       "namespaces"."file_template_project_id",
       "namespaces"."saml_discovery_token",
       "namespaces"."runners_token_encrypted",
       "namespaces"."custom_project_templates_group_id",
       "namespaces"."auto_devops_enabled",
       "namespaces"."extra_shared_runners_minutes_limit",
       "namespaces"."last_ci_minutes_notification_at",
       "namespaces"."last_ci_minutes_usage_notification_level",
       "namespaces"."subgroup_creation_level",
       "namespaces"."max_pages_size",
       "namespaces"."max_artifacts_size",
       "namespaces"."mentions_disabled",
       "namespaces"."default_branch_protection",
       "namespaces"."max_personal_access_token_lifetime",
       "namespaces"."push_rule_id",
       "namespaces"."shared_runners_enabled",
       "namespaces"."allow_descendants_override_disabled_shared_runners",
       "namespaces"."traversal_ids",
       "namespaces"."organization_id"
FROM   "namespaces"
WHERE  "namespaces"."id" = 2452873
includes(:project)
 SELECT "projects"."id",
       "projects"."name",
       "projects"."path",
       "projects"."description",
       "projects"."created_at",
       "projects"."updated_at",
       "projects"."creator_id",
       "projects"."namespace_id",
       "projects"."last_activity_at",
       "projects"."import_url",
       "projects"."visibility_level",
       "projects"."archived",
       "projects"."avatar",
       "projects"."merge_requests_template",
       "projects"."star_count",
       "projects"."merge_requests_rebase_enabled",
       "projects"."import_type",
       "projects"."import_source",
       "projects"."approvals_before_merge",
       "projects"."reset_approvals_on_push",
       "projects"."merge_requests_ff_only_enabled",
       "projects"."issues_template",
       "projects"."mirror",
       "projects"."mirror_last_update_at",
       "projects"."mirror_last_successful_update_at",
       "projects"."mirror_user_id",
       "projects"."shared_runners_enabled",
       "projects"."runners_token",
       "projects"."build_allow_git_fetch",
       "projects"."build_timeout",
       "projects"."mirror_trigger_builds",
       "projects"."pending_delete",
       "projects"."public_builds",
       "projects"."last_repository_check_failed",
       "projects"."last_repository_check_at",
       "projects"."only_allow_merge_if_pipeline_succeeds",
       "projects"."has_external_issue_tracker",
       "projects"."repository_storage",
       "projects"."repository_read_only",
       "projects"."request_access_enabled",
       "projects"."has_external_wiki",
       "projects"."ci_config_path",
       "projects"."lfs_enabled",
       "projects"."description_html",
       "projects"."only_allow_merge_if_all_discussions_are_resolved",
       "projects"."repository_size_limit",
       "projects"."printing_merge_request_link_enabled",
       "projects"."auto_cancel_pending_pipelines",
       "projects"."service_desk_enabled",
       "projects"."cached_markdown_version",
       "projects"."delete_error",
       "projects"."last_repository_updated_at",
       "projects"."disable_overriding_approvers_per_merge_request",
       "projects"."storage_version",
       "projects"."resolve_outdated_diff_discussions",
       "projects"."remote_mirror_available_overridden",
       "projects"."only_mirror_protected_branches",
       "projects"."pull_mirror_available_overridden",
       "projects"."jobs_cache_index",
       "projects"."external_authorization_classification_label",
       "projects"."mirror_overwrites_diverged_branches",
       "projects"."pages_https_only",
       "projects"."external_webhook_token",
       "projects"."packages_enabled",
       "projects"."merge_requests_author_approval",
       "projects"."pool_repository_id",
       "projects"."runners_token_encrypted",
       "projects"."bfg_object_map",
       "projects"."detected_repository_languages",
       "projects"."merge_requests_disable_committers_approval",
       "projects"."require_password_to_approve",
       "projects"."max_pages_size",
       "projects"."max_artifacts_size",
       "projects"."pull_mirror_branch_prefix",
       "projects"."remove_source_branch_after_merge",
       "projects"."marked_for_deletion_at",
       "projects"."marked_for_deletion_by_user_id",
       "projects"."autoclose_referenced_issues",
       "projects"."suggestion_commit_message",
       "projects"."project_namespace_id",
       "projects"."hidden",
       "projects"."organization_id"
FROM   "projects"
WHERE  "projects"."id" IN (4894834, 5351075, 5373222, 5417931, 5498087, 5576532, 5617895, 5742170, 5779760, 5945915, 5976131, 5994307, 6130122, 6168240, 6802300, 9762266, 11146000, 11428501, 12006272, 12724255, 13467157, 14235835, 15232184, 15677708, 15841525, 16597098, 16683102, 18042963, 20145500, 20785013, 22441624, 23992616, 24111720, 24112430, 24249085, 26588735, 26751150, 26893673, 27038823, 27060148, 28274618, 28274632, 33931271, 37734140, 43777063, 58060746, 59029330, 60325468, 61151383)
SELECT sbom_occurrences
SELECT
    "sbom_occurrences".*
FROM
    "sbom_occurrences"
WHERE
    "sbom_occurrences"."component_version_id" = 45569598
    AND "sbom_occurrences"."project_id" IN (
        SELECT
            "projects"."id"
        FROM
            "projects"
        WHERE
            "projects"."namespace_id" IN (
                SELECT
                    "namespaces"."id"
                FROM
                    unnest(COALESCE((
                                SELECT
                                    ids FROM (
                                        SELECT
                                            "namespace_descendants"."self_and_descendant_group_ids" AS ids FROM "namespace_descendants"
                                        WHERE
                                            "namespace_descendants"."outdated_at" IS NULL
                                            AND "namespace_descendants"."namespace_id" = 2452873) cached_query), (
                                        SELECT
                                            ids FROM (
                                                SELECT
                                                    Array_agg("namespaces"."id") AS ids FROM (
                                                        SELECT
                                                            namespaces.traversal_ids[array_length(namespaces.traversal_ids, 1)] as id FROM "namespaces"
                                                        WHERE
                                                            "namespaces"."type" = 'Group'
                                                            AND (traversal_ids @> ('{2452873}'))) namespaces) consistent_query))) AS namespaces (id)))

After

preload(:namespace)
 SELECT "namespaces"."id",
       "namespaces"."name",
       "namespaces"."path",
       "namespaces"."owner_id",
       "namespaces"."created_at",
       "namespaces"."updated_at",
       "namespaces"."type",
       "namespaces"."description",
       "namespaces"."avatar",
       "namespaces"."membership_lock",
       "namespaces"."share_with_group_lock",
       "namespaces"."visibility_level",
       "namespaces"."request_access_enabled",
       "namespaces"."ldap_sync_status",
       "namespaces"."ldap_sync_error",
       "namespaces"."ldap_sync_last_update_at",
       "namespaces"."ldap_sync_last_successful_update_at",
       "namespaces"."ldap_sync_last_sync_at",
       "namespaces"."description_html",
       "namespaces"."lfs_enabled",
       "namespaces"."parent_id",
       "namespaces"."shared_runners_minutes_limit",
       "namespaces"."repository_size_limit",
       "namespaces"."require_two_factor_authentication",
       "namespaces"."two_factor_grace_period",
       "namespaces"."cached_markdown_version",
       "namespaces"."project_creation_level",
       "namespaces"."runners_token",
       "namespaces"."file_template_project_id",
       "namespaces"."saml_discovery_token",
       "namespaces"."runners_token_encrypted",
       "namespaces"."custom_project_templates_group_id",
       "namespaces"."auto_devops_enabled",
       "namespaces"."extra_shared_runners_minutes_limit",
       "namespaces"."last_ci_minutes_notification_at",
       "namespaces"."last_ci_minutes_usage_notification_level",
       "namespaces"."subgroup_creation_level",
       "namespaces"."max_pages_size",
       "namespaces"."max_artifacts_size",
       "namespaces"."mentions_disabled",
       "namespaces"."default_branch_protection",
       "namespaces"."max_personal_access_token_lifetime",
       "namespaces"."push_rule_id",
       "namespaces"."shared_runners_enabled",
       "namespaces"."allow_descendants_override_disabled_shared_runners",
       "namespaces"."traversal_ids",
       "namespaces"."organization_id"
FROM   "namespaces"
WHERE  "namespaces"."id" = 2452873
preload(:project)

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/32396/commands/100062

SELECT "projects"."id", "projects"."name", "projects"."path", "projects"."description", "projects"."created_at", "projects"."updated_at", "projects"."creator_id", "projects"."namespace_id", "projects"."last_activity_at", "projects"."import_url", "projects"."visibility_level", "projects"."archived", "projects"."avatar", "projects"."merge_requests_template", "projects"."star_count", "projects"."merge_requests_rebase_enabled", "projects"."import_type", "projects"."import_source", "projects"."approvals_before_merge", "projects"."reset_approvals_on_push", "projects"."merge_requests_ff_only_enabled", "projects"."issues_template", "projects"."mirror", "projects"."mirror_last_update_at", "projects"."mirror_last_successful_update_at", "projects"."mirror_user_id", "projects"."shared_runners_enabled", "projects"."runners_token", "projects"."build_allow_git_fetch", "projects"."build_timeout", "projects"."mirror_trigger_builds", "projects"."pending_delete", "projects"."public_builds", "projects"."last_repository_check_failed", "projects"."last_repository_check_at", "projects"."only_allow_merge_if_pipeline_succeeds", "projects"."has_external_issue_tracker", "projects"."repository_storage", "projects"."repository_read_only", "projects"."request_access_enabled", "projects"."has_external_wiki", "projects"."ci_config_path", "projects"."lfs_enabled", "projects"."description_html", "projects"."only_allow_merge_if_all_discussions_are_resolved", "projects"."repository_size_limit", "projects"."printing_merge_request_link_enabled", "projects"."auto_cancel_pending_pipelines", "projects"."service_desk_enabled", "projects"."cached_markdown_version", "projects"."delete_error", "projects"."last_repository_updated_at", "projects"."disable_overriding_approvers_per_merge_request", "projects"."storage_version", "projects"."resolve_outdated_diff_discussions", "projects"."remote_mirror_available_overridden", "projects"."only_mirror_protected_branches", "projects"."pull_mirror_available_overridden", "projects"."jobs_cache_index", "projects"."external_authorization_classification_label", "projects"."mirror_overwrites_diverged_branches", "projects"."pages_https_only", "projects"."external_webhook_token", "projects"."packages_enabled", "projects"."merge_requests_author_approval", "projects"."pool_repository_id", "projects"."runners_token_encrypted", "projects"."bfg_object_map", "projects"."detected_repository_languages", "projects"."merge_requests_disable_committers_approval", "projects"."require_password_to_approve", "projects"."max_pages_size", "projects"."max_artifacts_size", "projects"."pull_mirror_branch_prefix", "projects"."remove_source_branch_after_merge", "projects"."marked_for_deletion_at", "projects"."marked_for_deletion_by_user_id", "projects"."autoclose_referenced_issues", "projects"."suggestion_commit_message", "projects"."project_namespace_id", "projects"."hidden", "projects"."organization_id" FROM "projects" WHERE "projects"."id" IN (4894834, 5351075, 5373222, 5417931, 5498087, 5576532, 5617895, 5742170, 5779760, 5945915, 5976131, 5994307, 6130122, 6168240, 6802300, 9762266, 11146000, 11428501, 12006272, 12724255, 13467157, 14235835, 15232184, 15677708, 15841525, 16597098, 16683102, 18042963, 20145500, 20785013, 22441624, 23992616, 24111720, 24112430, 24249085, 26588735, 26751150, 26893673, 27038823, 27060148, 28274618, 28274632, 33931271, 37734140, 43777063, 58060746, 59029330, 60325468, 61151383)
SELECT sbom_occurrences

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/32396/commands/100063

SELECT "sbom_occurrences".*
FROM "sbom_occurrences"
WHERE (traversal_ids >= ('{9970, 2452873}')) AND (traversal_ids < ('{9970, 2452874}')) AND
  "sbom_occurrences"."component_version_id" = 5510816967

Related to #472113 (closed)

Edited by Lucas Charles

Merge request reports

Loading