JOIN with ORDER on both tables does a sort when it souldn't
От | Dániel Dénes |
---|---|
Тема | JOIN with ORDER on both tables does a sort when it souldn't |
Дата | |
Msg-id | freemail.20070427184932.8285@fm06.freemail.hu обсуждение исходный текст |
Ответы |
Re: JOIN with ORDER on both tables does a sort when it souldn't
|
Список | 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
В списке pgsql-general по дате отправления: