Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id E575D650080 for ; Sat, 9 Aug 2008 23:39:29 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 88565-10 for ; Sat, 9 Aug 2008 23:39:20 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by postgresql.org (Postfix) with ESMTP id A7DD8650078 for ; Sat, 9 Aug 2008 23:39:26 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1KS0q3-0003J4-VQ for pgsql-performance@postgresql.org; Sun, 10 Aug 2008 02:39:23 +0000 Received: from apn-77-112-108-137.gprs.plus.pl ([77.112.108.137]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 10 Aug 2008 02:39:23 +0000 Received: from public by apn-77-112-108-137.gprs.plus.pl with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 10 Aug 2008 02:39:23 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-performance@postgresql.org From: Miernik Subject: Re: why query plan for the inner SELECT of WHERE x IN is wrong, but when run the inner query alone is OK? Date: Sun, 10 Aug 2008 04:20:59 +0200 Lines: 34 Message-ID: <20080810022059.7330.0.NOFFLE@turbacz.local> References: <20080809203435.7227.0.NOFFLE@turbacz.local> <20080809223208.7297.0.NOFFLE@turbacz.local> Reply-To: Miernik X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: apn-77-112-108-137.gprs.plus.pl X-Archive: encrypt=none User-Agent: tin/1.9.3-20080506 ("Dalintober") (UNIX) (Linux/2.6.25-2-686 (i686)) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200808/93 X-Sequence-Number: 30974 Miernik wrote: > I present a SELECT uid plan with the 1000 table also below, just to be > sure, this is the "bad" plan, that takes forever: > > miernik=> EXPLAIN SELECT uid FROM cnts WHERE uid IN (SELECT uid FROM alog WHERE pid = 3452654 AND o = 1); > QUERY PLAN > ----------------------------------------------------------------------------------------------- > Nested Loop IN Join (cost=0.00..3532.70 rows=1 width=4) > -> Seq Scan on cnts (cost=0.00..26.26 rows=1026 width=4) > -> Index Scan using alog_uid_idx on alog (cost=0.00..297.32 rows=1 width=4) > Index Cond: ((alog.uid)::integer = (cnts.uid)::integer) > Filter: ((alog.pid = 3452654::numeric) AND (alog.o = 1::numeric)) > (5 rows) If I reduce the number of rows in cnts to 100, I can actually make an EXPLAIN ANALYZE with this query plan finish in reasonable time: miernik=> EXPLAIN ANALYZE SELECT uid FROM cnts WHERE uid IN (SELECT uid FROM alog WHERE pid = 555949 AND odp = 1); QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop IN Join (cost=0.00..3585.54 rows=1 width=4) (actual time=51831.430..267844.815 rows=7 loops=1) -> Seq Scan on cnts (cost=0.00..14.00 rows=700 width=4) (actual time=0.005..148.464 rows=100 loops=1) -> Index Scan using alog_uid_idx on alog (cost=0.00..301.02 rows=1 width=4) (actual time=2676.959..2676.959 rows=0 loops=100) Index Cond: ((alog.uid)::integer = (cnts.uid)::integer) Filter: ((alog.pid = 555949::numeric) AND (alog.odp = 1::numeric)) Total runtime: 267844.942 ms (6 rows) The real running times are about 10 times more than the estimates. Is that normal? -- Miernik http://miernik.name/