Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find_in_batches works only with integer-based 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, seeing the documentation, saying it only works with integer-based primary key. I went through the code, and I see it just order records by "primary_key ASC". Why it doesn't work with non-integer based primary key? Just because of this ordering? I tried my model with this method, it works okay.

Could anyone explain me about this?

like image 948
Chamnap Avatar asked Jun 17 '12 15:06

Chamnap


1 Answers

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

Internally, on each step it gets id of last record (last_id) and selects 1000 records with id greater than last_id on next step. So if application creates new record with unique id < last_id during the processing step, this record will be excluded from processing.

like image 188
dimuch Avatar answered Nov 03 '22 15:11

dimuch