Re: [SQL] Please, HELP! Why is the query plan so wrong???

From: Dmitry Tkach <dmitry(at)openratings(dot)com>
To: Jie Liang <jie(at)stbernard(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] Please, HELP! Why is the query plan so wrong???
Date: 2002-07-12 14:34:27
Message-ID: 3D2EE8F3.5060904@openratings.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

Jie Liang wrote:

>I believe that SQL will use the index of join 'key' when you join the tables
>if
>have any, in your query the (a,c) is the join key but d is not.
>
>
>Jie Liang
>

Not really... I tried this:

explain select * from fb joing fbr on (fb.a=fbr.a and
fb.c=fbr.c and fbr.d is null) where fb.b=0

It results in the same query plan (seq scan on fbr).

Dima

>
>
>
>-----Original Message-----
>From: Dmitry Tkach [mailto:dmitry(at)openratings(dot)com]
>Sent: Thursday, July 11, 2002 3:51 PM
>To: pgsql-general(at)postgresql(dot)org; pgsql-sql(at)postgresql(dot)org
>Subject: [SQL] Please, HELP! Why is the query plan so wrong???
>
>
>Hi, everybody!
>
>Here is the problem:
>
>test=# create table fb (a int, b int, c datetime);
>CREATE
>test=# create table fbr (a int, c datetime, d int);
>CREATE
>test=# create unique index fb_idx on fb(b);
>CREATE
>test=# create index fbr_idx on fbr(a,c) where d is null;
>CREATE
>test=# set enable_seqscan=off;
>
>SET VARIABLE
>rapidb=# explain select * from fb, fbr where fb.b=0 and fb.a=fbr.a and
>fb.c=fbr.c and fbr.d is null;
>NOTICE: QUERY PLAN:
>
>Hash Join (cost=100000005.82..100001015.87 rows=1 width=32)
> -> Seq Scan on fbr (cost=100000000.00..100001010.00 rows=5 width=16)
> -> Hash (cost=5.81..5.81 rows=1 width=16)
> -> Index Scan using fb_idx on fb (cost=0.00..5.81 rows=1
>width=16)
>
>Could someone PLEASE explain to me, why doesn't it want to use the index on
>fbr?
>
>If I get rid of the join, then it works:
>
>test=# explain select * from fbr where a=1 and c=now() and d is null;
>NOTICE: QUERY PLAN:
>
>Index Scan using fbr_idx on fbr (cost=0.00..5.82 rows=1 width=16)
>
>What's the catch???
>
>Any help would be greatly appreciated!
>
>Thanks!
>
>Dima
>
>
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 3: if posting/reading through Usenet, please send an appropriate
>subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
>message can get through to the mailing list cleanly
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jean-Luc Lachance 2002-07-12 14:45:13 Re: 7.2.1 optimises very badly against 7.2
Previous Message Thomas Lockhart 2002-07-12 14:29:11 Re: syncronizing databases?

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2002-07-12 15:07:29 Re: rules / triggers on insert. why after?
Previous Message Jean-Luc Lachance 2002-07-12 14:28:17 Re: SQL problem with aggregate functions.