From: Дилян Палаузов <dilyan.palauzov@aegee.org>
To: pgsql-bugs@postgresql.org
Subject: Query planner skipping index depending on DISTINCT parameter order
Date: Thu, 14 Sep 2017 17:38:25 +0200
Message-ID: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Hello,
I have this database in Pg 9.6.5, the schema comes from spamassassin:
┌────────────┬─────────┬──────────────────────────────┐
│ Column │ Type │ Modifiers │
├────────────┼─────────┼──────────────────────────────┤
│ id │ integer │ not null default 0 │
│ token │ bytea │ not null default '\x'::bytea │
│ spam_count │ integer │ not null default 0 │
│ ham_count │ integer │ not null default 0 │
│ atime │ integer │ not null default 0 │
└────────────┴─────────┴──────────────────────────────┘
Indexes:
"bayes_token_pkey" PRIMARY KEY, btree (id, token)
"bayes_token_idx1" btree (token)
"the_index" btree (id, token)
with 261000 rows. According to my understanding, "SELECT DISTINCT ON(token, id) token FROM bayes_token;" and "SELECT DISTINCT ON(id, token) token FROM bayes_token;" are supposed to deliver the same results, as only the "token" and "id" columns are exchanged in DISTINCT, so the query optimizer is supposed to generate the same query plan. But it does not:
EXPLAIN ANALYZE SELECT DISTINCT ON(token, id) token FROM bayes_token;
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ QUERY PLAN │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Unique (cost=32624.56..34588.73 rows=261889 width=10) (actual time=154.947..264.439 rows=261935 loops=1) │
│ -> Sort (cost=32624.56..33279.28 rows=261889 width=10) (actual time=154.945..215.378 rows=261935 loops=1) │
│ Sort Key: token, id │
│ Sort Method: external merge Disk: 5624kB │
│ -> Seq Scan on bayes_token (cost=0.00..4579.89 rows=261889 width=10) (actual time=0.015..35.282 rows=261935 loops=1) │
│ Planning time: 0.085 ms │
│ Execution time: 285.303 ms │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
EXPLAIN ANALYZE SELECT DISTINCT ON(id, token) token FROM bayes_token;
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ QUERY PLAN │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Unique (cost=0.42..13819.07 rows=262023 width=10) (actual time=0.028..207.695 rows=262018 loops=1) │
│ -> Index Only Scan using the_index on bayes_token (cost=0.42..12508.95 rows=262023 width=10) (actual time=0.026..147.882 rows=262018 loops=1) │
│ Heap Fetches: 261729 │
│ Planning time: 0.086 ms │
│ Execution time: 232.598 ms │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
The_index and the SELECTs are invented for the sake of this demonstration.
Regards
Diluan
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
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-bugs@postgresql.org
Cc: dilyan.palauzov@aegee.org
Subject: Re: Query planner skipping index depending on DISTINCT parameter order
In-Reply-To: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org>
References: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org>
* 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