Unix Technical Forum

Avoiding sub-query with group by

This is a discussion on Avoiding sub-query with group by within the pgsql Sql forums, part of the PostgreSQL category; --> Hi, I think the following query is O(n^2) in the number of table rows (see details below, including "explain" ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 06:01 PM
a.cooke@isti.com
 
Posts: n/a
Default Avoiding sub-query with group by


Hi,

I think the following query is O(n^2) in the number of table rows (see
details below, including "explain" output). Is there any way to make
it linear?

Given a table like
val | rmp | xtr
-----+-----+-----
1 | 1 | a
1 | 2 | b
1 | 3 | c
2 | 1 | d
2 | 2 | e
2 | 3 | f
I want to select
val | rmp | xtr
-----+-----+-----
1 | 2 | b
2 | 3 | f
ie the row where rmp > val, but just the first such value (smallest
rmp).

I can do this with:
andrew=# select * from tbl inner join (select val as v, min(rmp) as mr
from tbl where rmp > val group by val) as x on tbl.val=x.v and
tbl.rmp=x.mr;
val | rmp | xtr | v | mr
-----+-----+-----+---+----
1 | 2 | b | 1 | 2
2 | 3 | f | 2 | 3
(2 rows)

But it requires two scans:
andrew=# create index tbl_index_val on tbl (val);
CREATE INDEX
andrew=# explain select * from tbl inner join (select val as v,
min(rmp) as mr from tbl where rmp > val group by val) as x on
tbl.val=x.v and tbl.rmp=x.mr;
QUERY PLAN
---------------------------------------------------------------------
Hash Join (cost=1.16..2.27 rows=1 width=24)
Hash Cond: ((public.tbl.val = x.v) AND (public.tbl.rmp = x.mr))
-> Seq Scan on tbl (cost=0.00..1.06 rows=6 width=16)
-> Hash (cost=1.13..1.13 rows=2 width=8)
-> HashAggregate (cost=1.08..1.11 rows=2 width=8)
-> Seq Scan on tbl (cost=0.00..1.07 rows=2 width=8)
Filter: (rmp > val)
(7 rows)

Thanks,
Andrew

PS I cannot preview this message; I hope it is formatted OK.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 06:01 PM
a.cooke@isti.com
 
Posts: n/a
Default Re: Avoiding sub-query with group by


Or maybe those scans are not nested and so it's O(n), not O(n^2)?

Sorry, I am not experienced using "explain".

Andrew

> QUERY PLAN
> ---------------------------------------------------------------------
> Hash Join (cost=1.16..2.27 rows=1 width=24)
> Hash Cond: ((public.tbl.val = x.v) AND (public.tbl.rmp = x.mr))
> -> Seq Scan on tbl (cost=0.00..1.06 rows=6 width=16)
> -> Hash (cost=1.13..1.13 rows=2 width=8)
> -> HashAggregate (cost=1.08..1.11 rows=2 width=8)
> -> Seq Scan on tbl (cost=0.00..1.07 rows=2 width=8)
> Filter: (rmp > val)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:38 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com