pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andrey Povazhnyi <w0rse.t@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Slow query question
Date: Tue, 06 Dec 2016 10:33:55 -0500
Message-ID: <26351.1481038435@sss.pgh.pa.us> (raw)
In-Reply-To: <48E80576-4047-436C-97DD-59EA7BE4DE34@gmail.com>
References: <48E80576-4047-436C-97DD-59EA7BE4DE34@gmail.com>
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>

Andrey Povazhnyi <w0rse.t@gmail.com> writes:
> We’ve got a strange planner behavior on a query to one of our bigger tables after we upgraded to postgres 9.6.1 recently.

The basic problem with this query is that there are no good alternatives.
The planner believes there are about 53K rows matching the WHERE
condition.  (I assume this estimate is roughly in line with reality,
else we have different problems to talk about.)  It can either scan down
the "id" index and stop when it finds the 30th row matching WHERE, or
it can use the "symbol" index to read all 53K rows matching WHERE and
then sort them by "id".  Neither one of those is going to be speedy;
but the more rows there are matching WHERE, the better the first way
is going to look.

If you're worried about doing this a lot, it might be worth your while
to provide a 2-column index on (source, id) --- in that order --- which
would allow a query plan that directly finds the required 30 rows as
consecutive index entries.  Possibly this could replace your index on
"source" alone, depending on how much bigger the 2-col index is and
how many queries have no use for the second column.  See
https://www.postgresql.org/docs/current/static/indexes.html
particularly 11.3 - 11.5.

			regards, tom lane


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



view thread (3+ messages)  latest in thread

Message-ID: <26351.1481038435@sss.pgh.pa.us>
Permalink:  ../26351.1481038435@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/26351.1481038435@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, w0rse.t@gmail.com
  Subject: Re: Slow query question
  In-Reply-To: <26351.1481038435@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