Create indexes for your foreign keys. Even for 50 000 rows.
If you load 12 000 eloquent object + 4 relationship for each in memory there will be problems. Use the chunk method to treat them by smaller chunks.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I was optimizing an app, where I need to export some data.
There is a query to export around 12 000 rows, but for all rows, there is 3 or 4 relations used.
When I execute the request excluding the relations data, export is build in less than 0.5s. But, when I add this relations (with eager loading, or not) in the export, request take more than 2 min.
I've looked at the server side, and mysql is not really overloaded, but apache is used at 100% on one thread during those 2 mins.
So, I think it's a php/Eloquent issue, and not from the request.
My question is : If a relations is declared in my php application (the Eloquent way), but I have not set the foreign key on the mysql side, may it generate performance issue for Eloquent, where Eloquent needs to test if the relation is set on active record etc ? Because I know I can use indexes, but we work on around 50 000 rows in our tables, so in can't explain this time difference.
Thanks
Create indexes for your foreign keys. Even for 50 000 rows.
If you load 12 000 eloquent object + 4 relationship for each in memory there will be problems. Use the chunk method to treat them by smaller chunks.
Please or to participate in this conversation.