Fix performance issues in DB queries.