reduce the number of queries on IN clauses, fix relation queries in `where`