Does Find_in_batches only work with whole primary keys?

My ActiveRecord models use uuid-based primary keys, and I want to use find_in_batches to load 1000 records at a time. However, after seeing the documentation , he says that he works only with the whole key. I looked at the code and I see that it just writes the entries for " primary_key ASC ". Why does this not work with an inappropriate base key? Just because of this order? I tried my model using this method, it works fine.

Can someone explain to me about this?

+6
source share
1 answer

Guess the documentation is not 100% consistent. It works correctly with an incremental primary key. If you can guarantee that the uuid of any new record will be greater than the key of any existing record in the table, it will work correctly. Otherwise, you have a chance to skip new entries added after the start of batch processing.

Inside, at each step, it receives the identifier of the last record ( last_id ) and in the next step selects 1000 records with an identifier greater than last_id . Therefore, if the application creates a new record with a unique identifier < last_id at the processing stage, this record will be excluded from processing.

+7
source

Source: https://habr.com/ru/post/918289/


All Articles