Try to use whereIn for trip_id and then key results by that trip_id or something that makes sense in order to get an easy match with the remaining data.
Database performance improvement: searching through 500,000+ rows
Hi,
I have a job that takes quite some time (near or more than a minute). I would like to find any opportunities to improve my code for better performance.
Currently, the function goes through $entity (up to 1,200) and try to find a corresponding $trip. There is more than 500,000 trips (records).
foreach ($feed->getEntity() as $entity) {
/*
* Check if trip is in database
*/
$trip = Trip::where([['agency_id', '=', $this->agency->id], ['trip_id', '=', $entity->getVehicle()->getTrip()->getTripId()]])
->remember(60 * 60 * 24)
->cacheTags('trip_queries')
->select('id')
->first();
}
Over the time, I have made some improvements, the first one being to select only the id field, wich resulted in a big improvement. Then, I added the Watson\Rememberable package, wich didn't had any major difference.
Currently, when there is a lot of vehicles, the job timeout after 60 seconds (I don't want to extend the timeout, as I want the same job to run every minute).
Any ideas on improvements?
Thank you!
bit worrying that you don't mention indexes anywhere in your question?
You would definitely need an index of agency_id +trip_id
Must watch this; https://youtu.be/HubezKbFL7E
Please or to participate in this conversation.