Paginated query timeout on API groups list
During global enable of use_traversal_ids_groups_finder
feature flag it was discovered that certain queries resulted in SQL query timeouts. The query that times out is a count of the records returned in the response.
SELECT
COUNT(count_column)
FROM
(
WITH "descendants_base_cte" AS MATERIALIZED (
SELECT
"namespaces"."traversal_ids",
"namespaces"."id"
FROM
"namespaces"
INNER JOIN "members" ON "namespaces"."id" = "members"."source_id"
WHERE
"members"."type" = $3
AND "members"."source_type" = $4
AND "namespaces"."type" = $5
AND "members"."user_id" = $6
AND "members"."requested_at" IS NULL
AND (access_level >= $7)
AND (members.access_level >= $8)
),
"superset" AS (
SELECT
d1.traversal_ids
FROM
descendants_base_cte d1
WHERE
NOT EXISTS (
SELECT
$9
FROM
descendants_base_cte d2
WHERE
d2.id = ANY(d1.traversal_ids)
AND d2.id <> d1.id
)
)
SELECT
DISTINCT id AS count_column
FROM
"superset",
"namespaces"
WHERE
"namespaces"."type" = $1
AND next_traversal_ids_sibling("superset"."traversal_ids") > "namespaces"."traversal_ids"
AND "superset"."traversal_ids" <= "namespaces"."traversal_ids"
LIMIT
$2
) subquery_for_count
Edited by Alex Pooley