Re: Slow SQL?

From: Bjørn T Johansen <btj(at)havleik(dot)no>
To: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Slow SQL?
Date: 2016-07-13 06:32:17
Message-ID: 20160713083217.6b1556a5@arch-btj.norsvin.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 12 Jul 2016 14:34:50 +0200
hubert depesz lubaczewski <depesz(at)depesz(dot)com> wrote:

> 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
>

Rewrote using the with s as method and now it's faster than the oracle querytime, so I am happy...
And I learned something new, never used the with s as way before...

Thx! :)

BTJ

In response to

  • Re: Slow SQL? at 2016-07-12 12:34:50 from hubert depesz lubaczewski

Browse pgsql-general by date

  From Date Subject
Next Message Karsten Hilbert 2016-07-13 08:41:03 Re: pg_restore out of memory
Previous Message dandl 2016-07-13 00:29:55 Re: Question about antijoin