Re: Grouping Too Closely - Mailing list pgsql-sql

From Russell Simpkins
Subject Re: Grouping Too Closely
Date
Msg-id BAY103-F8E562EE568057C24D58B9B5EA0@phx.gbl
Whole thread Raw
In response to Grouping Too Closely  ("Thomas F. O'Connell" <tfo@sitening.com>)
List pgsql-sql
I'm not sure if this is the best thing to do in all occasions, but I have 
found a great speed increase using unions over group by.

select fkey, uid, seq2 from mytable where seq2 > 2 and seq1 = ( select 
min(seq1) from mytable);
union
select fkey, uid, seq2 from mytable where seq2 > 2 and seq1 = ( select 
min(seq1) from mytable);
order by fkey, uid, seq2;

the union clause with remove your duplicates for you as you were doing with 
your group by.

using min on large tables can cause problems. you may want to do your select 
min(seq1) from mytable or even have a trigger function after insert/update 
that checks the new value against the current lowest stored in another 
table.

not sure if this helps, but i hope it does.

russ




pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: optimizer, view, union
Next
From: Markus Bertheau
Date:
Subject: empty view, replace view, column type change?