Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-08 Thread Dimitri
BTW, will it improve something if you change your index to "my_table( id, the_date )"? Rgds, -Dimitri On 9/5/07, JS Ubei <[EMAIL PROTECTED]> wrote: > Hi all, > > I need to improve a query like : > > SELECT id, min(the_date), max(the_date) FROM my_table GROUP BY id; > > Stupidly, I create a B-tree

Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread Tom Lane
Gregory Stark <[EMAIL PROTECTED]> writes: > You can simulate such a plan with the subqueries I described but > there's a bit more overhead than necessary and you need a reasonably > efficient source of the distinct ids. Yeah, that seems like the $64 question. If you have no better way of finding

Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread Gregory Stark
"Pavel Stehule" <[EMAIL PROTECTED]> writes: >> >> why not >> >> select id, >>min(the_date) as min_date, >>max(the_date) as max_date >> from my_table group by id; >> >> Since 8.0 or was it earlier this will use an index should a reasonable one >> exist. As I mentioned in the othe

Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread Gregory Stark
"Peter Childs" <[EMAIL PROTECTED]> writes: > My personal reaction is why are you using distinct at all? > > why not > > select id, >min(the_date) as min_date, >max(the_date) as max_date > from my_table group by id; > > Since 8.0 or was it earlier this will use an index should a r

Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread Gregory Stark
"hubert depesz lubaczewski" <[EMAIL PROTECTED]> writes: > On Wed, Sep 05, 2007 at 12:30:21PM +0100, Gregory Stark wrote: >> SELECT DISTINCT ON (id) id, the_date AS min_date FROM my_table ORDER BY id, >> the_date ASC >> SELECT DISTINCT ON (id) id, the_date AS max_date FROM my_table ORDER BY id, >

Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread hubert depesz lubaczewski
On Wed, Sep 05, 2007 at 12:30:21PM +0100, Gregory Stark wrote: > SELECT DISTINCT ON (id) id, the_date AS min_date FROM my_table ORDER BY id, > the_date ASC > SELECT DISTINCT ON (id) id, the_date AS max_date FROM my_table ORDER BY id, > the_date DESC > I think the first of these can actually use y

Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread Pavel Stehule
> > why not > > select id, >min(the_date) as min_date, >max(the_date) as max_date > from my_table group by id; > > Since 8.0 or was it earlier this will use an index should a reasonable one > exist. without any limits, seq scan is optimal. Regards Pavel Stehule

Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread Peter Childs
On 05/09/07, Gregory Stark <[EMAIL PROTECTED]> wrote: > > "Gregory Stark" <[EMAIL PROTECTED]> writes: > > > "JS Ubei" <[EMAIL PROTECTED]> writes: > > > >> I need to improve a query like : > >> > >> SELECT id, min(the_date), max(the_date) FROM my_table GROUP BY id; > >... > > I don't think you'll fi

Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread Gregory Stark
"Gregory Stark" <[EMAIL PROTECTED]> writes: > "JS Ubei" <[EMAIL PROTECTED]> writes: > >> I need to improve a query like : >> >> SELECT id, min(the_date), max(the_date) FROM my_table GROUP BY id; >... > I don't think you'll find anything much faster for this particular query. You > could profile ru

Re: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread Gregory Stark
"JS Ubei" <[EMAIL PROTECTED]> writes: > Hi all, > > I need to improve a query like : > > SELECT id, min(the_date), max(the_date) FROM my_table GROUP BY id; > > Stupidly, I create a B-tree index on my_table(the_date), witch is logically > not used in my query, because it's not with a constant ? isn

FW: [PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread Gregory Williamson
bad address kep his from going to the list on my first try ... apologies to the moderators. -Original Message- From: Gregory Williamson Sent: Wed 9/5/2007 4:59 AM To: JS Ubei; pgsql-performance@postgresql.org Subject: RE: [PERFORM] optimize query with a maximum(date) extraction In

[PERFORM] optimize query with a maximum(date) extraction

2007-09-05 Thread JS Ubei
Hi all, I need to improve a query like : SELECT id, min(the_date), max(the_date) FROM my_table GROUP BY id; Stupidly, I create a B-tree index on my_table(the_date), witch is logically not used in my query, because it's not with a constant ? isn't it ? I know that I can't create a function inde