pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedHelp with multistage query
4+ messages / 4 participants
[nested] [flat]
* Help with multistage query
@ 2005-09-07 17:57 Rick Schumeyer <rschumeyer@ieee.org>
2005-09-07 20:05 ` Re: Help with multistage query Russell Simpkins <russellsimpkins@hotmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Rick Schumeyer @ 2005-09-07 17:57 UTC (permalink / raw)
To: 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.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Help with multistage query
2005-09-07 17:57 Help with multistage query Rick Schumeyer <rschumeyer@ieee.org>
@ 2005-09-07 20:05 ` Russell Simpkins <russellsimpkins@hotmail.com>
2005-09-07 21:37 ` Re: Help with multistage query Matt Emmerton <matt@gsicomp.on.ca>
0 siblings, 1 reply; 4+ messages in thread
From: Russell Simpkins @ 2005-09-07 20:05 UTC (permalink / raw)
To: 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
}
how about
select t1.id from t1, t2 where t1.id = t2.id and t2.id = x
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Help with multistage query
2005-09-07 17:57 Help with multistage query Rick Schumeyer <rschumeyer@ieee.org>
2005-09-07 20:05 ` Re: Help with multistage query Russell Simpkins <russellsimpkins@hotmail.com>
@ 2005-09-07 21:37 ` Matt Emmerton <matt@gsicomp.on.ca>
2005-10-04 22:34 ` Re: Help with multistage query Jim C. Nasby <jnasby@pervasive.com>
0 siblings, 1 reply; 4+ messages in thread
From: Matt Emmerton @ 2005-09-07 21:37 UTC (permalink / raw)
To: Russell Simpkins <russellsimpkins@hotmail.com>; pgsql-sql
----- Original Message -----
From: Russell Simpkins
To: pgsql-sql@postgresql.org
Sent: Wednesday, September 07, 2005 4:05 PM
Subject: Re: [SQL] Help with multistage query
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
}
how about
select t1.id from t1, t2 where t1.id = t2.id and t2.id = x
or more correctly, based on the OP's example:
select t2.x from t1, t2 where t1.id = t2.id and t1.id = <condition>
--
Matt
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Help with multistage query
2005-09-07 17:57 Help with multistage query Rick Schumeyer <rschumeyer@ieee.org>
2005-09-07 20:05 ` Re: Help with multistage query Russell Simpkins <russellsimpkins@hotmail.com>
2005-09-07 21:37 ` Re: Help with multistage query Matt Emmerton <matt@gsicomp.on.ca>
@ 2005-10-04 22:34 ` Jim C. Nasby <jnasby@pervasive.com>
0 siblings, 0 replies; 4+ messages in thread
From: Jim C. Nasby @ 2005-10-04 22:34 UTC (permalink / raw)
To: Matt Emmerton <matt@gsicomp.on.ca>; +Cc: Russell Simpkins <russellsimpkins@hotmail.com>; pgsql-sql
On Wed, Sep 07, 2005 at 05:37:47PM -0400, Matt Emmerton wrote:
>
> ----- Original Message -----
> From: Russell Simpkins
> To: pgsql-sql@postgresql.org
> Sent: Wednesday, September 07, 2005 4:05 PM
> Subject: Re: [SQL] Help with multistage query
>
> 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
>
> }
>
> how about
> select t1.id from t1, t2 where t1.id = t2.id and t2.id = x
>
> or more correctly, based on the OP's example:
>
> select t2.x from t1, t2 where t1.id = t2.id and t1.id = <condition>
Actually, I think you want AND t2.x <condition>, not t1.id.
BTW, I recommend not using id as a bareword field name. Very easy to get
confused when you start joining a bunch of stuff together.
--
Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2005-10-04 22:34 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-09-07 17:57 Help with multistage query Rick Schumeyer <rschumeyer@ieee.org>
2005-09-07 20:05 ` Russell Simpkins <russellsimpkins@hotmail.com>
2005-09-07 21:37 ` Matt Emmerton <matt@gsicomp.on.ca>
2005-10-04 22:34 ` Jim C. Nasby <jnasby@pervasive.com>
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