This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SomeModel.where(some_variable: %i(some_value another_value yet_another_value)).where(“some_date <= ?”, Time.zone.today).select(:id) | |
.find_each_with_order(additional_order_column: :some_variable) do |some_model_id| | |
# do something | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT “some_table”.”id” FROM “some_table” WHERE (some_variable IN (‘some_value’, ‘another_value’, ‘yet_another_value’) AND ((some_date <= ‘2016–05–01’ AND DATE_TRUNC(‘day’, updated_at) <= some_date) AND (“some_table”.”id” > 133891263636178370) ORDER BY “some_table”.”id” ASC, “some_table”.”some_variable” ASC LIMIT 1000; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
some_table_some_index_query AS ( | |
SELECT “some_table”.”id” FROM “some_table” WHERE (some_variable IN (‘some_value’, ‘another_value’, ‘yet_another_value’) AND ((some_date <= ‘2016–05–01’ AND DATE_TRUNC(‘day’, updated_at) <= some_date) | |
) | |
SELECT * FROM “some_table_some_index_query” WHERE (“some_table”.”id” > 123456789) ORDER BY “id” ASC LIMIT 1000; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
moneybird_app=> EXPLAIN ANALYZE SELECT “some_table”.”id” FROM “some_table” WHERE (some_variable IN (‘some_value’, ‘another_value’, ‘yet_another_value’) AND ((some_date <= ‘2016–05–01’ AND DATE_TRUNC(‘day’, updated_at) <= some_date) AND (“some_table”.”id” > 123456789) ORDER BY “some_table”.”id” ASC; | |
— REMOVED DETAILS | |
Bitmap Index Scan on some_table_index (cost=0.00..1362.84 rows=53272 width=0) (actual time=30.145..30.145 rows=19102 loops=1) | |
— REMOVED DETAILS | |
Planning time: 0.527 ms | |
Execution time: 4533.796 ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
moneybird_app=> ANALYZE some_table; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
moneybird_app=> EXPLAIN ANALYZE SELECT “some_table”.”id” FROM “some_table” WHERE (some_variable IN (‘some_value’, ‘another_value’, ‘yet_another_value’) AND ((some_date <= ‘2016–05–01’ AND DATE_TRUNC(‘day’, updated_at) <= some_date) AND (“some_table”.”id” > 133891263636178370) ORDER BY “some_table”.”id” ASC LIMIT 1000; | |
— REMOVED DETAILS | |
Index Scan using some_table_pkey on some_table (cost=0.43..1295055.42 rows=75277 width=8) (actual time=0.771..35490.356 rows=1000 loops=1) | |
— REMOVED DETAILS | |
Planning time: 2.049 ms | |
Execution time: 35521.964 ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE INDEX some_table_some_index ON some_table USING btree (some_variable, some_date) WHERE some_variable IN ('some_value', 'another_value', 'yet_another_value') AND DATE_TRUNC('day', updated_at) <= some_date; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
moneybird_app=> EXPLAIN ANALYZE SELECT "some_table"."id" FROM "some_table" WHERE (some_variable IN ('some_value', 'another_value', 'yet_another_value') AND ((some_date <= '2016-05-01' AND DATE_TRUNC('day', updated_at) <= some_date) AND ("some_table"."id" > 123456789) ORDER BY "some_table"."id" ASC LIMIT 1000; | |
-- REMOVED DETAILS | |
Index Scan using some_table_pkey on some_table (cost=0.43..1295055.42 rows=75277 width=8) (actual time=0.771..36133.574 rows=1000 loops=1) | |
-- REMOVED DETAILS | |
Planning time: 2.200 ms | |
Execution time: 36134.375 ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveRecord | |
module Batches | |
def find_each_with_order(options = {}) | |
if block_given? | |
find_in_batches_with_order(options) do |records| | |
records.each { |record| yield record } | |
end | |
else | |
enum_for :find_each, options do | |
options[:start] ? where(table[primary_key].gteq(options[:start])).size : size |