in reply to how to speed the mail process?

Like others, I suspect that your code is waiting for your mail transfer agent; I'd try mailing everything to a local account or appending to a local file just to see how timing is effected.

Yes, I know it's pseudocode, but:

Your prepare statement should be outside the loop.

It is good to avoid select * in code as the schema may change underneath you.

You may want to throw a sleep SOME_INTERVAL into your loop to ease the load on your database or add an option to let the database admin tune as load warrants. Do you have passes through the database that come up empty?

Setting mark should be contingent on the success of send_customer_mail in handing off the message to a MTA.

Be well,
rir