Re: Performance Problem with sub-select using array

From: "Aaron Bono" <postgresql(at)aranya(dot)com>
To: "Travis Whitton" <tinymountain(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Performance Problem with sub-select using array
Date: 2006-08-28 04:10:06
Message-ID: bf05e51c0608272110t6e994e09n7922e494fe62cd2f@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 8/24/06, Travis Whitton <tinymountain(at)gmail(dot)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
==================================================================

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Aaron Bono 2006-08-28 04:14:37 Re: Importing data from csv
Previous Message Richard Broersma Jr 2006-08-27 14:39:49 Re: Create Assertion -- Question from a newbie