Received: from grynet.passagen.se (grynet.passagen.se [195.163.107.36]) by hub.org (8.9.3/8.9.3) with ESMTP id NAA79313 for ; Mon, 1 Nov 1999 13:46:26 -0500 (EST) (envelope-from osk@hem.passagen.se) Received: from oskar (d212-151-111-242.swipnet.se [212.151.111.242]) by grynet.passagen.se (8.8.6/8.8.6) with ESMTP id TAA13649 for ; Mon, 1 Nov 1999 19:46:24 +0100 (MET) Received: from usel by oskar with local (Exim 3.03 #1 (Debian)) id 11iMTz-0001qA-00 for ; Mon, 01 Nov 1999 19:47:07 +0100 Date: Mon, 1 Nov 1999 19:47:07 +0100 From: Oskar Liljeblad To: pgsql-sql@postgresql.org Subject: query with subquery abnormally slow? Message-ID: <19991101194707.A7034@oskar> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii User-Agent: Mutt/1.0pre4i I'm doing a SELECT query with a subquery on a table with 12K rows but it is very slow (10 seconds+). The query looks like this: select * from items where package in (select package from items where ...blah... group by package) Where package is an indexed varchar field. The subquery generates 50 rows, and completes quickly when I run it alone. Now if I manually replace the subquery with these 50 strings - like select * from items where package in ('r1', 'r2', 'r3', .., 'r50') this new query completes in less than a second. The result of the complete query is 500 rows. Is this natural? Is there anything I can do to speed things up? (Besides doing N+1 queries where N is the number of rows the subquery would produce.) Oskar Liljeblad (osk@hem.passagen.se)