Remove DISTINCT queries and try using sets