Help with multistage query

From: "Rick Schumeyer" <rschumeyer(at)ieee(dot)org>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Help with multistage query
Date: 2005-09-07 17:57:44
Message-ID: 000001c5b3d5$a67a3540$0300a8c0@dell8200
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I have a perl script that issues a series of SQL statements to perform some
queries. The script works, but I believe there must be a more elegant way
to do this.

The simplified queries look like this:

SELECT id FROM t1 WHERE condition1; ;returns about 2k records which are
stored in @idarray

foreach $id (@idarray) {

SELECT x FROM t2 WHERE id=$id; ; each select returns about 100 records
which are saved in a perl variable

}

At this point I have a list of about 200k records, from which I can manually
filter based on x values.

There are indices on id in both t1 and t2, so the first two queries are both
index scans. I cannot afford a table scan on t2 due to the size of the
table.

Like I said, this works (and uses only index scans), but I would think it
would be better to somehow select the 200k records into a temp table.
Because the temp table would be relatively small, a seq scan is ok to
produce my final list.

Also, I am now issuing the second query about 2k times.this seems
inefficient.

I would think there would a way to restate the first two queries as either
a join or a subselect. My initial attempts result in a table scan
(according to EXPLAIN) on t2.

For example I tried

SELECT x FROM t2 WHERE id in ( SELECT id FROM t1 WHERE condition1);

but this gives a seq scan.

Any ideas are appreciated.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2005-09-07 18:01:55 Re: uuid type for postgres
Previous Message Tom Lane 2005-09-07 17:52:35 Re: uuid type for postgres