From: Tom Lane <tgl@sss.pgh.pa.us>
To: Shantanu Shekhar <shekharshan@yahoo.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Range search on primary key does not use index scan
Date: Wed, 23 Dec 2020 20:43:08 -0500
Message-ID: <750937.1608774188@sss.pgh.pa.us> (raw)
In-Reply-To: <1366951904.2544671.1608772134019@mail.yahoo.com>
References: <1366951904.2544671.1608772134019.ref@mail.yahoo.com>
<1366951904.2544671.1608772134019@mail.yahoo.com>
Shantanu Shekhar <shekharshan@yahoo.com> writes:
> (2) Here I am searching for employee_id < 123. I was expecting the plan would use the index on employees_pk to find all leaf nodes where employee_id < 123 and then issue read of table blocks for each of the matching entries in the index leaf. But looks like the query plan has decided on using full table scan instead and not using the index. Any ideas why is this happening?
> testdb# explain select first_name, last_name from testschema.employees where employee_id < 123; QUERY PLAN ══════════════════════════════════════════════════════════════ Seq Scan on employees (cost=0.00..10.62 rows=17 width=1032) Filter: (employee_id < 123)(2 rows)
In the absence of any statistics (and since you have no data in the table,
there are no stats either), the default assumption about an inequality
condition is that it's not very selective --- I think it's assumed to
select one-third of the table, but am too lazy to go check that right now.
In any case, it's enough of the table to discourage use of an indexscan.
With Postgres' normal cost settings, a potentially indexable condition
has to be estimated to select just a few percent of the table, else a
seqscan is going to look cheaper.
If you'd used an actual range condition (which I take to be a BETWEEN
clause or equivalent), you probably would have gotten an indexscan plan,
because the default estimate for that is a lot tighter. But a one-sided
inequality can't reasonably be assumed to be selecting just a small
part of the table without any evidence to back that. Now, if you'd
populated and analyzed the table, the planner might discover that only
a few percent of the rows have employee_id < 123 (if you've got many
thousands of employees), and then it'd pick an indexscan for the
query as it stands.
Taking a few steps back here, the way you are testing things is not going
to do anything except mislead you. Queries on empty tables that lack any
statistics are unlikely to produce the same plans as queries on populated,
analyzed tables. See for instance the advice at
https://www.postgresql.org/docs/current/using-explain.html#USING-EXPLAIN-CAVEATS
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, shekharshan@yahoo.com
Subject: Re: Range search on primary key does not use index scan
In-Reply-To: <750937.1608774188@sss.pgh.pa.us>
References: <1366951904.2544671.1608772134019.ref@mail.yahoo.com> <1366951904.2544671.1608772134019@mail.yahoo.com> <750937.1608774188@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