Postgresql isn't very efficient with subselects like that,try:explain select c.id from content c LEFT JOIN (select min(id) AS idfrom content group by hash) cg ON cg.id=c.id WHERE cg.id is null;