Search code examples
rubydatamapperruby-datamapper

What's the most efficient way to iterate through an entire table using Datamapper?


What's the most efficient way to iterate through an entire table using Datamapper?

If I do this, does Datamapper try to pull the entire result set into memory before performing the iteration? Assume, for the sake of argument, that I have millions of records and that this is infeasible:

Author.all.each do |a|
  puts a.title
end

Is there a way that I can tell Datamapper to load the results in chunks? Is it smart enough to know to do this automatically?


Solution

  • Datamapper will run just one sql query for the example above so it will have to keep the whole result set in memory.

    I think you should use some sort of pagination if your collection is big. Using dm-pagination you could do something like:

    PAGE_SIZE = 20
    pager = Author.page(:per_page => PAGE_SIZE).pager # This will run a count query
    (1..pager.total_pages).each do |page_number|
      Author.page(:per_page => PAGE_SIZE, :page => page_number).each do |a|
        puts a.title
      end
    end
    

    You can play around with different values for PAGE_SIZE to find a good trade-off between the number of sql queries and memory usage.