Re: select max(column) not using index

Lists: pgsql-hackers
From: Thomas Zehetbauer <thomasz(at)hostmaster(dot)org>
To: pgsql-hackers(at)postgresql(dot)org
Subject: select max(column) not using index
Date: 2002-03-07 16:04:47
Message-ID: 20020307170447.A29650@hostmaster.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I think you all should really buy the book 'Database Development for Dummies'.
Postgresql is for sure the only database on this planet that cannot optimize a
select(max) using an index. Not even Microsoft has implemented such a design
deficiency yet and even MySQL which you like to talk so bad about uses an
index to optimize select max() queries. Some of you should really consider
attending a programming course and all of you should consider to stop working
on this totally screwed up monster!

Tom

Nirvana: Zustand des Gluecks durch Ausloeschung des Selbst.
--
T h o m a s Z e h e t b a u e r ( TZ251 )
PGP encrypted mail preferred - KeyID 96FFCB89
mail pgp-key-request(at)hostmaster(dot)org


From: Gavin Sherry <swm(at)linuxworld(dot)com(dot)au>
To: Thomas Zehetbauer <thomasz(at)hostmaster(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: select max(column) not using index
Date: 2002-03-13 15:37:09
Message-ID: Pine.LNX.4.21.0203140236240.11709-100000@linuxworld.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, 7 Mar 2002, Thomas Zehetbauer wrote:

> I think you all should really buy the book 'Database Development for Dummies'.
> Postgresql is for sure the only database on this planet that cannot optimize a
> select(max) using an index. Not even Microsoft has implemented such a design
> deficiency yet and even MySQL which you like to talk so bad about uses an
> index to optimize select max() queries. Some of you should really consider
> attending a programming course and all of you should consider to stop working
> on this totally screwed up monster!

I real man would proffer criticism in diff -u format.

Gavin


From: mlw <markw(at)mohawksoft(dot)com>
To: Thomas Zehetbauer <thomasz(at)hostmaster(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: select max(column) not using index
Date: 2002-03-13 16:03:31
Message-ID: 3C8F7853.C42CB2B4@mohawksoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Thomas Zehetbauer wrote:
>
> I think you all should really buy the book 'Database Development for Dummies'.
> Postgresql is for sure the only database on this planet that cannot optimize a
> select(max) using an index. Not even Microsoft has implemented such a design
> deficiency yet and even MySQL which you like to talk so bad about uses an
> index to optimize select max() queries. Some of you should really consider
> attending a programming course and all of you should consider to stop working
> on this totally screwed up monster!
>
> Tom

The query:

select max from table order by max desc limit 1

Will do it, but "max()" is by no means an easy to optimize function. Aggregates
have an assumption of a range scan, especially custom aggregates. What about
this:

select max(foo) from bar where x = 'y';

How is the index used in this query?

The only instance where an aggregate optimization would pay off is when there
is no selection criteria and there is an index on the field. In this case, it
is easy enough to create a function for the particular application.

I hear and understand your frustration, yes PostgreSQL should be able to do
that, and maybe it would be worth the time and effort, that is not for me to
say, however there is a very viable work around for the problem you state and
the stated problem, while a common query, is a small subset of the actual
capability of the max() function.


From: Hannu Krosing <hannu(at)tm(dot)ee>
To: Thomas Zehetbauer <thomasz(at)hostmaster(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: select max(column) not using index
Date: 2002-03-13 16:14:49
Message-ID: 1016036094.30674.8.camel@taru.tm.ee
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, 2002-03-07 at 18:04, Thomas Zehetbauer wrote:
> I think you all should really buy the book 'Database Development for Dummies'.
> Postgresql is for sure the only database on this planet that cannot optimize a
> select(max) using an index.

PostgreSQL is extensible enough that luser can define max() to mean
anything and thus you don't have a general way to optimise it without
breaking some cases.

If you know that max(x) means the biggest x there is and you have a
b-tree index on x you can use:

select x from t order by x desc limit 1;

> Not even Microsoft has implemented such a design deficiency yet and

It would be a very microsofty way to optimise in ways that sometimes
produce wrong results ;)

> even MySQL which you like to talk so bad about uses an
> index to optimize select max() queries.

What do you need the superfast max() for ?

If you are trying to re-implement sequences you may yet find some
surprises.

> Some of you should really consider
> attending a programming course and all of you should consider to stop working
> on this totally screwed up monster!

Did you make yourself look bad by assuming that postgreSQL _does_ your
suggested optimisation ?

> Nirvana: Zustand des Gluecks durch Ausloeschung des Selbst.

How is this related to above ??

-------------
Hannu


From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Thomas Zehetbauer <thomasz(at)hostmaster(dot)org>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: select max(column) not using index
Date: 2002-03-13 16:35:00
Message-ID: 20020313082734.W81426-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, 7 Mar 2002, Thomas Zehetbauer wrote:

> I think you all should really buy the book 'Database Development for Dummies'.
> Postgresql is for sure the only database on this planet that cannot optimize a
> select(max) using an index. Not even Microsoft has implemented such a design
> deficiency yet and even MySQL which you like to talk so bad about uses an
> index to optimize select max() queries. Some of you should really consider
> attending a programming course and all of you should consider to stop working
> on this totally screwed up monster!

I'm not sure why I'm bothering to respond, but...

Given that postgres allows user defined aggregates and I guess it'd be
possible for a user to redefine max into some form where the optimization
isn't valid (I'm not sure why mind you, but...) that'd mean that the
optimization is not always available. Personally, I'd generally prefer
correct and slow over incorrect and fast. I'm fairly sure that if you made
a patch that cleanly dealt with the issue without programming in special
knowledge of min and max it'd be considered for inclusion.