X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id B6A149FC901 for ; Mon, 28 Aug 2006 01:10:12 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 12983-02 for ; Mon, 28 Aug 2006 01:10:08 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.177]) by postgresql.org (Postfix) with ESMTP id 310DF9FC8FF for ; Mon, 28 Aug 2006 01:10:07 -0300 (ADT) Received: by py-out-1112.google.com with SMTP id d80so2267890pyd for ; Sun, 27 Aug 2006 21:10:07 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:references:x-google-sender-auth; b=qDazoY1cIBYzKlsq0y2hwGqFp8MbVPRXDnHowwbHyGPm4dyBbSlAA0aOqvyVJj2KURVUWJqzTeQ6Ss/5tPGgxyIItylujthhViS8EeMPrDw0l7/09OesxiysxAjbsBHER+/L/CBpGX1kGzwNNHcsdS+BsAwdgLIus/eS8g+Y11g= Received: by 10.35.38.17 with SMTP id q17mr11479213pyj; Sun, 27 Aug 2006 21:10:06 -0700 (PDT) Received: by 10.35.80.9 with HTTP; Sun, 27 Aug 2006 21:10:06 -0700 (PDT) Message-ID: Date: Sun, 27 Aug 2006 23:10:06 -0500 From: "Aaron Bono" To: "Travis Whitton" Subject: Re: Performance Problem with sub-select using array Cc: pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_212698_5678244.1156738206071" References: X-Google-Sender-Auth: 8430b5c22b167806 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.694 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, HTML_30_40, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200608/209 X-Sequence-Number: 26167 ------=_Part_212698_5678244.1156738206071 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 8/24/06, Travis Whitton wrote: > > Hello all, I'm running the following query on about 6,000 records worth of > data, and it takes about 8 seconds to complete. Can anyone provide any > suggestions to improve performance? I have an index on two columns in the > transacts table (program_id, customer_id). If I specify a number for > customer.id in the sub-select, query time is reduced to about 2 seconds, > which still seems like a million years for only 6,000 records, but I'm > guessing that the sub-select can't resolve the id since it's done before the > outer query, so it scans the entre recordset for every row? Transacts is a > many to many table for customers and programs. I know this query doesn't > even reference any columns from programs; however, I dynamically insert > where clauses to constrain the result set. > > SELECT distinct customers.id, first_name, last_name, address1, > contact_city, contact_state, primary_phone, email, array(select > programs.program_name from transacts, programs where customer_id = > customers.id and programs.id = transacts.program_id and submit_status = > 'success') AS partners from customers, transacts, programs where > transacts.customer_id = customers.id and transacts.program_id = > programs.id > My guess is that your problem is that you may be getting 6000 rows, but the array(select ....) is having to run once for each of record returned (so it is running 6000 times). Try an explain analyze: http://www.postgresql.org/docs/7.4/interactive/sql-explain.html - that will reveal more of where the performance problem is. ================================================================== Aaron Bono Aranya Software Technologies, Inc. http://www.aranya.com http://codeelixir.com ================================================================== ------=_Part_212698_5678244.1156738206071 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On 8/24/06, Travis Whitton <tinymountain@gmail.com> wrote:
Hello all, I'm running the following query on about 6,000 records worth of data, and it takes about 8 seconds to complete. Can anyone provide any suggestions to improve performance? I have an index on two columns in the transacts table (program_id, customer_id). If I specify a number for customer.id in the sub-select, query time is reduced to about 2 seconds, which still seems like a million years for only 6,000 records, but I'm guessing that the sub-select can't resolve the id since it's done before the outer query, so it scans the entre recordset for every row? Transacts is a many to many table for customers and programs. I know this query doesn't even reference any columns from programs; however, I dynamically insert where clauses to constrain the result set.

SELECT distinct customers.id, first_name, last_name, address1, contact_city, contact_state, primary_phone, email, array(select programs.program_name from transacts, programs where customer_id = customers.id and programs.id = transacts.program_id and submit_status = 'success') AS partners from customers, transacts, programs where transacts.customer_id = customers.id and transacts.program_id = programs.id

 
My guess is that your problem is that you may be getting 6000 rows, but the array(select ....) is having to run once for each of record returned (so it is running 6000 times).

Try an explain analyze: http://www.postgresql.org/docs/7.4/interactive/sql-explain.html - that will reveal more of where the performance problem is.

==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
================================================================== ------=_Part_212698_5678244.1156738206071--