Move Clusters::Application::Helm distinct_by_user to batch counting
What does this MR do?
Move ::Clusters::Applications::Helm.where(time_period).distinct_by_user,
to batch distinct count
The purpose of the change
This changes are a performance improvement of count queries and a refactoring. We prefer to use batch counting and optimize indexes if is the case in order to have the queries under 1 sec and avoid the timeouts.
Proposed solution
- Refactor the counter to use batch counting
- Ensure that the resulted queries are < 1 sec in database-lab
- No need for optimization
Queries
time_period = {}
clusters_applications_cert_managers: distinct_count(::Clusters::Applications::Helm.where(time_period).available.joins(:cluster), 'clusters.user_id'),
time_period = { created_at: 28.days.ago..Time.current }
clusters_applications_cert_managers: distinct_count(::Clusters::Applications::Helm.where(time_period).available.joins(:cluster), 'clusters.user_id'),
MIN with no period
SELECT MIN(clusters.user_id)
FROM "clusters_applications_helm"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_helm"."cluster_id"
WHERE "clusters_applications_helm"."status" IN (3,5)
https://explain.depesz.com/s/1rXF
Time: 244.331 msFilter: (clusters_applications_helm.status = ANY ('{3,5}'::integer[]))
MAX with no period
SELECT MAX(clusters.user_id)
FROM "clusters_applications_helm"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_helm"."cluster_id"
WHERE "clusters_applications_helm"."status" IN (3,5)
https://explain.depesz.com/s/YVI0
Time: 45.696 msFilter: (clusters_applications_helm.status = ANY ('{3,5}'::integer[]))
COUNT with no period
SELECT COUNT(DISTINCT clusters.user_id)
FROM "clusters_applications_helm"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_helm"."cluster_id"
WHERE "clusters_applications_helm"."status" IN (3,5)
AND "clusters"."user_id" BETWEEN 0 AND 9999
https://explain.depesz.com/s/5YTS
Time: 2.206 msFilter: (clusters_applications_helm.status = ANY ('{3,5}'::integer[]))
MIN with period
SELECT MIN(clusters.user_id)
FROM "clusters_applications_helm"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_helm"."cluster_id"
WHERE "clusters_applications_helm"."created_at" BETWEEN '2020-03-10 05:47:46.901004' AND '2020-04-07 05:47:46.901175'
AND "clusters_applications_helm"."status" IN (3,5)
https://explain.depesz.com/s/3GTz
Time: 20.157 msFilter: Filter: ((clusters_applications_helm.created_at >= '2020-03-10 05:47:46.901004+00'::timestamp with time zone) AND (clusters_applications_helm.created_at <= '2020-04-07 05:47:46.901175+00'::timestamp with time zone) AND (clusters_applications_helm.status = ANY ('{3,5}'::integer[])))
MAX with period
SELECT MAX(clusters.user_id)
FROM "clusters_applications_helm"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_helm"."cluster_id"
WHERE "clusters_applications_helm"."created_at" BETWEEN '2020-03-10 05:47:46.901004' AND '2020-04-07 05:47:46.901175'
AND "clusters_applications_helm"."status" IN (3,5)
https://explain.depesz.com/s/YqDh
Time: 23.904 msFilter: ((clusters_applications_helm.created_at >= '2020-03-10 05:47:46.901004+00'::timestamp with time zone) AND (clusters_applications_helm.created_at <= '2020-04-07 05:47:46.901175+00'::timestamp with time zone) AND (clusters_applications_helm.status = ANY ('{3,5}'::integer[])))
COUNT with period
SELECT COUNT(DISTINCT clusters.user_id)
FROM "clusters_applications_helm"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_helm"."cluster_id"
WHERE "clusters_applications_helm"."created_at" BETWEEN '2020-03-10 05:47:46.901004' AND '2020-04-07 05:47:46.901175'
AND "clusters_applications_helm"."status" IN (3,5)
AND "clusters"."user_id" BETWEEN 0 AND 9999
https://explain.depesz.com/s/Z0AW
Time: 1.292 msFilter: ((clusters_applications_helm.created_at >= '2020-03-10 05:47:46.901004+00'::timestamp with time zone) AND (clusters_applications_helm.created_at <= '2020-04-07 05:47:46.901175+00'::timestamp with time zone) AND (clusters_applications_helm.status = ANY ('{3,5}'::integer[]))) Rows Removed by Filter: 1
Part of #212962 (closed)