pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Nandakumar M <m.nanda92@gmail.com>
Cc: Pgsql Performance <pgsql-performance@lists.postgresql.org>
Subject: Re: Need help with optimising simple query
Date: Mon, 09 Jul 2018 10:23:47 -0400
Message-ID: <9715.1531146227@sss.pgh.pa.us> (raw)
In-Reply-To: <CANcFUu5a9W_MORQa5Tv9vsofN_w_irU9-OBQtN34YmL7p4gP_g@mail.gmail.com>
References: <CANcFUu5a9W_MORQa5Tv9vsofN_w_irU9-OBQtN34YmL7p4gP_g@mail.gmail.com>

Nandakumar M <m.nanda92@gmail.com> writes:
> I am having a query that has an order by and a limit clause. The
> column on which I am doing order by is indexed (default b tree index).
> However the index is not being used. On tweaking the query a bit I
> found that when I use left join index is not used whereas when I use
> inner join the index is used.

The reason the index isn't being used is that the sort order the query
requests isn't the same as the order provided by the index.  Here:

> performance_test=# explain analyze select * from parent left join
> child on parent.child_id = child.id order by child.name limit 10;

you're asking to sort by a column that will include null values for
child.name anywhere that there's a parent row without a match for
child_id.  Those rows aren't even represented in the index on child.name,
much less placed in the right order.

			regards, tom lane




view thread (5+ messages)  latest in thread

Message-ID: <9715.1531146227@sss.pgh.pa.us>
Permalink:  ../9715.1531146227@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/9715.1531146227@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, m.nanda92@gmail.com, pgsql-performance@lists.postgresql.org
  Subject: Re: Need help with optimising simple query
  In-Reply-To: <9715.1531146227@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