Optimize tree predicates to use more efficient SQL queries