Implement batch counter for summing a column
What does this MR do?
Introduces batch_sum
to Gitlab::Database::BatchCount
. This method iterates over a relation and returns the sum of values in a given column. It uses the same batch iteration logic as batch_count
and batch_distinct_count
.
Usage ping will make use of this method in a follow-up MR. Documentation will be updated to recommend using batch_sum
after we have proved that it works at scale.
Part of #215313 (closed).
Sample queries
[22] pry(main)> Gitlab::Database::BatchCount.batch_sum(User, :sign_in_count, batch_size: 10) # 47 users exist
(0.7ms) SELECT MIN("users"."id") FROM "users"
(0.5ms) SELECT MAX("users"."id") FROM "users"
(0.4ms) SELECT SUM("users"."sign_in_count") FROM "users" WHERE "users"."id" BETWEEN 1 AND 10
(0.3ms) SELECT SUM("users"."sign_in_count") FROM "users" WHERE "users"."id" BETWEEN 11 AND 20
(0.3ms) SELECT SUM("users"."sign_in_count") FROM "users" WHERE "users"."id" BETWEEN 21 AND 30
(0.5ms) SELECT SUM("users"."sign_in_count") FROM "users" WHERE "users"."id" BETWEEN 31 AND 40
(0.4ms) SELECT SUM("users"."sign_in_count") FROM "users" WHERE "users"."id" BETWEEN 41 AND 50
Does this MR meet the acceptance criteria?
Conformity
- [-] Changelog entry
- [-] Documentation (if required)
-
Code review guidelines - [-] Merge request performance guidelines
-
Style guides -
Database guides - [-] Separation of EE specific content
Availability and Testing
- [-] Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process.
- [-] Tested in all supported browsers
- [-] Informed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
- [-] Label as security and @ mention
@gitlab-com/gl-security/appsec
- [-] The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
- [-] Security reports checked/validated by a reviewer from the AppSec team
Edited by Alishan Ladhani