Commit d8729627 authored by Imre Farkas's avatar Imre Farkas

Merge branch '208923-enable-batch-counting-for-some-individual-queries-6' into 'master'

Optimize usage ping queries by using batch counting

See merge request gitlab-org/gitlab!27455
parents 0eef93f9 3f27fcf3
---
title: Optimize usage ping queries by using batch counting
merge_request: 27455
author:
type: performance
......@@ -2,6 +2,7 @@
class Packages::Package < ApplicationRecord
include Sortable
include Gitlab::SQL::Pattern
include UsageStatistics
belongs_to :project
# package_files must be destroyed by ruby code in order to properly remove carrierwave uploads and update project statistics
......
# frozen_string_literal: true
class UsersOpsDashboardProject < ApplicationRecord
include UsageStatistics
belongs_to :project
belongs_to :user
validates :user, presence: true
validates :user_id, uniqueness: { scope: [:project_id] }
validates :project, presence: true
def self.distinct_users(users)
select('distinct user_id').joins(:user).merge(users)
end
end
......@@ -129,7 +129,7 @@ module EE
def operations_dashboard_usage
users_with_ops_dashboard_as_default = count(::User.active.with_dashboard('operations'))
users_with_projects_added = count(UsersOpsDashboardProject.distinct_users(::User.active), batch: false)
users_with_projects_added = distinct_count(UsersOpsDashboardProject.joins(:user).merge(::User.active), :user_id) # rubocop:disable CodeReuse/ActiveRecord
{
operations_dashboard_default_dashboard: users_with_ops_dashboard_as_default,
......@@ -154,7 +154,7 @@ module EE
merge_requests_with_required_codeowners: distinct_count(::ApprovalMergeRequestRule.code_owner_approval_required, :merge_request_id),
projects_mirrored_with_pipelines_enabled: count(::Project.mirrored_with_enabled_pipelines),
projects_reporting_ci_cd_back_to_github: count(::GithubService.without_defaults.active),
projects_with_packages: count(::Packages::Package.select('distinct project_id'), batch: false),
projects_with_packages: distinct_count(::Packages::Package, :project_id),
projects_with_prometheus_alerts: distinct_count(PrometheusAlert, :project_id),
projects_with_tracing_enabled: count(ProjectTracingSetting),
template_repositories: count(::Project.with_repos_templates) + count(::Project.with_groups_level_repos_templates)
......@@ -253,7 +253,7 @@ module EE
clusters: distinct_count(::Clusters::Cluster.where(time_period), :user_id),
clusters_applications_prometheus: ::Clusters::Applications::Prometheus.where(time_period).distinct_by_user,
operations_dashboard_default_dashboard: count(::User.active.with_dashboard('operations').where(time_period)),
operations_dashboard_users_with_projects_added: count(UsersOpsDashboardProject.distinct_users(::User.active).where(time_period), batch: false),
operations_dashboard_users_with_projects_added: distinct_count(UsersOpsDashboardProject.joins(:user).merge(::User.active).where(time_period), :user_id),
projects_prometheus_active: distinct_count(::Project.with_active_prometheus_service.where(time_period), :creator_id),
projects_with_error_tracking_enabled: distinct_count(::Project.with_enabled_error_tracking.where(time_period), :creator_id),
projects_with_tracing_enabled: distinct_count(::Project.with_tracing_enabled.where(time_period), :creator_id)
......
......@@ -218,9 +218,9 @@ module Gitlab
results[:projects_jira_server_active] += counts[:server].count if counts[:server]
results[:projects_jira_cloud_active] += counts[:cloud].count if counts[:cloud]
if results[:projects_jira_active] == -1
results[:projects_jira_active] = count(services, batch: false)
results[:projects_jira_active] = services.size
else
results[:projects_jira_active] += count(services, batch: false)
results[:projects_jira_active] += services.size
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment