debian-mirror-gitlab/lib/gitlab/database/batch_count.rb

60 lines
2.7 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
# For large tables, PostgreSQL can take a long time to count rows due to MVCC.
2022-07-23 23:45:48 +05:30
# Implements:
# - distinct batch counter
# - ordinary batch counter
# - sum batch counter
# - average batch counter
2020-03-13 15:44:24 +05:30
# Needs indexes on the column below to calculate max, min and range queries
# For larger tables just set use higher batch_size with index optimization
2020-04-22 19:07:51 +05:30
#
# In order to not use a possible complex time consuming query when calculating min and max for batch_distinct_count
# the start and finish can be sent specifically
#
2021-01-03 14:25:43 +05:30
# Grouped relations can be used as well. However, the preferred batch count should be around 10K because group by count is more expensive.
#
2020-03-13 15:44:24 +05:30
# See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/22705
2020-04-22 19:07:51 +05:30
#
2020-03-13 15:44:24 +05:30
# Examples:
# extend ::Gitlab::Database::BatchCount
# batch_count(User.active)
# batch_count(::Clusters::Cluster.aws_installed.enabled, :cluster_id)
2021-01-03 14:25:43 +05:30
# batch_count(Namespace.group(:type))
2020-03-13 15:44:24 +05:30
# batch_distinct_count(::Project, :creator_id)
2021-09-30 23:02:18 +05:30
# batch_distinct_count(::Project.aimed_for_deletion.service_desk_enabled.where(time_period), start: ::User.minimum(:id), finish: ::User.maximum(:id))
2021-01-03 14:25:43 +05:30
# batch_distinct_count(Project.group(:visibility_level), :creator_id)
2020-10-24 23:57:45 +05:30
# batch_sum(User, :sign_in_count)
2021-01-03 14:25:43 +05:30
# batch_sum(Issue.group(:state_id), :weight))
2022-07-23 23:45:48 +05:30
# batch_average(Ci::Pipeline, :duration)
# batch_average(MergeTrain.group(:status), :duration)
2020-03-13 15:44:24 +05:30
module Gitlab
module Database
module BatchCount
2020-04-22 19:07:51 +05:30
def batch_count(relation, column = nil, batch_size: nil, start: nil, finish: nil)
BatchCounter.new(relation, column: column).count(batch_size: batch_size, start: start, finish: finish)
2020-03-13 15:44:24 +05:30
end
2022-10-11 01:57:18 +05:30
def batch_count_with_timeout(relation, column = nil, batch_size: nil, start: nil, finish: nil, timeout: nil, partial_results: nil)
BatchCounter.new(relation, column: column).count_with_timeout(batch_size: batch_size, start: start, finish: finish, timeout: timeout, partial_results: partial_results)
end
2020-04-22 19:07:51 +05:30
def batch_distinct_count(relation, column = nil, batch_size: nil, start: nil, finish: nil)
BatchCounter.new(relation, column: column).count(mode: :distinct, batch_size: batch_size, start: start, finish: finish)
2020-03-13 15:44:24 +05:30
end
2020-10-24 23:57:45 +05:30
def batch_sum(relation, column, batch_size: nil, start: nil, finish: nil)
BatchCounter.new(relation, column: nil, operation: :sum, operation_args: [column]).count(batch_size: batch_size, start: start, finish: finish)
end
2022-07-23 23:45:48 +05:30
def batch_average(relation, column, batch_size: nil, start: nil, finish: nil)
2022-10-11 01:57:18 +05:30
BatchAverageCounter.new(relation, column).count(batch_size: batch_size)
2022-07-23 23:45:48 +05:30
end
2020-03-13 15:44:24 +05:30
class << self
include BatchCount
end
end
end
end