Replaced the application level loop over the entire query set  with a DB level join.
Shouled results in a big boost in performance.