Re: Need help with optimising simple query - Mailing list pgsql-performance

From Tom Lane
Subject Re: Need help with optimising simple query
Date
Msg-id 9715.1531146227@sss.pgh.pa.us
Whole thread Raw
In response to Need help with optimising simple query  (Nandakumar M <m.nanda92@gmail.com>)
Responses Re: Need help with optimising simple query  (Nandakumar M <m.nanda92@gmail.com>)
List pgsql-performance
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


pgsql-performance by date:

Previous
From: Nandakumar M
Date:
Subject: Need help with optimising simple query
Next
From: Nandakumar M
Date:
Subject: Re: Need help with optimising simple query