I think I know the answer to this question but wanted another opinion on it. So I have inherited an e-mailing script that sends out X number of mails every time it executes. It currently does things (IMO) in an odd way by
SELECT emailid, emailTo
FROM email
ORDER BY
dateadded ASC, priority ASC, emailDomain.DESC
LIMIT 250
Then runs this through a PHP loop grabbing out the content by
SELECT subject, message, emailHeaders
FROM email
WHERE emailid = {$id}
The message and headers are full e-mail headers (with HTML) so quite a lot of content, now I think it would be more efficient to select out this content on the initial select rather than hit the DB 250 times requesting each mail individually. So changing the main query to
SELECT emailid, emailTo, subject, message, emailHeaders
FROM email
ORDER BY
dateadded ASC, priority ASC, emailDomain.DESC
LIMIT 250
To me that feels better, I have run the query and it is not significantly slower than selecting without all the content, but would it be better asking for the content by id (indexed) 250 times? If anyone has some stats on this or opinions I would appreciate the input.
Thanks
It’s better to get all the data in single query. You don’t refer to subject, message nor emailHeaders in WHERE and ORDER BY clause so the data is not being processed. Also it’s better to execute one query because of: