diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-07-07 15:08:24 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-07-07 15:08:24 +0200 |
commit | b7b3aef444bba457b4967ee7fa122527853a2eb5 (patch) | |
tree | d93adead912ef964c791c3a744eee1699f209c43 /doc/development | |
parent | af0eeefc324e96d79c85b337ae9e441947a9f729 (diff) | |
parent | 5f9c84584efd5a7cbe19ada49fdccefbd5f54aea (diff) | |
download | gitlab-ce-b7b3aef444bba457b4967ee7fa122527853a2eb5.tar.gz |
Merge remote-tracking branch 'origin/active-record-each-batch' into fix/gb/stage-id-reference-background-migration
* origin/active-record-each-batch: (59 commits)
Added EachBatch for iterating tables in batches
Extend MR tabs a bit to cover up the avatar holder and collapse icon on scroll
Update VERSION to 9.4.0-pre.
Add CHANGELOG
Fix some N+1 queries in the GET /projects API
Don't show auxiliary blob viewer for README when there is no wiki
Improve & fix the performance bar UI and behavior
Remove orphaned haml files
Fixed CHANGELOG.md for 9.3.4 release
Add table for merge request commits
34727 Remove two columned layout from project member settings
Just draw :legacy_builds
Re-enable polling for environments
Cleanup minor UX issues in the performance dashboard
Upgrade GitLab Workhorse to v2.3.0
Added test for the chart legend
Use correct field for label name, fix default for unit to be blank
Fix shorter route helpers in production environment
Encode certificate-authority-data in base64
Revert "Merge branch 'winh-mr-widget-no-pipeline' into 'master'"
...
Diffstat (limited to 'doc/development')
-rw-r--r-- | doc/development/README.md | 1 | ||||
-rw-r--r-- | doc/development/iterating_tables_in_batches.md | 37 |
2 files changed, 38 insertions, 0 deletions
diff --git a/doc/development/README.md b/doc/development/README.md index a2a07c37ced..58993c52dcd 100644 --- a/doc/development/README.md +++ b/doc/development/README.md @@ -55,6 +55,7 @@ - [Single Table Inheritance](single_table_inheritance.md) - [Background Migrations](background_migrations.md) - [Storing SHA1 Hashes As Binary](sha1_as_binary.md) +- [Iterating Tables In Batches](iterating_tables_in_batches.md) ## i18n diff --git a/doc/development/iterating_tables_in_batches.md b/doc/development/iterating_tables_in_batches.md new file mode 100644 index 00000000000..590c8cbba2d --- /dev/null +++ b/doc/development/iterating_tables_in_batches.md @@ -0,0 +1,37 @@ +# Iterating Tables In Batches + +Rails provides a method called `in_batches` that can be used to iterate over +rows in batches. For example: + +```ruby +User.in_batches(of: 10) do |relation| + relation.update_all(updated_at: Time.now) +end +``` + +Unfortunately this method is implemented in a way that is not very efficient, +both query and memory usage wise. + +To work around this you can include the `EachBatch` module into your models, +then use the `each_batch` class method. For example: + +```ruby +class User < ActiveRecord::Base + include EachBatch +end + +User.each_batch(of: 10) do |relation| + relation.update_all(updated_at: Time.now) +end +``` + +This will end up producing queries such as: + +``` +User Load (0.7ms) SELECT "users"."id" FROM "users" WHERE ("users"."id" >= 41654) ORDER BY "users"."id" ASC LIMIT 1 OFFSET 1000 + (0.7ms) SELECT COUNT(*) FROM "users" WHERE ("users"."id" >= 41654) AND ("users"."id" < 42687) +``` + +The API of this method is similar to `in_batches`, though it doesn't support +all of the arguments that `in_batches` supports. You should always use +`each_batch` _unless_ you have a specific need for `in_batches`. |