[DB] optimize queries using DISTINCT