Re: Need help with optimising simple query

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Nandakumar M <m(dot)nanda92(at)gmail(dot)com>
Cc: Pgsql Performance <pgsql-performance(at)lists(dot)postgresql(dot)org>
Subject: Re: Need help with optimising simple query
Date: 2018-07-09 14:23:47
Message-ID: 9715.1531146227@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Nandakumar M <m(dot)nanda92(at)gmail(dot)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

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Bruce Momjian 2018-07-09 15:19:28 Re: add default parallel query to v10 release notes? (Re: [PERFORM] performance drop after upgrade (9.6 > 10))
Previous Message Nandakumar M 2018-07-09 11:47:23 Need help with optimising simple query