Optimizing template nodes query.