Dev.to
6/26/2026

You Wanted a Number, but Loaded 500,000 Rows Into Memory
Short summary
Rails developers commonly load entire tables into memory when they only need a single number, causing OOM issues at scale. The culprit is using Ruby Array methods (like `.select { block }`, `.present?`, `.sum { block }`) instead of database operations. The fix: use `.count` for always-DB queries, `.size` for smart behavior, `.exists?` with LIMIT 1 for checks, and `.sum(:column)` instead of `.sum { }` blocks.
- •Using Ruby Array methods instead of ActiveRecord causes entire table loads into memory—common Rails trap at scale
- •Key methods differ: .count always queries DB, .size is smart (DB if not loaded, memory if loaded), .exists? uses LIMIT 1
- •Aggregations must use column names like .sum(:col) not blocks like .sum { } to push computation to the database
Generated with AI, which can make mistakes.
Is this a good recommendation for you?



