more performance tweaks on query construction