Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mkdesign82's avatar

Stop select the database on each itterate

Hi,

I have a table with 700,000 records init with 20 column length, in part of my project I have a foreach which I should select one record from the table, for 400 iterates and 400 select, it took me 46sec to retrieve the 400 results. which is not good. guys do you have any idea to improve the speed and the time I use.

0 likes
2 replies
tykus's avatar

This is a use case for eager-loading if there is a relationship between the collection being iterated over and the select query being made.

It is difficult suggest anything further without more information about your specific case.

1 like
audiogram's avatar

Yes, the GOTO for avoiding the hell you're in is eager loading. Check out the docs on this for a very clear and concise explanation.

Please or to participate in this conversation.