I have a select statement,
SELECT `metrics`.*
FROM `metrics`
WHERE `metrics`.`engine` = 0
AND (`metrics`.connection_id = 27874)
AND (`metrics`.`id` > 181586235)
ORDER BY `metrics`.`id` ASC LIMIT 1000
which is generated by find_each being called on a relation (e.g. metric.engines.find_each). This select statement scans 15 million records each time it is run.
I’d like to make this lighter on the database by telling the query only to look for a range of metrics.ids (e.g. metrics.id > 1 AND metrics.id < 1000).
Is there a way to do this with find_each?
find_eachcan be applied to scopes like all finder methods:EDIT: If you need to process all records but don’t want to load them all at once, you can use the find_in_batches method: