From: dato0011@hotmail.com
To: pgsql-bugs@postgresql.org
Subject: BUG #7665: Query planner generating incorrect query plan
Date: Fri, 16 Nov 2012 13:21:27 +0000
Message-ID: <E1TZLrL-0002Cy-In@wrigleys.postgresql.org> (raw)
The following bug has been logged on the website:
Bug reference: 7665
Logged by: David Popiashvili
Email address: dato0011@hotmail.com
PostgreSQL version: 9.2.1
Operating system: Windows 8 x64
Description:
The problem is in LIMIT keyword and how it affects query planner.
I have a database with 6 tables and ~450 million rows distributed among
them.
When I run the following query:
select * from "Payments" as p
inner join "PaymentOrders" as po
on po."Id" = p."PaymentOrderId"
inner join "Users" as u
On u."Id" = po."UserId"
INNER JOIN "Roles" as r
on u."RoleId" = r."Id"
Where r."Name" = 'Moses'
LIMIT 1000
It completes in a short period of time (several milliseconds), but if I
modify the where clause as r."Name" = 'SomeNonExistentName' the query takes
a very very long time to complete. You can see EXPLAIN ANALYZE details here:
http://explain.depesz.com/s/7e7
From the discussion with other community members on
StackOverflow(http://stackoverflow.com/questions/13407555/postgresql-query-taking-too-long/13415984),
we think that due to the fact that the query has LIMIT keyword, PostgreSQL
always assumes that it will find rows with specified where clause soon
enough and that's why it is doing a seq scan on the tables, which is wrong.
As I already mentioned, when r."Name" = 'Something' yield no match, the
query takes too long, it scans several hundred millions of rows.
The cure is to just remove LIMIT keyword from the query, and we will get the
following query plan:
http://explain.depesz.com/s/0wOq
however, this is not always an option.
Here's the full postgresql.conf file I'm running
https://dl.dropbox.com/u/3055964/postgresql.conf
I've been recommended to let you know about the issue. In case of questions,
don't hesitate to contact me.
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: dato0011@hotmail.com
Subject: Re: BUG #7665: Query planner generating incorrect query plan
In-Reply-To: <E1TZLrL-0002Cy-In@wrigleys.postgresql.org>
References: <E1TZLrL-0002Cy-In@wrigleys.postgresql.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