I\'m having an inner debate at my company about looping queries in this matter:
$sql = \"
SELECT foreign_key
FROM t1\";
foreach(fetchAll($sql) as $row)
This is similar to another question I answered, but different enough not to cv. My full answer is here but I'll summarize the main points:
Whenever you make a connection to a database, there are three steps taken:
Using a loop structure, you will end up generating additional overhead with driver requests, where you will have a request and a return per loop cycle rather than a single request and single return. Even if the looped queries do not take any longer than the single large query (this is very unlikely as MySQL internals have a lot of shortcuts built in to prevent using a full repetitive loop), you will still find that the single query is faster on driver overhead.
Using a loop without TRANSACTIONS, you will also find that you run into relational data integrity issues where other operations affect the data you're iterating between loop cycles. Using transactions, again, increases overhead because the database has to maintain two persistent states.