Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 21F42476718 for ; Wed, 22 Jan 2003 05:49:15 -0500 (EST) Received: from serwer.skawsoft.com.pl (serwer.skawsoft.com.pl [213.25.37.66]) by postgresql.org (Postfix) with ESMTP id 20280476519 for ; Wed, 22 Jan 2003 05:47:49 -0500 (EST) Received: from klaster.net (ph32.krakow.cvx.ppp.tpnet.pl [217.99.208.32]) by serwer.skawsoft.com.pl (Postfix) with ESMTP id 450A22B85D; Wed, 22 Jan 2003 10:53:39 +0100 (CET) Message-ID: <3E2E6C2F.6020902@klaster.net> Date: Wed, 22 Jan 2003 11:02:23 +0100 From: Tomasz Myrta User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.2b) Gecko/20021016 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Michael Hostbaek Cc: pgsql-sql@postgresql.org Subject: Re: optimal sql References: <20030122085454.GA55124@mich2.itxmarket.com> In-Reply-To: <20030122085454.GA55124@mich2.itxmarket.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/331 X-Sequence-Number: 11196 Michael Hostbaek wrote: >Hi, > >I am running postgresql 7.2.3 on a test server (with potential of >becoming my production server). > >On the server I have a perl script, that is grabbing some data from a >inventory database (local) - with some subselects. >The query is like this: > > >my $sth = $ppdb->prepare(" > select partno, create_date, mfg, condition, gescode, qty, >cmup,(SELECT partno_main FROM partno_lookup where > partno_lookup.partno_alias ilike (?|| inventory.partno ||?) and mfg >ilike ? limit 1) > as partno_main, (SELECT subcat FROM partno_lookup where >partno_lookup.partno_alias ilike > (?|| inventory.partno ||?) and mfg ilike ? limit 1) as subcat, > (SELECT key_search FROM partno_lookup where >partno_lookup.partno_alias ilike (?|| inventory.partno ||?) and > mfg ilike ? limit 1) as key_search, > (SELECT text_desc FROM descriptions where >descriptions.partno=(SELECT partno_main FROM partno_lookup > where partno_lookup.partno_alias ilike (?|| inventory.partno ||?) >and mfg ilike ? limit 1) > limit 1) as descri from inventory where mfg ilike ? and ? < >create_date $refurbed order by key_search, > subcat, partno_main, status DESC "); 1. Probably your query can't use index on table partno_lookup.partno_alias. Consider creating table aliases which contains all possible parts aliases. You can change then "ilike" into "=" which will use indexes. 2. You don't need subselects in your query. You can change them into ordinary table joins and use "group by" or "distinct on". In your case selecting from partno_lookup is executed several times per one row. 3. Explain analyze would be helpful like in most performance cases... The same with SQL query instead of Perl script. 4. This is rather a sql problem, than hardware/configuration one. Regards, Tomasz Myrta