Unix Technical Forum

Postgres8: subselect and optimizer/planner

This is a discussion on Postgres8: subselect and optimizer/planner within the Pgsql General forums, part of the PostgreSQL category; --> Hi, I am fairly new to EXPLAIN, butl working on it. ;-) I have a few slow running queries ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 11:04 PM
Erwin Moller
 
Posts: n/a
Default Postgres8: subselect and optimizer/planner

Hi,

I am fairly new to EXPLAIN, butl working on it. ;-)
I have a few slow running queries I am trying to optimize.

First thing I wonder: I sometimes (lazy) add a subselect to queries.
A stupid example to clearify what I mean:

SELECT U.userid, U.username,
(SELECT G.groupname FROM tblgroup WHERE (G.userid=U.userid)) AS ingroup
FROM tbluser WHERE (bla..bla...);

Will this approach be slower than a regular join?

I mean, will this construct 'force' a repetitive query for each result,
or will Postgres8 see my clumpsy construct, and make a join of it
internally?

Or is my question too general and is the answer 'it depends'?

I found a lot of queries I wrote like that in earlier projects, and I
wonder if I should fix them.
Thanks for any insights!

Regards,
Erwin Moller

--



-------------------
Erwin Moller
Darwine BV

Groenendaal 25f
3011 SK Rotterdam
tel 010-2133996
-------------------


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 11:04 PM
Erwin Moller
 
Posts: n/a
Default Re: Postgres8: subselect and optimizer/planner

Erwin Moller wrote:
>
> SELECT U.userid, U.username,
> (SELECT G.groupname FROM tblgroup WHERE (G.userid=U.userid)) AS ingroup


typo, that should be 'tblgroup as G' of course.

> FROM tbluser WHERE (bla..bla...);



-------------------
Erwin Moller
Darwine BV

Groenendaal 25f
3011 SK Rotterdam
tel 010-2133996
-------------------


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 11:04 PM
Tom Lane
 
Posts: n/a
Default Re: Postgres8: subselect and optimizer/planner

Erwin Moller <erwin@darwine.nl> writes:
> SELECT U.userid, U.username,
> (SELECT G.groupname FROM tblgroup WHERE (G.userid=U.userid)) AS ingroup
> FROM tbluser WHERE (bla..bla...);


> Will this approach be slower than a regular join?


Probably; it's unlikely to be faster anyway. The best plan you'll get
from this is equivalent to a nestloop with inner indexscan on
tblgroup.userid. Now that might be the best plan anyway, or it might
not --- if you are selecting many rows from ingroup it's likely to suck.

> Or is my question too general and is the answer 'it depends'?


The only way I could see for this way to win would be if a nestloop is
actually the fastest plan, but the planner misestimates and decides to
use merge or hash join instead. Which could happen :-(

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org/

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 05:28 AM.


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