Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id A5826B5DCCD for ; Tue, 12 Jul 2011 17:13:08 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 82702-02 for ; Tue, 12 Jul 2011 20:13:02 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 8AD17B5DBC1 for ; Tue, 12 Jul 2011 17:13:02 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.4/8.14.4) with ESMTP id p6CKD0Jt083472 for ; Tue, 12 Jul 2011 17:13:00 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.4/8.14.4/Submit) id p6CJtJ2w039970 for pgsql-performance@postgresql.org; Tue, 12 Jul 2011 16:55:19 -0300 (ADT) (envelope-from news) From: Clem Dickey X-Newsgroups: pgsql.performance Subject: Re: Planner choosing NestedLoop, although it is slower... Date: Tue, 12 Jul 2011 12:55:14 -0700 Organization: Hub.Org Networking Services Lines: 44 Message-ID: References: <4E1C8E62.2090800@megafon.hr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110621 Fedora/3.1.11-1.fc14 Thunderbird/3.1.11 In-Reply-To: <4E1C8E62.2090800@megafon.hr> To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.01 tagged_above=-10 required=5 tests=BAYES_40=-0.001, DOS_RCVD_IP_TWICE_B=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201107/145 X-Sequence-Number: 44357 On 07/12/2011 11:11 AM, Mario Splivalo wrote: > Hi, all. > > I have a query, looking like this: > SELECT > pub_date > FROM > tubesite_object > INNER JOIN tubesite_image > ON tubesite_image.object_ptr_id = tubesite_object.id > WHERE > tubesite_object.site_id = 8 > AND tubesite_object.pub_date < E'2011-07-12 13:25:00' > ORDER BY > tubesite_object.pub_date ASC > LIMIT 21; > > Why is planner using NestedLoops, that is, what can I do to make him NOT > to use NestedLoops (other than issuing SET enable_nestloop TO false; > before each query) ? The planner is using a nested loops because the startup overhead is less, and it think that it will only have run a small 0.2% (21/9404) of the loops before reaching your limit of 21 results. In fact it has to run all the loops, because there are 0 results. (Is that what you expected?) Try a using CTE to make the planner think you are going to use all the rows of the joined table. That may cause the planner to use a merge join, which has higher startup cost (sort) but less overall cost if it the join will not finish early. WITH t AS ( SELECT tubesite_object.site_id AS site_id, tubesite_object.pub_date as pub_date FROM tubesite_object INNER JOIN tubesite_image ON tubesite_image.object_ptr_id = tubesite_object.id ) SELECT pub_date FROM t WHERE t.site_id = 8 AND t.pub_date < E'2011-07-12 13:25:00' ORDER BY t.pub_date ASC LIMIT 21;