Optimize several inefficient HQLs.