Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id B8837B5DC27 for ; Tue, 12 Jul 2011 23:04:42 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 59806-10 for ; Wed, 13 Jul 2011 02:04:36 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out2.mail.amis.hr (out2.mail.amis.hr [188.129.0.5]) by mail.postgresql.org (Postfix) with ESMTP id 4B268B5DC26 for ; Tue, 12 Jul 2011 23:04:36 -0300 (ADT) Received: from out.fsec.sm.srv.amis.hr (out.fsec.sm.srv.amis.hr [192.168.106.22]) by out2.mail.amis.hr (Postfix) with ESMTP id 204B31FD20 for ; Wed, 13 Jul 2011 04:04:36 +0200 (CEST) Received: from pps.filterd (out [127.0.0.1]) by out.fsec.sm.srv.amis.hr (8.14.3/8.14.3) with SMTP id p6D24apN018366 for ; Wed, 13 Jul 2011 04:04:36 +0200 Received: from smtp.amis.hr (smtp.sm.srv.amis.hr [192.168.106.39]) by out.fsec.sm.srv.amis.hr with ESMTP id xgkdxg78f-1 for ; Wed, 13 Jul 2011 04:04:35 +0200 A-Original-Client-UID: 188.129.65.237 splivalo Received: from [192.168.10.1] (cpe-188-129-65-237.dynamic.amis.hr [188.129.65.237]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: splivalo) by smtp.amis.hr (Postfix) with ESMTPSA id CE3D2265 for ; Wed, 13 Jul 2011 04:04:35 +0200 (CEST) Message-ID: <4E1CFD33.7040407@megafon.hr> Date: Wed, 13 Jul 2011 04:04:35 +0200 From: Mario Splivalo User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Planner choosing NestedLoop, although it is slower... References: <4E1C8E62.2090800@megafon.hr> <4626.1310501052@sss.pgh.pa.us> <4E1CC576.2080200@megafon.hr> <18737.1310510362@sss.pgh.pa.us> <4E1CEC8C.3000508@megafon.hr> In-Reply-To: <4E1CEC8C.3000508@megafon.hr> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.4.6813, 1.0.211, 0.0.0000 definitions=2011-07-13_01:2011-07-12, 2011-07-13, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=1 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=6.0.2-1012030000 definitions=main-1107120211 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201107/132 X-Sequence-Number: 44344 On 07/13/2011 02:53 AM, Mario Splivalo wrote: > On 07/13/2011 12:39 AM, Tom Lane wrote: >> Mario Splivalo writes: >>> On 07/12/2011 10:04 PM, Tom Lane wrote: >>>> What you need to look into is why the estimated join size is 9400 rows >>>> when the actual join size is zero. Are both tables ANALYZEd? Are you >>>> intentionally selecting rows that have no join partners? >> >>> Yes, both tables have been ANALYZEd. What do you mean, intentilnaly >>> selecting rows taht have no join partners? >> >> I'm wondering why the actual join size is zero. That seems like a >> rather unexpected case for a query like this. > > Yes, seems that planer gets confused by LIMIT. This query: > > select * from tubesite_object join tubesite_image on id=object_ptr_id > where site_id = 8 and pub_date < '2011-07-12 13:25:00' order by pub_date > desc ; > > Does not choose Nested Loop, and is done instantly (20 ms), and returns > no rows. However, if I add LIMIT at the end, it chooses NestedLoop and > it takes 500ms if I'm alone on the server, and 10+ seconds if there 50+ > connections on the server. As explained/suggested by RhodiumToad on IRC, adding composite index on (site_id, pub_date) made nestedloop query finish in around 100 seconds! Thank you! Mario