diff options
author | Eugene Kenny <elkenny@gmail.com> | 2017-04-24 21:10:47 +0100 |
---|---|---|
committer | Eugene Kenny <elkenny@gmail.com> | 2017-07-06 14:21:07 +0100 |
commit | 6658e3746b236f84e27e711fced6a83b187ad2b1 (patch) | |
tree | 7b6fb771bdb70425016a6a8f59883d0ea8a773ba /activerecord/test/cases/calculations_test.rb | |
parent | c8ce3459648ce0f86646b564ce1c0bb16a4b48eb (diff) | |
download | rails-6658e3746b236f84e27e711fced6a83b187ad2b1.tar.gz rails-6658e3746b236f84e27e711fced6a83b187ad2b1.tar.bz2 rails-6658e3746b236f84e27e711fced6a83b187ad2b1.zip |
Skip query cache for in_batches and friends
The `find_each`, `find_in_batches` and `in_batches` APIs usually operate
on large numbers of records, where it's preferable not to load them all
into memory at once.
If the query cache is enabled, it will hold onto the query results until
the end of the execution context (request/job), which means the memory
used is still proportional to the total number of records. These queries
are typically not repeated, so the query cache isn't desirable here.
Diffstat (limited to 'activerecord/test/cases/calculations_test.rb')
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 80baaac30a..7d6dc21e34 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -817,4 +817,46 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal 6, Account.sum(:firm_id) { 1 } end end + + test "#skip_query_cache! for #pluck" do + Account.cache do + assert_queries(1) do + Account.pluck(:credit_limit) + Account.pluck(:credit_limit) + end + + assert_queries(2) do + Account.all.skip_query_cache!.pluck(:credit_limit) + Account.all.skip_query_cache!.pluck(:credit_limit) + end + end + end + + test "#skip_query_cache! for a simple calculation" do + Account.cache do + assert_queries(1) do + Account.calculate(:sum, :credit_limit) + Account.calculate(:sum, :credit_limit) + end + + assert_queries(2) do + Account.all.skip_query_cache!.calculate(:sum, :credit_limit) + Account.all.skip_query_cache!.calculate(:sum, :credit_limit) + end + end + end + + test "#skip_query_cache! for a grouped calculation" do + Account.cache do + assert_queries(1) do + Account.group(:firm_id).calculate(:sum, :credit_limit) + Account.group(:firm_id).calculate(:sum, :credit_limit) + end + + assert_queries(2) do + Account.all.skip_query_cache!.group(:firm_id).calculate(:sum, :credit_limit) + Account.all.skip_query_cache!.group(:firm_id).calculate(:sum, :credit_limit) + end + end + end end |