Move Clusters::Applications::Prometheus to batch counting
What does this MR do?
Move ::Clusters::Applications::Prometheus.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
time_period = {}
clusters_applications_cert_managers: distinct_count(::Clusters::Applications::Prometheus.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::Prometheus.where(time_period).available.joins(:cluster), 'clusters.user_id'),
Queries
MIN with no period
SELECT MIN(clusters.user_id)
FROM "clusters_applications_prometheus"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_prometheus"."cluster_id"
WHERE "clusters_applications_prometheus"."status" IN (3, 5)
https://explain.depesz.com/s/qjLI
Time: 155.702 msFilter: (clusters_applications_prometheus.status = ANY ('{3,5}'::integer[]))
MAX with no period
SELECT MAX(clusters.user_id)
FROM "clusters_applications_prometheus"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_prometheus"."cluster_id"
WHERE "clusters_applications_prometheus"."status" IN (3, 5)
https://explain.depesz.com/s/Fb3A
Time: 23.431 msFilter: (clusters_applications_prometheus.status = ANY ('{3,5}'::integer[]))
COUNT with no period
SELECT COUNT(DISTINCT clusters.user_id)
FROM "clusters_applications_prometheus"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_prometheus"."cluster_id"
WHERE "clusters_applications_prometheus"."status" IN (3,5)
AND "clusters"."user_id" BETWEEN 0 AND 9999
https://explain.depesz.com/s/p18N
Time: 1.135 msFilter: (clusters_applications_prometheus.status = ANY ('{3,5}'::integer[]))
MIN with period
SELECT MIN(clusters.user_id)
FROM "clusters_applications_prometheus"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_prometheus"."cluster_id"
WHERE "clusters_applications_prometheus"."created_at" BETWEEN '2020-03-10 08:10:24.409482' AND '2020-04-07 08:10:24.409745'
AND "clusters_applications_prometheus"."status" IN (3, 5)
https://explain.depesz.com/s/uk3K
Time: 6.471 msFilter: ((clusters_applications_prometheus.created_at >= '2020-03-10 08:10:24.409482+00'::timestamp with time zone) AND (clusters_applications_prometheus.created_at <= '2020-04-07 08:10:24.409745+00'::timestamp with time zone) AND (clusters_applications_prometheus.status = ANY ('{3,5}'::integer[])))
MAX with period
SELECT MAX(clusters.user_id)
FROM "clusters_applications_prometheus"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_prometheus"."cluster_id"
WHERE "clusters_applications_prometheus"."created_at" BETWEEN '2020-03-10 08:10:24.409482' AND '2020-04-07 08:10:24.409745'
AND "clusters_applications_prometheus"."status" IN (3, 5)
https://explain.depesz.com/s/0iEE
Time: 3.825 msFilter: Filter: ((clusters_applications_prometheus.created_at >= '2020-03-10 08:10:24.409482+00'::timestamp with time zone) AND (clusters_applications_prometheus.created_at <= '2020-04-07 08:10:24.409745+00'::timestamp with time zone) AND (clusters_applications_prometheus.status = ANY ('{3,5}'::integer[])))
COUNT with period
SELECT COUNT(DISTINCT clusters.user_id)
FROM "clusters_applications_prometheus"
INNER JOIN "clusters" ON "clusters"."id" = "clusters_applications_prometheus"."cluster_id"
WHERE "clusters_applications_prometheus"."created_at" BETWEEN '2020-03-10 08:10:24.409482' AND '2020-04-07 08:10:24.409745'
AND "clusters_applications_prometheus"."status" IN (3, 5)
AND "clusters"."user_id" BETWEEN 0 AND 9999
https://explain.depesz.com/s/GUa0
Time: 1.165 msFilter: ((clusters_applications_prometheus.created_at >= '2020-03-10 08:10:24.409482+00'::timestamp with time zone) AND (clusters_applications_prometheus.created_at <= '2020-04-07 08:10:24.409745+00'::timestamp with time zone) AND (clusters_applications_prometheus.status = ANY ('{3,5}'::integer[])))
Part of #212962 (closed)