I highly recommend using integers as the primary key id. It will be much more performant than using uuids.
How to go about chunkByID method with UUID primary keys?
Hello! I am having problems trying do this type of query:
Entity::where('type', 'value')->chunk(100, function ($elements) {
foreach($elements as $element){
//some editions here
}
});
The problem: I am trying to use chunk, chunkById, cursor and lazy methods. Everything returns one half part of content. When I use the method count I can find the correct number. I saw that chunkById is the most correct for it according to laravel documentation, but I think the problem here is the use of UUID as primary keys.
Did someone have a situation like this?
Good. I've been researching and I realized that chunkById is the most suitable method when we need to remove or update the models that we're going to go through with foreach. The chunkById problem in my case is the use of the UUID as the primary key. This type of key does not allow for proper searching. In this scenario, the most appropriate thing is to make an initial count of how many elements match your criteria in the query and use this value for a loop where each iteration takes the first occurrence of the element and then does what it needs.
function exampleWithoutChunk(){
$collectionsCount = Entity::where('conditions here ...')
->with('joins here ...')
->count();
for($i=0;$i < $collectionsCount; $i++){
$myEntity = Entity::where('conditions here ...')
->with('joins here ...')
->get()
->first();
$myEntity->method();
}
}
It is obvious that if you have a lot of elements, it will use a lot of processing. In my case, we use the process in queues.
Please or to participate in this conversation.