Re: Optimizing sum() operations

From: "Sean Davis" <sdavis2(at)mail(dot)nih(dot)gov>
To: "Dobes Vandermeer" <dobesv(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Optimizing sum() operations
Date: 2008-10-03 19:23:42
Message-ID: 264855a00810031223n68fea879q622443eefb950770@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Fri, Oct 3, 2008 at 3:13 PM, Dobes Vandermeer <dobesv(at)gmail(dot)com> wrote:
> On Fri, Oct 3, 2008 at 4:51 AM, Sean Davis <sdavis2(at)mail(dot)nih(dot)gov> wrote:
>> On Fri, Oct 3, 2008 at 4:51 AM, Dobes Vandermeer <dobesv(at)gmail(dot)com> wrote:
>>> I'm currently using sum() to compute historical values in reports;
>>> basically select sum(amount) on records where date <= '...' and date
>>>>= '...' who = X.
>>>
>>> Second, if this is a concern, is there a best practice for optimizing
>>> these kinds of queries?
>>
>> You'll need to test to see what performance you get. That said,
>> indexing is a good place to start. You can always run explain and
>> explain analyze on the queries to double-check the planner.
>
> Could I create an index that includes a sum() function - like:
>
> create index sumidx on records (who, date, sum(amount)) ?
>
> I'm sure that theoretically this is possible, but does postgres support it?

I'm not sure what you want to do. Trying to make an index on a sum()
doesn't make any sense because the sum() depends on the rows used in a
query; i.e., sum() is an aggregate and cannot be used in an index.
You CAN index functions that operate on a single row.

What is wrong with an index on who and date and then doing the sum?

Sean

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Dobes Vandermeer 2008-10-03 22:38:50 Re: Optimizing sum() operations
Previous Message Dobes Vandermeer 2008-10-03 19:13:59 Re: Optimizing sum() operations