dbTalk Databases Forums  

subselect, order by and left join

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss subselect, order by and left join in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Morten K. Poulsen
 
Posts: n/a

Default subselect, order by and left join - 11-08-2004 , 04:18 AM






(re-post)

Dear list,

Please let me know if this is not the list to ask this kind of question.

I am trying to optimize a query that joins two relatively large (750000 rows in
each) tables. If I do it using a subselect, I can "force" the planner to choose
the fastest path. Now, my question is:

If I have a subselect with an ORDER BY, and I LEFT JOIN the result with the
other table, is the order maintained? Or is PostgreSQL free to return the rows
in any order, after the join?

My query is the following:

SELECT a.*
FROM (SELECT * FROM tree WHERE parent_id=1363405 ORDER BY order_index DESC) AS a
LEFT JOIN content AS b ON a.object_id=b.id
WHERE (b.onair = 't') LIMIT 1;

Thanks,
Morten

--
Morten K. Poulsen <morten-postgresql (AT) afdelingp (DOT) dk>
http://www.afdelingp.dk/

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)


Reply With Quote
  #2  
Old   
Stephan Szabo
 
Posts: n/a

Default Re: subselect, order by and left join - 11-08-2004 , 06:54 AM






On Mon, 8 Nov 2004, Morten K. Poulsen wrote:

Quote:
Please let me know if this is not the list to ask this kind of question.

I am trying to optimize a query that joins two relatively large (750000 rows in
each) tables. If I do it using a subselect, I can "force" the planner to choose
the fastest path. Now, my question is:

If I have a subselect with an ORDER BY, and I LEFT JOIN the result with the
other table, is the order maintained? Or is PostgreSQL free to return the rows
in any order, after the join?
AFAIK, you have no guarantees as to the output order unless you have
another order by. The join may destroy the ordering, so even if you get
the ordering you want right now, you shouldn't rely on it.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings



Reply With Quote
  #3  
Old   
Morten K. Poulsen
 
Posts: n/a

Default Re: subselect, order by and left join - 11-08-2004 , 08:58 AM



On Mon, Nov 08, 2004 at 04:54:40AM -0800, Stephan Szabo wrote:
Quote:
If I have a subselect with an ORDER BY, and I LEFT JOIN the result with the
other table, is the order maintained? Or is PostgreSQL free to return the
rows in any order, after the join?

AFAIK, you have no guarantees as to the output order unless you have another
order by. The join may destroy the ordering, so even if you get the ordering
you want right now, you shouldn't rely on it.
OK. Thanks for the reply.

Morten

--
Morten K. Poulsen <morten-postgresql (AT) afdelingp (DOT) dk>
http://www.afdelingp.dk/

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.