Improving PostgreSQL Aggregate Performance?

I googl'ed around, seems like postgreesql (up to 8.4) doesn't like MAX and MIN, it does a sequential scan of the table to get the result. It's hard to say that it's your case without the query plan and the version You can try this workaround SELECT id from mytable WHERE ref_id=2660 ORDER BY id DESC LIMIT 1 Edit: Make sure you have an index with (ref_id, id), otherwise a table scan/sort is inevitable.

I googl'ed around, seems like postgreesql (up to 8.4) doesn't like MAX and MIN, it does a sequential scan of the table to get the result. It's hard to say that it's your case without the query plan and the version. You can try this workaround.

SELECT id from mytable WHERE ref_id=2660 ORDER BY id DESC LIMIT 1 Edit: Make sure you have an index with (ref_id, id), otherwise a table scan/sort is inevitable.

This takes about a minute to run, which is a lot faster than my query, but still otherwise slow. – Cerin Feb 20 at 22:26 The (ref_id, id) index did the trick. Thanks.

– Cerin Feb 21 at 14:55.

It's not an answer. – arthurprs Feb 20 at 21:53.

I am using Postgres 8.4 and can say it may be a bug in Postgres optimizer to not using indexes for queries envolving min and max agregation functions. After changing my queries from Select max(field) from table to Select field from table order by field limit 1 my query execution time improved from 10s to less than a second. Of course You might define and index for the column in question, otherwise postgres will do a seq_scan.

I have a modest number of records associated with an id, which I can COUNT() in a second e.g. However, when I try and find the most recent record id using MAX(), the query takes nearly 5 minutes.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions