pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Vitaly Belman <vitalib@012.net.il>
To: pgsql-performance@postgresql.org
Subject: Additional select fields in a GROUP BY
Date: Sun, 13 Jun 2004 06:21:17 +0300
Message-ID: <2393942843.20040613062117@012.net.il> (raw)

Hello,

Consider the following query:

select t1field1, avg(t2fieild2)
from t1, t2
where t1.field1 = t2.field2
group by t1field1

That works fine. But I'd really like to see more fields of t1 in this
query, however I can't add them into the select because they're not
part of the GROUP BY, thus I have to add them to there too:

select t1field1, t1field2, t1field3, avg(t2fieild2)
from t1, t2
where t1.field1 = t2.field2
group by t1field1, t1field2, t1field3

The problem is that addind them all to GROUP BY causes a performance
loss.. The only solution I found is using a subquery like this:

select * from
t1, (select t1field1, avg(t2fieild2)
from t1, t2
where t1.field1 = t2.field2
group by t1field1) t1inner
where t1.field1 = t1inner.field1

It works just fine.. But I prefer not to use subqueries unless I am
really forced to due to the design of my application.

Another solution I considered is using aggreate function like that:

select t1field1, max(t1field2), max(t1field3), avg(t2fieild2)
from t1, t2
where t1.field1 = t2.field2
group by t1field1

Sadly, this caused the same performance... I wonder though, is it
possible to make an aggregate function like first(), last() in Oracle
(IIRC)? I believe that in such cases MySQL does first() by itself.

Other ideas are welcome too.
  

Regards,
 Vitaly Belman
 
 ICQ: 1912453
 AIM: VitalyB1984
 MSN: tmdagent@hotmail.com
 Yahoo!: VitalyBe




view thread (5+ messages)  latest in thread

Message-ID: <2393942843.20040613062117@012.net.il>
Permalink:  ../2393942843.20040613062117@012.net.il/
Also on:    postgresql.org/message-id/2393942843.20040613062117@012.net.il

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: vitalib@012.net.il
  Subject: Re: Additional select fields in a GROUP BY
  In-Reply-To: <2393942843.20040613062117@012.net.il>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox