Skip to content

Make `ActiveRecord::Batches#find_each` to not return `self`. #1300

Closed
wants to merge 4 commits into from

4 participants

@knapo
knapo commented May 25, 2011

This caused that find_each may produce extra db call taking all the records from db, return array with all records, and was less efficient than ActiveRecord::Base#all. There is no sense to keep scope with this method, as it expects block and having further scopes after find_each is not possible.

eg. (notice last query)

ruby-1.9.2-p180 :016 > User.find_each{|u| }
  User Load (93.8ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` >= 0) ORDER BY `users`.`id` ASC LIMIT 1000
  User Load (175.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` > 1000) ORDER BY `users`.`id` ASC LIMIT 1000
  User Load (0.5ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` > 2000) ORDER BY `users`.`id` ASC LIMIT 1000
  User Load (308.2ms)  SELECT `users`.* FROM `users` 
knapo added some commits May 25, 2011
@knapo knapo Make `ActiveRecord::Batches#find_each` to not return `self`.
This caused that `find_each` was producing extra db call taking all the records from db, and was less efficient than `ActiveRecord::Base#all`.
9ddf699
@knapo knapo Merge remote branch 'upstream/master' 9561094
@knapo
knapo commented May 25, 2011

Sorry for merge commit - did merge instead of pull --rebase :/

@dmathieu

Test ?

@knapo
knapo commented May 25, 2011

If you're not able to combine commits (dunno if it's possible with pull request), I can provide a patch with both of them.

@dasch dasch and 1 other commented on an outdated diff May 25, 2011
activerecord/test/cases/batches_test.rb
@@ -18,6 +18,13 @@ class EachTest < ActiveRecord::TestCase
end
end
+ def test_each_should_not_return_query_chain_and_excecute_only_one_query
@dasch
dasch added a note May 25, 2011

Typo: should be "execute", not "excecute".

@knapo
knapo added a note May 26, 2011

thanks dasch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@knapo
knapo commented May 31, 2011

ping. can you merge this?

@spastorino
Ruby on Rails member

Can you first squash all the commits into one?, thanks

@knapo
knapo commented Jul 7, 2011

@spastorino - I created new pull request with squashed commits: #1997

@knapo knapo closed this Jul 7, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.