Re: Slow SQL?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bjørn T Johansen <btj(at)havleik(dot)no>
Cc: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>, "'hamann(dot)w(at)t-online(dot)de *EXTERN*'" <hamann(dot)w(at)t-online(dot)de>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Slow SQL?
Date: 2016-07-12 14:05:32
Message-ID: 19794.1468332332@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

=?UTF-8?B?QmrDuHJu?= T Johansen <btj(at)havleik(dot)no> writes:
> But when I try to run the 3 queries separately, then they are very quick, 2 barely measurable and the third takes about 1,5 seconds. The union query
> takes a little over 9 seconds, so I guess the union part is the bottleneck?

No; it's clear from your EXPLAIN output that the first UNION arm is taking
the bulk of the runtime. It's odd that it wouldn't be exactly the same
when run standalone. What does EXPLAIN ANALYZE show if you run that first
arm by itself?

I concur with depesz's observation that the MAX() subselects seem to be
pretty expensive. If you don't want to rewrite the query in a wholesale
fashion like he suggests, you might be able to make the MAX's cheaper by
providing an index on sed_uttak(avlsnr, dato); but I'm not sure how much
that will help.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2016-07-12 14:13:23 Re: Question about antijoin
Previous Message Sameer Kumar 2016-07-12 13:08:02 Re: pg_restore out of memory