pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Arjun Ranade <ranade@nodalexchange.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: SELECT statement returns in 10seconds, but INSERT/CREATE TABLE AS with same SELECT takes 7 minutes
Date: Thu, 27 Sep 2018 13:21:34 -0400
Message-ID: <24273.1538068894@sss.pgh.pa.us> (raw)
In-Reply-To: <CANrrCRxsC3wr711bjAJ4zK_16vF-qQHZXjSJTstbQt9kG5QDSw@mail.gmail.com>
References: <CANrrCRxsC3wr711bjAJ4zK_16vF-qQHZXjSJTstbQt9kG5QDSw@mail.gmail.com>

Arjun Ranade <ranade@nodalexchange.com> writes:
> I have a strange performance situation that I cannot resolve with my usual
> process.
> I have a SELECT statement that completes in about 12 seconds for the full
> result (~1100 rows).
> If I create an empty table first, and then INSERT with the SELECT query, it
> takes 6.5 minutes.

> When I look at the EXPLAIN ANALYZE output, it seems that it's using a
> drastically different query plan for the INSERT+SELECT than SELECT by
> itself.

The reason for the plan shape difference is probably that the bare SELECT
is allowed to use parallelism while INSERT/SELECT isn't.  I'm not sure
to what extent we could relax that without creating semantic gotchas.

However, your real problem with either query is that the planner's
rowcount estimates are off by several orders of magnitude.  If you could
improve that, you'd likely get better plan choices in both cases.

I also notice that this seems to be a 14-way join, which means you're
probably getting an artificially poor plan as a result of 
from_collapse_limit and/or join_collapse_limit constraining the planner's
search space.  Maybe raising those limits would help, although I'm not
sure how much it'd help if the rowcount estimates aren't improved.

Since you haven't told us much of anything about the actual query or the
data, it's hard to offer concrete advice beyond that.

			regards, tom lane




view thread (11+ messages)  latest in thread

Message-ID: <24273.1538068894@sss.pgh.pa.us>
Permalink:  ../24273.1538068894@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/24273.1538068894@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, ranade@nodalexchange.com
  Subject: Re: SELECT statement returns in 10seconds, but INSERT/CREATE TABLE AS with same SELECT takes 7 minutes
  In-Reply-To: <24273.1538068894@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox