Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id DEB169FA45A for ; Thu, 23 Aug 2007 14:24:31 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 76109-02-2 for ; Thu, 23 Aug 2007 14:24:25 -0300 (ADT) X-Greylist: delayed 00:06:38.931837 by SQLgrey-1.7.5 Received: from outbound-mail-33.bluehost.com (outbound-mail-33.bluehost.com [69.89.18.153]) by postgresql.org (Postfix) with SMTP id 64D8E9FA40A for ; Thu, 23 Aug 2007 14:24:25 -0300 (ADT) Received: (qmail 32165 invoked by uid 0); 23 Aug 2007 17:17:44 -0000 Received: from unknown (HELO box54.bluehost.com) (69.89.20.54) by mailproxy2.bluehost.com with SMTP; 23 Aug 2007 17:17:44 -0000 Received: from rrcs-71-41-16-51.sw.biz.rr.com ([71.41.16.51] helo=[10.0.12.106]) by box54.bluehost.com with esmtp (Exim 4.66) (envelope-from ) id 1IOGJS-0005oA-U6 for pgsql-performance@postgresql.org; Thu, 23 Aug 2007 11:17:43 -0600 From: Kevin Kempter To: pgsql-performance@postgresql.org Subject: long-running query - needs tuning Date: Thu, 23 Aug 2007 11:16:57 -0600 User-Agent: KMail/1.9.7 MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Message-Id: <200708231116.58109.kevin@kevinkempterllc.com> X-Identified-User: {840:box54.bluehost.com:kevinkem:kevinkempterllc.com} {sentby:bopbeforesmtp 71.41.16.51 authed with ossfusion.com} X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.131 tagged_above=0 required=5 tests=BAYES_50=0.001, DNS_FROM_OPENWHOIS=1.13 X-Spam-Level: * X-Archive-Number: 200708/264 X-Sequence-Number: 26394 Hi List; I've just started working with a new client and they have amoung other issues with their databases a particular update that basically locks out users. The below query was running for over 6 hours this morning and the CPU load had climbed to a point where new connections simply hung waiting to connect. We had to kill the query to allow business users to connect to the applications that connect to the database, thus I could not post an explain analyze. In any case the query looks like this: update dat_customer_mailbox_counts set total_contacts = contacts.ct, total_contact_users = contacts.dct from ( select customer_id, count(*) as ct, count( distinct con.user_id ) as dct from dat_user_contacts con group by customer_id ) contacts where contacts.customer_id = dat_customer_mailbox_counts.customer_id Here's the latest counts from the system catalogs: dat_customer_mailbox_counts: 423 dat_user_contacts 59,469,476 And here's an explain plan: QUERY PLAN ---------------------------------------------------------------------------------------------------- Merge Join (cost=17118858.51..17727442.30 rows=155 width=90) Merge Cond: ("outer".customer_id = "inner".customer_id) -> GroupAggregate (cost=17118772.93..17727347.34 rows=155 width=8) -> Sort (cost=17118772.93..17270915.95 rows=60857208 width=8) Sort Key: con.customer_id -> Seq Scan on dat_user_contacts con (cost=0.00..7332483.08 rows=60857208 width=8) -> Sort (cost=85.57..88.14 rows=1026 width=74) Sort Key: dat_customer_mailbox_counts.customer_id -> Seq Scan on dat_customer_mailbox_counts (cost=0.00..34.26 rows=1026 width=74) (9 rows) Any thoughts, comments, Ideas for debugging, etc would be way helpful... Thanks in advance. /Kevin