Subject Re: performance of subselect with group by
Author Dmitry Yemanov
15.12.2014 23:27, bjoern.reimer@... wrote:
>
> select min(t.Id) FROM test t
> group by t.reference, t.key
> having count(*) > 1
>
> costs 1000 Non indexed reads
>
> select * from test where Id in (
> select min(t.Id) FROM test t
> group by t.reference, t.key
> having count(*) > 1
> )
>
> costs 1001000 non indexed reads
>
> Why?

IN predicate is always evaluated for the every row, because internally
it's transformed into a correlated EXISTS equivalent:

select * from test where exists (
select * FROM test t
group by t.reference, t.key
having count(*) > 1 and min(t.Id) = ::id
)


Dmitry