Re: exists

From: Joseph Shraibman <jks(at)selectacast(dot)net>
To: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: exists
Date: 2001-08-21 17:29:38
Message-ID: 3B829A82.4040709@selectacast.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Stephan Szabo wrote:
> On Tue, 21 Aug 2001, Joseph Shraibman wrote:
>
>
>>Thank you, I was missing the parens.
>>
>>If I do an explain I see:
>>
>>-> Index Scan using m_u_and_p_key on m (cost=0.00..3035.22 rows=1363 width=12)
>>
>>
>>even if I put a limit 1 on the select. Why is that?
>>
>
> Is that the inner query (on the exists) or the entire explain?

Just the inner query

>
> I guess it'd be useful to see the whole query and explain and maybe
> schema.

That's big and complicated. Can you reproduce this somewhere else?

Here is the whole explain:

Limit (cost=48.39..48.39 rows=1 width=70)
-> Sort (cost=48.39..48.39 rows=2 width=70)
-> Hash Join (cost=18.46..48.38 rows=2 width=70)
-> Index Scan using u_p_key on u (cost=0.00..27.66 rows=48 width=28)
-> Hash (cost=18.39..18.39 rows=28 width=42)
-> Seq Scan on d (cost=0.00..18.39 rows=28 width=42)
SubPlan
-> Nested Loop (cost=0.00..4.04 rows=1 width=20)
-> Index Scan using a_pkey on a (cost=0.00..2.01 rows=1 width=4)
-> Index Scan using p_pkey on pu (cost=0.00..2.02 rows=1 width=16)
-> Index Scan using m_u_and_p_key on m (cost=0.00..3035.22 rows=1363
width=44)

I tried to make a simple test case:

create table ta (a int);
create table tb (c int, b int);
insert into ta values (2);
insert into ta values (4);
insert into ta values (6);
insert into ta values (8);
insert into ta values (10);
insert into tb values (1,1);
insert into tb values (2,2);
insert into tb values (3,3);
insert into tb values (4,4);
vacuum analyze;

select c, b , exists(select a from ta where a = c) from tb;
explain select c, b , exists(select a from ta where a = c) from tb;

drop table ta;
drop table tb;

... but the data is so small it uses a seq scan:
Seq Scan on tb (cost=0.00..1.04 rows=4 width=8)
SubPlan
-> Seq Scan on ta (cost=0.00..1.06 rows=1 width=4)

>
>
>>Stephan Szabo wrote:
>>
>>>On Mon, 20 Aug 2001, Joseph Shraibman wrote:
>>>
>>>
>>>
>>>>I want to select a boolean if there exists a row in another table that matches this one.
>>>>So I did select ..., (select count(*) from table2 where ...) > 0 ...
>>>>but that count(*) was taking forever. I know there is a better way to do it, but whenever
>>>>I try to use EXISTS I get a syntax error. What is the proper way?
>>>>
>>>>
>>>Hmm, on current sources I can do:
>>>select ..., exists (select * from table2 where ...) from table1;
>>>
>>>I don't know if that's new though...
>>>

--
Joseph Shraibman
jks(at)selectacast(dot)net
Increase signal to noise ratio. http://www.targabot.com

In response to

  • Re: exists at 2001-08-21 16:45:25 from Stephan Szabo

Responses

  • Re: exists at 2001-08-21 17:57:53 from Stephan Szabo

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2001-08-21 17:57:53 Re: exists
Previous Message Stephan Szabo 2001-08-21 16:45:25 Re: exists