Help with multistage query - Mailing list pgsql-sql

From Rick Schumeyer
Subject Help with multistage query
Date
Msg-id 000001c5b3d5$a67a3540$0300a8c0@dell8200
Whole thread Raw
List pgsql-sql
<div class="Section1"><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">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.</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">The simplified queries look like this:</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">SELECT id FROM t1 WHERE condition1;   ;returns about 2k records which are stored in
@idarray</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">foreach $id (@idarray) {</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial">   SELECT x FROM t2 WHERE id=$id;   ; each select returns about 100 records which are saved in a
perlvariable</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial">}</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">At this point I have a list of about 200k records, from which I can manually filter based on x
values.</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">There are indices on id in both t1 and t2, so the first two queries are both index scans.  I cannot
afforda table scan on t2 due to the size of the table.</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Like I said, this works (and uses only index scans), but I would think it would be better to somehow
selectthe 200k records into a temp table.  Because the temp table would be relatively small, a seq scan is ok to
producemy final list.</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Also, I am now issuing the second query about 2k times…this seems inefficient.</span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">I would think there would  a way to restate the first two queries as either a join or a subselect. 
Myinitial attempts result in a table scan (according to EXPLAIN) on t2.</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">For example I tried </span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial">   SELECT x FROM t2 WHERE id in ( SELECT id FROM t1 WHERE condition1);</span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial">but this gives a seq scan.</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Any ideas are appreciated.</span></font></div>

pgsql-sql by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: AGE function
Next
From: Josh Berkus
Date:
Subject: Re: uuid type (moved from HACKERS)