Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a9x0p-000343-NQ for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Dec 2015 15:32:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a9x0p-0003c0-9k for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Dec 2015 15:32:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a9x0C-0002Co-8z for pgsql-bugs@postgresql.org; Fri, 18 Dec 2015 15:31:28 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a9x09-000727-DU for pgsql-bugs@postgresql.org; Fri, 18 Dec 2015 15:31:27 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id tBIFVKhb006539; Fri, 18 Dec 2015 10:31:20 -0500 From: Tom Lane To: michal.schwarz@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #13827: planner chooses more expensive plan than it should In-reply-to: <20151218143324.14453.55010@wrigleys.postgresql.org> References: <20151218143324.14453.55010@wrigleys.postgresql.org> Comments: In-reply-to michal.schwarz@gmail.com message dated "Fri, 18 Dec 2015 14:33:24 +0000" Date: Fri, 18 Dec 2015 10:31:20 -0500 Message-ID: <6538.1450452680@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org michal.schwarz@gmail.com writes: > => explain select 1 FROM nakupy_prodeje n, smlouvy s WHERE > n.smlouva_id=s.smlouva_id AND s.osoba_id='900316'; > QUERY PLAN > ----------------------------------------------------------------------------------------------------------------- > Nested Loop (cost=0.00..18415.76 rows=88 width=0) > -> Index Scan using smlouvy_osoba_id on smlouvy s (cost=0.00..678.38 > rows=170 width=4) > Index Cond: (osoba_id = 900316) > -> Index Only Scan using nakupy_prodeje_smlouva_id on nakupy_prodeje n > (cost=0.00..100.99 rows=335 width=4) > Index Cond: (smlouva_id = s.smlouva_id) > (5 rows) > Total expected cost is 18415 and 88 rows. OK. > Problematic behaviour is when I just add "AND n.datum_realizace is null" to > original WHERE condition. This query should be at least as fast as previous > query, because everything is absolutely the same, and only an ADDITIONAL > condition "AND n.datum_realizace is null" was used. But datum_realizace is not in the nakupy_prodeje_smlouva_id index. So it could not have done this "exactly the same"; it would have had to have used a regular index scan, which is a whole lot more expensive than the index-only scan because it involves fetching heap tuples too. Evidently the planner thinks the hash join is a better option than that. If you try turning off enable_hashjoin and enable_mergejoin, you'll probably get the nestloop/indexscan plan, and you'll be able to see what cost the planner is assigning to it; but it will certainly be higher than for the plan that got selected. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs