Optimize service_desk_enabled_projects counter in usage_data
What does this MR do?
Optimize service_desk_enabled_projects
counter
Index added
CREATE INDEX CONCURRENTLY index_service_desk_enabled_projects_on_id_creator_id_and_created_at ON projects USING btree(id, creator_id, created_at)
WHERE "projects"."service_desk_enabled"
the query has been executed. Duration: 6.496 min
Optimization
time_period = { }
project_creator_id_start = ::User.minimum(:id)
project_creator_id_finish = ::User.maximum(:id)
Gitlab::UsageData.distinct_count(::Project.service_desk_enabled.where(time_period), :creator_id, start: project_creator_id_start, finish: project_creator_id_finish)
time_period = { created_at: 28.days.ago..Time.current }
project_creator_id_start = ::User.minimum(:id)
project_creator_id_finish = ::User.maximum(:id)
Gitlab::UsageData.distinct_count(::Project.service_desk_enabled.where(time_period), :creator_id, start: project_creator_id_start, finish: project_creator_id_finish)
Queries
Query 1 MIN no period
SELECT MIN("users"."id")
FROM "users"
https://explain.depesz.com/s/zR4
Before: Time: 7.839 msNo filter
Query 2 MAX no period
SELECT MAX("users"."id")
FROM "users"
https://explain.depesz.com/s/pdHK
Before: Time: 10.718 msno Filter
Query 3 COUNT no period
SELECT COUNT(DISTINCT "projects"."creator_id")
FROM "projects"
WHERE "projects"."service_desk_enabled" = TRUE
AND "projects"."creator_id" BETWEEN 1 AND 10000
https://explain.depesz.com/s/I0Cj
Before: Time: 2.027 minFilter: ((projects.creator_id >= 1) AND (projects.creator_id <= 10000))
https://explain.depesz.com/s/OMDo
After: Time: 484.426 msNo filter
Query 4 COUNT with time period
SELECT COUNT(DISTINCT "projects"."creator_id")
FROM "projects"
WHERE "projects"."service_desk_enabled" = TRUE
AND "projects"."created_at" BETWEEN '2020-02-29 11:20:43.529892' AND '2020-03-28 11:20:43.530006'
AND "projects"."creator_id" BETWEEN 0 AND 9999
Before: Time: 5.701 s https://explain.depesz.com/s/anig
Filter: (projects.service_desk_enabled AND (projects.creator_id >= 0) AND (projects.creator_id <= 9999))
https://explain.depesz.com/s/jOj2
After: Time: 573.991 msConformity
-
Changelog entry -
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Closes #211802 (closed)
Edited by Alina Mihaila