Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a8YzD-000333-Fk for pgsql-bugs@arkaria.postgresql.org; Mon, 14 Dec 2015 19:40:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a8YzD-0001CZ-23 for pgsql-bugs@arkaria.postgresql.org; Mon, 14 Dec 2015 19:40:43 +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 1a8RKR-0006QG-PH for pgsql-bugs@postgresql.org; Mon, 14 Dec 2015 11:30:07 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1a8RKN-00024X-Fm for pgsql-bugs@postgresql.org; Mon, 14 Dec 2015 11:30:07 +0000 MIME-Version: 1.0 Subject: BUG #13816: Query planner strange choose while select/count small part of big table To: pgsql-bugs@postgresql.org From: sienkomarcin@gmail.com Date: Mon, 14 Dec 2015 11:29:32 +0000 Message-ID: <20151214112932.2082.71114@wrigleys.postgresql.org> X-Pg-Spam-Score: -0.3 (/) Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" 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 The following bug has been logged on the website: Bug reference: 13816 Logged by: Marcin Sieńko Email address: sienkomarcin@gmail.com PostgreSQL version: 9.4.5 Operating system: Windows 7 x64 Description: Hi, I've check todo list but i can't find exact problem i'm reporting. It seems like query planner fires not needed sequence scan by all rows in table when only a few rows were picked. I will explain on example. I have 2 tables with relation 1 - n. Index on foreign key exist. On n side there are a lot o rows (about 4 000 000 in my db). When i select/count rows form n table joined/subselected from 1-side than planner traverse every of 4 000 000 rows although it has only 3 in "valid from where conditions". Execution takes 2191 ms on my pc. When i turn off sequence scan it takes 12 ms. Below i include query and explain analyze output with sequence scan on and off. Similar situation is without subquery (with joins). Query: select * --count(this_.id) as y0_ from -- shipment_order_sub_item this_ left outer join shipment_order_item orderitem1_ on this_.shipment_order_item_id=orderitem1_.id where orderitem1_.id in ( select oi_.id as y0_ from shipment_order_item oi_ inner join shipment_order order1_ on oi_.order_id=order1_.id inner join court_department courtdepar3_ on order1_.court_department_id=courtdepar3_.department_id inner join application_user user2_ on order1_.user_id=user2_.users_id where order1_.id = 610 and order1_.court_department_id in (1,292,32768 ) ) ; -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs