Re: optimize query with a maximum(date) extraction

Поиск
Список
Период
Сортировка
От Gregory Stark
Тема Re: optimize query with a maximum(date) extraction
Дата
Msg-id 87ps0xp4nu.fsf@oxford.xeocode.com
обсуждение исходный текст
Ответ на Re: optimize query with a maximum(date) extraction  ("Pavel Stehule" <pavel.stehule@gmail.com>)
Ответы Re: optimize query with a maximum(date) extraction  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-performance
"Pavel Stehule" <pavel.stehule@gmail.com> writes:

>>
>> why not
>>
>> select id,
>>        min(the_date) as min_date,
>>        max(the_date) as max_date
>>   from my_table group by id;
>>
>> Since 8.0 or was it earlier this will use an index should a reasonable one
>> exist.

As I mentioned in the other post that's not true for this query.

> without any limits, seq scan is optimal.

That's not necessarily true either. You could have ten distinct ids and
millions of dates for them. In that case a scan of the index which jumped
around to scan from the beginning and end of each distinct id value would be
faster. There's just no such plan type in Postgres currently.

You can simulate such a plan with the subqueries I described but there's a bit
more overhead than necessary and you need a reasonably efficient source of the
distinct ids. Also it may or may not be faster than simply scanning the whole
table like above and simulating it with subqueries makes it impossible to
choose the best plan.

--
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com

В списке pgsql-performance по дате отправления:

Предыдущее
От: Gregory Stark
Дата:
Сообщение: Re: optimize query with a maximum(date) extraction
Следующее
От: "Carlo Stonebanks"
Дата:
Сообщение: Re: Performance on 8CPU's and 32GB of RAM