Re: Slow SQL?

Поиск
Список
Период
Сортировка
От hubert depesz lubaczewski
Тема Re: Slow SQL?
Дата
Msg-id 20160712123450.GA774@depesz.com
обсуждение исходный текст
Ответ на Slow SQL?  (Bjørn T Johansen <btj@havleik.no>)
Ответы Re: Slow SQL?  (Bjørn T Johansen <btj@havleik.no>)
Список pgsql-general
On Tue, Jul 12, 2016 at 10:23:24AM +0200, Bjørn T Johansen wrote:
> I am trying to move a small system from Oracle to PostgreSQL and
> I have come upon a sql that runs really slow compared to on the Oracle
> database and I am not able to interpret why this is slow.

I loaded your explain analyze to https://explain.depesz.com/, as:
https://explain.depesz.com/s/iXK

as you can see there, the problem is that you made 280 thousand checks
for "sed_uttak y", which seems to be related to this part:


> Select a.status, a.plass, a.navn, a.avlsnr,
>            date_part('day',(now() - s.dato)) dato_diff, v.tekst, COALESCE(a.avlsverdi,0)
> From   sed_uttak s, sem_avlsverdi a, semin_vare v
> where a.aktiv = 1
> And    s.dato  = (Select Max(y.dato)
>                           From sed_uttak y
>                           Where y.avlsnr = s.avlsnr)

from what I understand, you're doing it to work on newest record from
sed_uttak, for each avlsnr.

What is rowcount in the table, and how many different avlsnr are there?

You might want to do something like:

with s as (
    select distinct on (avlsnr) *
    from sed_uttak
    order by avlsnr desc, dato desc
)

and then use "s" instead of set_uttak, and get rid of the s.dato
= (select max....) checks.

Best regards,

depesz

--
The best thing about modern society is how easy it is to avoid contact with it.
                                                             http://depesz.com/


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

Предыдущее
От: Felipe Santos
Дата:
Сообщение: Re: pg_restore out of memory
Следующее
От: Albe Laurenz
Дата:
Сообщение: Re: Slow SQL?