Thank you everyone for your responses so far, they
That's an interesting point. I don't have the code to hand at the moment but it is a loop indeed, literally just getting the user ID's from DB in one query, looping through them and for each ID inserting a new row in the mail table with a single query and with the unique user id in one of the columns. So is there a way to insert 40,000 rows (each with a unique user id) in it without a single query for each one? I'll look into the two things you suggested online myself but any further advice would be great. Of course when I'm back at the office I can post the exact code as well.
Thanks again guys.