Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a9i67-00087S-3p for pgsql-bugs@arkaria.postgresql.org; Thu, 17 Dec 2015 23:36:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a9i66-0007kl-Ma for pgsql-bugs@arkaria.postgresql.org; Thu, 17 Dec 2015 23:36:34 +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 1a9Ysf-0007t4-9Z for pgsql-bugs@postgresql.org; Thu, 17 Dec 2015 13:46:05 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1a9Ysc-0003gs-Iw for pgsql-bugs@postgresql.org; Thu, 17 Dec 2015 13:46:04 +0000 MIME-Version: 1.0 Subject: BUG #13824: EXISTS sometimes uses seq scan instead of index To: pgsql-bugs@postgresql.org From: grzegorz@thulium.pl Date: Thu, 17 Dec 2015 13:45:02 +0000 Message-ID: <20151217134502.9563.4560@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.2 (-) Content-Transfer-Encoding: 7bit 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: 13824 Logged by: Grzegorz G Email address: grzegorz@thulium.pl PostgreSQL version: 9.4.5 Operating system: Debian Description: Seems related to (9.4.4,9.3.9) Improve planner's cost estimates for semi-joins and anti-joins with inner indexscans (Tom Lane, Tomas Vondra) I have 2 tables: master(t_outbound) and detail (t_outbound_number). I want to select only those records from t_outbound that have detailed records in t_outbound number in certain statuses (id_status). There is an index on t_outbound_number: "t_on_id_status_1_or_9" btree (id_outbound) WHERE id_status = 1 OR id_status = 9 Whenever I perform select like below, planner thinks it's going to look up many rows and falls back to seq scan. If I disable seq scan, it correctly uses the index. When the t_outbound_number table grows and percentage of records with status 1 or 9 decreases, index is used. Seems like planner thinks it's going to retrieve all of the records to verify EXISTS clause, not just one. psql=> EXPLAIN ANALYZE SELECT id_outbound, (EXISTS (SELECT 1 FROM t_outbound_number ton WHERE (id_status = 1 OR id_status = 9) AND ton.id_outbound=tou.id_outbound)) from t_outbound tou ORDER BY id ASC; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=2.58..2.59 rows=5 width=10) (actual time=94.805..94.806 rows=5 loops=1) Sort Key: tou.id Sort Method: quicksort Memory: 25kB -> Seq Scan on t_outbound tou (cost=0.00..2.52 rows=5 width=10) (actual time=62.955..94.763 rows=5 loops=1) SubPlan 1 -> Seq Scan on t_outbound_number ton (cost=0.00..11910.24 rows=40453 width=0) (actual time=18.942..18.942 rows=1 loops=5) Filter: ((id_outbound = tou.id_outbound) AND ((id_status = 1) OR (id_status = 9))) Rows Removed by Filter: 114596 Planning time: 0.775 ms Execution time: 94.853 ms psql=> set enable_seqscan =off; SET psql=> EXPLAIN ANALYZE SELECT id_outbound, (EXISTS (SELECT 1 FROM t_outbound_number ton WHERE (id_status = 1 OR id_status = 9) AND ton.id_outbound=tou.id_outbound)) from t_outbound tou ORDER BY id ASC; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------- Index Scan using t_outbound_id_key on t_outbound tou (cost=0.13..14.69 rows=5 width=10) (actual time=0.033..0.103 rows=5 loops=1) SubPlan 1 -> Index Scan using t_on_id_status_1_or_9 on t_outbound_number ton (cost=0.42..3082.02 rows=40453 width=0) (actual time=0.015..0.015 rows=1 loops=5) Index Cond: (id_outbound = tou.id_outbound) Planning time: 0.811 ms Execution time: 0.142 ms -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs