Have you considered to use temporary tables (with indexes) instead of expensive ```IN``` clauses against thousands of elements?