Would there be any value in moving the mail step (5) up into (3)? Instead of building a hashmap and saving it until you're done reading the resultset, mail the prior department as soon as you hit the new department ... and again at end. You might even be able to run the mailing on its own
thread.
That would get the first mailing out sooner and avoid storing all the mail ids in the hashmap even for a short while. Don't know if it would get the LAST mailing out any sooner, tho.
[ January 25, 2006: Message edited by: Stan James ]