Skip to content

Draft: Add index on members source and role

mo khan requested to merge mokhax/498688/add-index into master

What does this MR do and why?

Before:

SELECT "members"."access_level", COUNT(*) AS members_count
FROM
  (
    (
      SELECT
        "members"."id",
        "members"."access_level",
        "members"."source_id",
        "members"."source_type",
        "members"."user_id",
        "members"."notification_level",
        "members"."type",
        "members"."created_at",
        "members"."updated_at",
        "members"."created_by_id",
        "members"."invite_email",
        "members"."invite_token",
        "members"."invite_accepted_at",
        "members"."requested_at",
        "members"."expires_at",
        "members"."ldap",
        "members"."override",
        "members"."state",
        "members"."invite_email_success",
        "members"."member_namespace_id",
        "members"."member_role_id",
        "members"."expiry_notified_at",
        "members"."request_accepted_at"
      FROM "members"
      WHERE "members"."source_type" = 'Namespace'
        AND "members"."source_id" IN (
          SELECT "namespaces"."id"
          FROM "namespaces"
          WHERE "namespaces"."type" = 'Group'
            AND (traversal_ids @> ('{70960922}'))
        )
    )
    UNION
    (
      SELECT
        "members"."id",
        "members"."access_level",
        "members"."source_id",
        "members"."source_type",
        "members"."user_id",
        "members"."notification_level",
        "members"."type",
        "members"."created_at",
        "members"."updated_at",
        "members"."created_by_id",
        "members"."invite_email",
        "members"."invite_token",
        "members"."invite_accepted_at",
        "members"."requested_at",
        "members"."expires_at",
        "members"."ldap",
        "members"."override",
        "members"."state",
        "members"."invite_email_success",
        "members"."member_namespace_id",
        "members"."member_role_id",
        "members"."expiry_notified_at",
        "members"."request_accepted_at"
      FROM "members"
      WHERE "members"."source_type" = 'Project'
        AND "members"."source_id" IN (
          SELECT "projects"."id"
          FROM "projects"
          WHERE "projects"."namespace_id" IN (
              SELECT namespaces.traversal_ids [array_length(namespaces.traversal_ids, 1)] AS id
              FROM "namespaces"
              WHERE "namespaces"."type" = 'Group'
                AND (traversal_ids @> ('{70960922}'))
            )
        )
    )
  ) members
WHERE "members"."member_role_id" IS NULL
GROUP BY "members"."access_level"
Time: 6.412 min
  - planning: 6.549 ms
  - execution: 6.412 min
    - I/O read: 6.103 min
    - I/O write: 138.583 ms

Shared buffers:
  - hits: 989059 (~7.50 GiB) from the buffer pool
  - reads: 1405254 (~10.70 GiB) from the OS file cache, including disk I/O
  - dirtied: 81064 (~633.30 MiB)
  - writes: 5242 (~41.00 MiB)

Temp buffers:
  - reads: 56709 (~443.00 MiB)
  - writes: 56716 (~443.10 MiB)

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/33194/commands/102515

After:

SELECT "members"."access_level", COUNT(*) AS members_count
FROM
  (
    (
      SELECT
        "members"."id",
        "members"."access_level",
        "members"."source_id",
        "members"."source_type",
        "members"."user_id",
        "members"."notification_level",
        "members"."type",
        "members"."created_at",
        "members"."updated_at",
        "members"."created_by_id",
        "members"."invite_email",
        "members"."invite_token",
        "members"."invite_accepted_at",
        "members"."requested_at",
        "members"."expires_at",
        "members"."ldap",
        "members"."override",
        "members"."state",
        "members"."invite_email_success",
        "members"."member_namespace_id",
        "members"."member_role_id",
        "members"."expiry_notified_at",
        "members"."request_accepted_at"
      FROM "members"
      WHERE "members"."source_type" = 'Namespace'
        AND "members"."source_id" IN (
          SELECT "namespaces"."id"
          FROM "namespaces"
          WHERE "namespaces"."type" = 'Group'
            AND (traversal_ids @> ('{70960922}'))
        )
    )
    UNION
    (
      SELECT
        "members"."id",
        "members"."access_level",
        "members"."source_id",
        "members"."source_type",
        "members"."user_id",
        "members"."notification_level",
        "members"."type",
        "members"."created_at",
        "members"."updated_at",
        "members"."created_by_id",
        "members"."invite_email",
        "members"."invite_token",
        "members"."invite_accepted_at",
        "members"."requested_at",
        "members"."expires_at",
        "members"."ldap",
        "members"."override",
        "members"."state",
        "members"."invite_email_success",
        "members"."member_namespace_id",
        "members"."member_role_id",
        "members"."expiry_notified_at",
        "members"."request_accepted_at"
      FROM "members"
      WHERE "members"."source_type" = 'Project'
        AND "members"."source_id" IN (
          SELECT "projects"."id"
          FROM "projects"
          WHERE "projects"."namespace_id" IN (
              SELECT namespaces.traversal_ids [array_length(namespaces.traversal_ids, 1)] AS id
              FROM "namespaces"
              WHERE "namespaces"."type" = 'Group'
                AND (traversal_ids @> ('{70960922}'))
            )
        )
    )
  ) members
WHERE "members"."member_role_id" IS NULL
GROUP BY "members"."access_level"

References

Please include cross links to any resources that are relevant to this MR This will give reviewers and future readers helpful context to give an efficient review of the changes introduced.

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.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

Edited by mo khan

Merge request reports

Loading