pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Mitch Vincent <mitch@venux.net>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Sub-select speed.
Date: Fri, 17 Dec 1999 16:45:51 -0500
Message-ID: <10357.945467151@sss.pgh.pa.us> (raw)
In-Reply-To: <018301bf48bf$8a068620$0300000a@doot.org>

"Mitch Vincent" <mitch@venux.net> writes:
> select * from applicants as a where a.status = 'A' and a.app_id in(select
> b.app_id from resume_search as b where a.app_id=b.app_id and b.user_id=291)

WHERE ... IN (sub-select) is a very inefficient substitute for a plain join
query.  Try something like

select a.* from applicants as a, resume_search as b
where a.status = 'A' and a.app_id = b.app_id and b.user_id = 291;

Providing indexes on one or both app_id fields might help.  Also, don't
forget to do a VACUUM every so often to make sure the planner has useful
statistics about the sizes of the tables.

There has been some talk of automatically rewriting queries to eliminate
unnecessary sub-selects, but I don't foresee it getting done for a
while yet.

> insert into users_download_app (user_id,app_id) select app_id from
> applicants where status = 'A' and app_id in(select b.app_id from
> resume_search as b where app_id=b.app_id and b.user_id=291) limit 200

BTW, there's a bug in current sources: LIMIT is ignored by INSERT
... SELECT.  I think it works OK to do SELECT INTO ... LIMIT, however,
and then you could insert into the final destination table from the
INTO temp table.

			regards, tom lane



view thread (5+ messages)  latest in thread

Message-ID: <10357.945467151@sss.pgh.pa.us>
Permalink:  ../10357.945467151@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/10357.945467151@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, mitch@venux.net
  Subject: Re: [SQL] Sub-select speed.
  In-Reply-To: <10357.945467151@sss.pgh.pa.us>
  References: <018301bf48bf$8a068620$0300000a@doot.org> <10357.945467151@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox