pg.ddx.io pgsql-performance@postgresql.org mailing list archive
help / color / mirror / Atom feedRecognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
12+ messages / 7 participants
[nested] [flat]
* Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-06 22:09 Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 3 replies; 12+ messages in thread
From: Tom Lane @ 2005-04-06 22:09 UTC (permalink / raw)
To: Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>; +Cc: pgsql-hackers@postgreSQL.org; pgsql-performance
I wrote:
> Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl> writes:
>> SELECT COUNT(*) FROM
>> data_main AS dm,
>> postcodes AS p
>> WHERE dm.range BETWEEN p.range_from AND p.range_till
> Planner error ... because it doesn't have any good way to estimate the
> number of matching rows, it thinks that way is a bit more expensive than
> data_main as the outside, but in reality it seems a good deal cheaper:
BTW, it would get the right answer if it had recognized the WHERE clause
as a range restriction --- it still doesn't know exactly what fraction
of rows will match, but its default estimate is a great deal tighter for
"WHERE x > something AND x < somethingelse" than it is for two unrelated
inequality constraints. Enough tighter that it would have gone for the
correct plan.
The problem is that it doesn't recognize the WHERE as a range constraint
on dm.range. I thought for a moment that this might be a
recently-introduced bug, but actually the code is operating as designed:
clauselist_selectivity says
* See if it looks like a restriction clause with a pseudoconstant
* on one side. (Anything more complicated than that might not
* behave in the simple way we are expecting.)
"Pseudoconstant" in this context means "a constant, parameter symbol, or
non-volatile functions of these" ... so comparisons against values from
another table don't qualify. It seems like we're missing a bet though.
Can anyone suggest a more general rule? Do we need for example to
consider whether the relation membership is the same in two clauses
that might be opposite sides of a range restriction? It seems like
a.x > b.y AND a.x < b.z
probably can be treated as a range restriction on a.x for this purpose,
but I'm much less sure that the same is true of
a.x > b.y AND a.x < c.z
Thoughts?
regards, tom lane
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [HACKERS] Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-06 22:25 Jim C. Nasby <decibel@decibel.org>
parent: Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 1 reply; 12+ messages in thread
From: Jim C. Nasby @ 2005-04-06 22:25 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>; pgsql-hackers@postgreSQL.org; pgsql-performance
On Wed, Apr 06, 2005 at 06:09:37PM -0400, Tom Lane wrote:
> Can anyone suggest a more general rule? Do we need for example to
> consider whether the relation membership is the same in two clauses
> that might be opposite sides of a range restriction? It seems like
>
> a.x > b.y AND a.x < b.z
In a case like this, you could actually look at the data in b and see
what the average range size is. If you wanted to get really fancy, the
optimizer could decide how best to access a based on each row of b.
> probably can be treated as a range restriction on a.x for this purpose,
> but I'm much less sure that the same is true of
>
> a.x > b.y AND a.x < c.z
Well, this could end up being much trickier, since who knows how b and c
are related. Though thinking about it, although I threw out the
row-by-row analysis idea to be glib, that would actually work in this
case; you could take a look at what b and c look like each time 'through
the loop'.
--
Jim C. Nasby, Database Consultant decibel@decibel.org
Give your computer some brain candy! www.distributed.net Team #1828
Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [HACKERS] Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-06 22:35 Tom Lane <tgl@sss.pgh.pa.us>
parent: Jim C. Nasby <decibel@decibel.org>
0 siblings, 2 replies; 12+ messages in thread
From: Tom Lane @ 2005-04-06 22:35 UTC (permalink / raw)
To: Jim C. Nasby <decibel@decibel.org>; +Cc: Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>; pgsql-hackers@postgreSQL.org; pgsql-performance
"Jim C. Nasby" <decibel@decibel.org> writes:
> On Wed, Apr 06, 2005 at 06:09:37PM -0400, Tom Lane wrote:
>> Can anyone suggest a more general rule? Do we need for example to
>> consider whether the relation membership is the same in two clauses
>> that might be opposite sides of a range restriction? It seems like
>>
>> a.x > b.y AND a.x < b.z
> In a case like this, you could actually look at the data in b and see
> what the average range size is.
Not with the current statistics --- you'd need some kind of cross-column
statistics involving both y and z. (That is, I doubt it would be
helpful to estimate the average range width by taking the difference of
independently-calculated mean values of y and z ...) But yeah, in
principle it would be possible to make a non-default estimate.
regards, tom lane
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [HACKERS] Recognizing range constraints (was Re: Plan
@ 2005-04-06 22:54 John A Meinel <john@arbash-meinel.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 12+ messages in thread
From: John A Meinel @ 2005-04-06 22:54 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Jim C. Nasby <decibel@decibel.org>; Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>; pgsql-hackers@postgresql.org; pgsql-performance
Tom Lane wrote:
> "Jim C. Nasby" <decibel@decibel.org> writes:
>
>>On Wed, Apr 06, 2005 at 06:09:37PM -0400, Tom Lane wrote:
>>
>>>Can anyone suggest a more general rule? Do we need for example to
>>>consider whether the relation membership is the same in two clauses
>>>that might be opposite sides of a range restriction? It seems like
>>>
>>>a.x > b.y AND a.x < b.z
>
>
>>In a case like this, you could actually look at the data in b and see
>>what the average range size is.
>
>
> Not with the current statistics --- you'd need some kind of cross-column
> statistics involving both y and z. (That is, I doubt it would be
> helpful to estimate the average range width by taking the difference of
> independently-calculated mean values of y and z ...) But yeah, in
> principle it would be possible to make a non-default estimate.
>
> regards, tom lane
Actually, I think he was saying do a nested loop, and for each item in
the nested loop, re-evaluate if an index or a sequential scan is more
efficient.
I don't think postgres re-plans once it has started, though you could
test this in a plpgsql function.
John
=:->
Attachments:
signature.asc (application/pgp-signature, 250B) download
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [HACKERS] Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-06 23:06 Tom Lane <tgl@sss.pgh.pa.us>
parent: John A Meinel <john@arbash-meinel.com>
0 siblings, 0 replies; 12+ messages in thread
From: Tom Lane @ 2005-04-06 23:06 UTC (permalink / raw)
To: John A Meinel <john@arbash-meinel.com>; +Cc: Jim C. Nasby <decibel@decibel.org>; Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>; pgsql-hackers@postgresql.org; pgsql-performance
John A Meinel <john@arbash-meinel.com> writes:
> Actually, I think he was saying do a nested loop, and for each item in
> the nested loop, re-evaluate if an index or a sequential scan is more
> efficient.
> I don't think postgres re-plans once it has started, though you could
> test this in a plpgsql function.
It doesn't, and in any case that's a microscopic view of the issue.
The entire shape of the plan might change depending on what we think
the selectivity is --- much more than could be handled by switching
scan types at the bottom level.
Also, I anticipate that bitmap-driven index scans will change things
considerably here. The range of usefulness of pure seqscans will
drop drastically...
regards, tom lane
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Recognizing range constraints (was Re: Plan for
@ 2005-04-06 23:24 Simon Riggs <simon@2ndquadrant.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 0 replies; 12+ messages in thread
From: Simon Riggs @ 2005-04-06 23:24 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>; pgsql-hackers@postgreSQL.org; pgsql-performance
On Wed, 2005-04-06 at 18:09 -0400, Tom Lane wrote:
> I wrote:
> > Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl> writes:
> >> SELECT COUNT(*) FROM
> >> data_main AS dm,
> >> postcodes AS p
> >> WHERE dm.range BETWEEN p.range_from AND p.range_till
>
> > Planner error ... because it doesn't have any good way to estimate the
> > number of matching rows, it thinks that way is a bit more expensive than
> > data_main as the outside, but in reality it seems a good deal cheaper:
>
> BTW, it would get the right answer if it had recognized the WHERE clause
> as a range restriction --- it still doesn't know exactly what fraction
> of rows will match, but its default estimate is a great deal tighter for
> "WHERE x > something AND x < somethingelse" than it is for two unrelated
> inequality constraints. Enough tighter that it would have gone for the
> correct plan.
>
> The problem is that it doesn't recognize the WHERE as a range constraint
> on dm.range.
> Can anyone suggest a more general rule? Do we need for example to
> consider whether the relation membership is the same in two clauses
> that might be opposite sides of a range restriction? It seems like
>
> a.x > b.y AND a.x < b.z
Not sure we need a more general rule. There's only three ways to view
this pair of clauses:
i) its a range constraint i.e. BETWEEN
ii) its the complement of that i.e. NOT BETWEEN
iii) its a mistake, but we're not allowed to take that path
Arjen's query and your generalisation of it above is a common type of
query - using a lookup of a reference data table with begin/end
effective dates. It would be very useful if this was supported.
> probably can be treated as a range restriction on a.x for this purpose,
> but I'm much less sure that the same is true of
>
> a.x > b.y AND a.x < c.z
I can't think of a query that would use such a construct, and might even
conclude that it was very poorly normalised model. I would suggest that
this is much less common in practical use.
Best Regards, Simon Riggs
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-07 14:20 Tom Lane <tgl@sss.pgh.pa.us>
parent: Bruno Wolff III <bruno@wolff.to>
0 siblings, 1 reply; 12+ messages in thread
From: Tom Lane @ 2005-04-07 14:20 UTC (permalink / raw)
To: Bruno Wolff III <bruno@wolff.to>; +Cc: Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>; pgsql-hackers@postgreSQL.org; pgsql-performance
Bruno Wolff III <bruno@wolff.to> writes:
> Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Can anyone suggest a more general rule?
> I think it makes sense to guess that a smaller fraction of the rows will
> be returned when a column value is bounded above and below than if it
> is only bounded on one side, even if the bounds aren't fixed. You can
> certainly be wrong.
Yeah, the whole thing is only a heuristic anyway. I've been coming
around to the view that relation membership shouldn't matter, because
of cases like
WHERE a.x > b.y AND a.x < 42
which surely should be taken as a range constraint.
regards, tom lane
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-07 14:31 Bruno Wolff III <bruno@wolff.to>
parent: Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 1 reply; 12+ messages in thread
From: Bruno Wolff III @ 2005-04-07 14:31 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>; pgsql-hackers@postgreSQL.org; pgsql-performance
On Wed, Apr 06, 2005 at 18:09:37 -0400,
Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Can anyone suggest a more general rule? Do we need for example to
> consider whether the relation membership is the same in two clauses
> that might be opposite sides of a range restriction? It seems like
>
> a.x > b.y AND a.x < b.z
>
> probably can be treated as a range restriction on a.x for this purpose,
> but I'm much less sure that the same is true of
>
> a.x > b.y AND a.x < c.z
>
> Thoughts?
I think it makes sense to guess that a smaller fraction of the rows will
be returned when a column value is bounded above and below than if it
is only bounded on one side, even if the bounds aren't fixed. You can
certainly be wrong. The difference between this and the normal case is that
column statistics aren't normally going to be that useful.
If date/time ranges are the common use for this construct, it might be better
to create date and/or time range types that use rtree or gist indexes.
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-07 21:26 Mischa <mischa.Sandberg@telus.net>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 12+ messages in thread
From: Mischa @ 2005-04-07 21:26 UTC (permalink / raw)
To: pgsql-performance
Quoting Tom Lane <tgl@sss.pgh.pa.us>:
> Yeah, the whole thing is only a heuristic anyway. I've been coming
> around to the view that relation membership shouldn't matter, because
> of cases like
>
> WHERE a.x > b.y AND a.x < 42
>
> which surely should be taken as a range constraint.
Out of curiosity, will the planner induce "b.y < 42" out of this?
--
"Dreams come true, not free."
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [HACKERS] Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-07 21:40 Jim C. Nasby <decibel@decibel.org>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 12+ messages in thread
From: Jim C. Nasby @ 2005-04-07 21:40 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>; pgsql-hackers@postgreSQL.org; pgsql-performance
On Wed, Apr 06, 2005 at 06:35:10PM -0400, Tom Lane wrote:
> "Jim C. Nasby" <decibel@decibel.org> writes:
> > On Wed, Apr 06, 2005 at 06:09:37PM -0400, Tom Lane wrote:
> >> Can anyone suggest a more general rule? Do we need for example to
> >> consider whether the relation membership is the same in two clauses
> >> that might be opposite sides of a range restriction? It seems like
> >>
> >> a.x > b.y AND a.x < b.z
>
> > In a case like this, you could actually look at the data in b and see
> > what the average range size is.
>
> Not with the current statistics --- you'd need some kind of cross-column
> statistics involving both y and z. (That is, I doubt it would be
> helpful to estimate the average range width by taking the difference of
> independently-calculated mean values of y and z ...) But yeah, in
> principle it would be possible to make a non-default estimate.
Actually, it might be possible to take a SWAG at it using the histogram
and correlation stats.
You know... since getting universally useful cross-platform stats seems
to be pretty pie-in-the-sky, would it be possible to generate more
complex stats on the fly from a sampling of a table? If you're looking
at a fairly sizeable table ISTM it would be worth sampling the rows on
10 or 20 random pages to see what you get. In this case, you'd want to
know the average difference between two fields. Other queries might want
something different.
--
Jim C. Nasby, Database Consultant decibel@decibel.org
Give your computer some brain candy! www.distributed.net Team #1828
Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-07 23:58 Tom Lane <tgl@sss.pgh.pa.us>
parent: Mischa <mischa.Sandberg@telus.net>
0 siblings, 1 reply; 12+ messages in thread
From: Tom Lane @ 2005-04-07 23:58 UTC (permalink / raw)
To: Mischa <mischa.Sandberg@telus.net>; +Cc: pgsql-performance
Mischa <mischa.Sandberg@telus.net> writes:
> Quoting Tom Lane <tgl@sss.pgh.pa.us>:
>> WHERE a.x > b.y AND a.x < 42
> Out of curiosity, will the planner induce "b.y < 42" out of this?
No. There's some smarts about transitive equality, but none about
transitive inequalities. Offhand I'm not sure if it'd be useful to add
such. The transitive-equality code pulls its weight because you so
often have situations like
create view v as select a.x, ... from a join b on (a.x = b.y);
select * from v where x = 42;
but I'm less able to think of common use-cases for transitive
inequality ...
regards, tom lane
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
@ 2005-04-08 00:30 a3a18850@telus.net
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 12+ messages in thread
From: a3a18850@telus.net @ 2005-04-08 00:30 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Mischa <mischa.Sandberg@telus.net>; pgsql-performance
Quoting Tom Lane <tgl@sss.pgh.pa.us>:
> Mischa <mischa.Sandberg@telus.net> writes:
> > Quoting Tom Lane <tgl@sss.pgh.pa.us>:
> >> WHERE a.x > b.y AND a.x < 42
>
> > Out of curiosity, will the planner induce "b.y < 42" out of this?
>
> No. There's some smarts about transitive equality, but none about
> transitive inequalities. Offhand I'm not sure if it'd be useful to add
> such. The transitive-equality code pulls its weight [...]
> but I'm less able to think of common use-cases for transitive
> inequality ...
Thanks. My apologies for not just going and looking at the code first.
Equality-transitives: yes, worth their weight in gold.
Inequality-transitivies: I see in OLAP queries (usually ranges), or in queries
against big UNION ALL views, where const false inequalities are the norm.
"a.x > b.y and a.x < c.z" comes up in OLAP, too, usually inside an EXISTS(...),
where you are doing something analogous to finding a path.
^ permalink raw reply [nested|flat] 12+ messages in thread
end of thread, other threads:[~2005-04-08 00:30 UTC | newest]
Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-04-06 22:09 Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient) Tom Lane <tgl@sss.pgh.pa.us>
2005-04-06 22:25 ` Jim C. Nasby <decibel@decibel.org>
2005-04-06 22:35 ` Tom Lane <tgl@sss.pgh.pa.us>
2005-04-06 22:54 ` Re: [HACKERS] Recognizing range constraints (was Re: Plan John A Meinel <john@arbash-meinel.com>
2005-04-06 23:06 ` Tom Lane <tgl@sss.pgh.pa.us>
2005-04-07 21:40 ` Jim C. Nasby <decibel@decibel.org>
2005-04-06 23:24 ` Re: Recognizing range constraints (was Re: Plan for Simon Riggs <simon@2ndquadrant.com>
2005-04-07 14:31 ` Bruno Wolff III <bruno@wolff.to>
2005-04-07 14:20 ` Tom Lane <tgl@sss.pgh.pa.us>
2005-04-07 21:26 ` Mischa <mischa.Sandberg@telus.net>
2005-04-07 23:58 ` Tom Lane <tgl@sss.pgh.pa.us>
2005-04-08 00:30 ` a3a18850@telus.net
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