From: | Dániel Dénes <panther-d(at)freemail(dot)hu> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | JOIN with ORDER on both tables does a sort when it souldn't |
Date: | 2007-05-27 16:49:32 |
Message-ID: | freemail.20070427184932.8285@fm06.freemail.hu |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Hi,
I have three tables involved in my problem:
forums_grps [means: Forum-Groups]
- id (PRIMARY KEY)
- title
forums [means: Forums]
- id (PRIMARY KEY)
- forum_group_id (NOT NULL, FOREIGN KEY)
- order (defines listing order of forums in the same forum_group)
INDEX: (forum_group_id, order)
sit_shw_fgr [means: Sites Show Forum-Groups]
- site_id (PRIMARY KEY)
- forum_group_id (PRIMARY KEY, FOREIGN KEY)
- order (defines listing order of shown forum_groups on a site)
INDEX: (site_id, order)
What I want to do is SELECT the forums shown on a given site,
ordered by sit_shw_fgr.order ASC, forums.order ASC. So the query is:
SELECT * FROM sit_shw_fgr JOIN forums
ON forums.forum_group_id = sit_shw_fgr.forum_group_id
WHERE sit_shw_fgr.site_id = 1
ORDER BY sit_shw_fgr.order ASC, forums.order ASC
If the plan uses a nestloop with both indexes I mentioned, it will get
the results in the correct order. But the planner will only choose this
plan, if I disable all other choices:
SET enable_seqscan TO false;
SET enable_hashjoin TO false;
SET enable_mergejoin TO false;
But even then, it won't realize that the result are in correct order, and
does a sort! Why?
Sort
Sort Key: sit_shw_fgr.order, forums.order
-> Nested Loop
-> Index Scan using sit_shw_fgr_idx_siteid_order on sit_shw_fgr
Index Cond: (sitid = 1)
-> Index Scan using forums_idx_forumgroupid_order on forums
Index Cond: (forums.fgrid = "outer".fgrid)
I'm using PostgreSQL 8.1.8.
Thanks for the answer in advance,
Denes Daniel
Végleges lézeres szőrtelenítés:jún. 30-ig most mindkét hónalj kezelése csak 79 000 Ft! Klikk ide a részleteketért!
http://www.webdesign.hu/aesthetica/flash_microsite/?id=8;p_code=2029
From | Date | Subject | |
---|---|---|---|
Next Message | ABHANG RANE | 2007-05-27 17:23:48 | CUBE SYNTAX |
Previous Message | Michael Glaesemann | 2007-05-27 16:15:17 | Re: User permissions/Data separation. |