Skip to content

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)

Time: 155.702 ms https://explain.depesz.com/s/qjLI

Filter: (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)

Time: 23.431 ms https://explain.depesz.com/s/Fb3A

Filter: (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

Time: 1.135 ms https://explain.depesz.com/s/p18N

Filter: (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)

Time: 6.471 ms https://explain.depesz.com/s/uk3K

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[])))

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)

Time: 3.825 ms https://explain.depesz.com/s/0iEE

Filter: 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

Time: 1.165 ms https://explain.depesz.com/s/GUa0

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[])))

Part of #212962 (closed)

Edited by Alina Mihaila

Merge request reports