From: Tom Lane <tgl@sss.pgh.pa.us>
To: Arup Rakshit <ar@zeit.io>
Cc: pgsql-general@postgresql.org
Subject: Re: Why order by column not using index with distinct keyword in select clause?
Date: Tue, 11 Sep 2018 10:07:08 -0400
Message-ID: <21078.1536674828@sss.pgh.pa.us> (raw)
In-Reply-To: <9B9E43A3-5FA7-4FF4-8444-73C24DEE4107@zeit.io>
References: <9B9E43A3-5FA7-4FF4-8444-73C24DEE4107@zeit.io>
Arup Rakshit <ar@zeit.io> writes:
> I have define a simple B Tree index on column *country* for users table. I don’t understand why the order by column not using the index scan when using *distinct* keyword in the select clause. Can anyone explain what is happening here?
Doesn't look complicated to me: the cost of the hashagg+sort plan is
283.85, while the cost of the indexscan plans is 886.27, and adding
a Uniq node on top of them would have been even more.
The critical point here seems to be that the hashagg node is able to
get rid of so many rows that the cost of sorting what remains is low.
Also notable is that the IOS isn't giving you any advantage over a
plain indexscan, which apparently is because none of the table's
pages are marked all-visible. If the table is static then a VACUUM
would help that.
regards, tom lane
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, ar@zeit.io
Subject: Re: Why order by column not using index with distinct keyword in select clause?
In-Reply-To: <21078.1536674828@sss.pgh.pa.us>
References: <9B9E43A3-5FA7-4FF4-8444-73C24DEE4107@zeit.io> <21078.1536674828@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