Re: why query plan for the inner SELECT of WHERE x IN is wrong, but when run the inner query alone is OK?

From: Miernik <public(at)public(dot)miernik(dot)name>
To: pgsql-performance(at)postgresql(dot)org
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: 2008-08-10 02:20:59
Message-ID: 20080810022059.7330.0.NOFFLE@turbacz.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Miernik <public(at)public(dot)miernik(dot)name> 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/

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Scott Carey 2008-08-10 03:25:07 Re: why query plan for the inner SELECT of WHERE x IN is wrong, but when run the inner query alone is OK?
Previous Message Miernik 2008-08-09 22:32:08 Re: why query plan for the inner SELECT of WHERE x IN is wrong, but when run the inner query alone is OK?