Skip site navigation (1) Skip section navigation (2)

Re: no index-usage on aggregate-functions?

From: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
To: "Harald Lau (Sector-X)" <harald(at)sector-x(dot)de>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: no index-usage on aggregate-functions?
Date: 2004-06-29 11:49:52
Message-ID: Pine.LNX.4.44.0406291337130.21809-100000@zigo.dhs.org (view raw or flat)
Thread:
Lists: pgsql-performance
On Tue, 29 Jun 2004, Harald Lau (Sector-X) wrote:

> > Average and sum can never use an index AFAIK, in any db server.  You 
> > need information from every row.
> 
> Take a look at the SQLSrv-pendant:
> create index x_1 on the_table (num_found)
> select avg(num_found) from the_table
> -> Index Scan(OBJECT:([midata].[dbo].[THE_TABLE].[x_1])

But is it really faster is the question?

This sum needs all the values in that column. As far as I know it uses the
index because it uses less space on disk and thus is a little faster due
to less IO. In pg the index doesn't work like that, so in pg it's faster
to sum all values using the table itself.

If you have a WHERE clause to only sum some values, then pg will use an
index (if applicable) and you will see a speedup.

For min and max the situation is different, there an index can give you
the answer without scanning all rows. For that the workaround exist in pg.  
The pg aggregate functions are very general and no one have special cased
min/max yet. Until that happen the work around works and is fast.

> So, it seems that PG is not soooo well suited for a datawarehouse and/or
> performing extensive statistics/calculations/reportings on large tables,
> is it?

I don't see how you can say that from your example. Just because it uses
an index for the sum above does not mean that it is a lot faster. It still 
have to do as many additions as pg has to do.

Sure, mvcc is best when you have both read and writes. But it should still
be comparable in speed even if you only do reads.

-- 
/Dennis Björklund


In response to

pgsql-performance by date

Next:From: Michal TáborskýDate: 2004-06-29 12:30:30
Subject: Slow INSERT
Previous:From: Harald Lau (Sector-X)Date: 2004-06-29 08:46:27
Subject: Re: no index-usage on aggregate-functions?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group