It seems unnecessary to query inside this for-loop at all.  I think you can move ex_logs and vid_logs outside of the loop.