Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 515D19FA339 for ; Wed, 6 Dec 2006 15:11:02 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 76131-03 for ; Wed, 6 Dec 2006 15:10:55 -0400 (AST) X-Greylist: delayed 01:46:42.383681 by SQLgrey-1.7.4 Received: from pat.uio.no (pat.uio.no [129.240.10.15]) by postgresql.org (Postfix) with ESMTP id E37329F9F0E for ; Wed, 6 Dec 2006 15:10:55 -0400 (AST) Received: from mail-mx4.uio.no ([129.240.10.45]) by pat.uio.no with esmtp (Exim 4.43) id 1Gs2AQ-00050T-6W for pgsql-performance@postgresql.org; Wed, 06 Dec 2006 20:10:54 +0100 Received: from ti200710a080-2739.bb.online.no ([85.164.138.179] helo=[10.1.1.101]) by mail-mx4.uio.no with esmtpsa (SSLv3:RC4-MD5:128) (Exim 4.43) id 1Gs2AL-0004j9-IA for pgsql-performance@postgresql.org; Wed, 06 Dec 2006 20:10:49 +0100 Subject: Problems with an update-from statement and pg-8.1.4 From: Rafael Martinez To: pgsql-performance@postgresql.org Content-Type: text/plain Organization: University of Oslo Date: Wed, 06 Dec 2006 20:10:43 +0100 Message-Id: <1165432244.9606.18.camel@bbking.linux> Mime-Version: 1.0 X-Mailer: Evolution 2.6.0 Content-Transfer-Encoding: 7bit X-UiO-Spam-info: not spam, SpamAssassin (score=-3.993, required 12, autolearn=disabled, AWL 0.87, RCVD_IN_SORBS_DUL 0.14, UIO_MAIL_IS_INTERNAL -5.00) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50 X-Spam-Level: X-Archive-Number: 200612/141 X-Sequence-Number: 22197 Hello We are having some problems with an UPDATE ... FROM sql-statement and pg-8.1.4. It takes ages to finish. The problem is the Seq Scan of the table 'mail', this table is over 6GB without indexes, and when we send thousands of this type of statement, the server has a very high iowait percent. How can we get rid of this Seq Scan? I send the output of an explain and table definitions: ------------------------------------------------------------------------- mailstats=# EXPLAIN update mail SET spamscore = '-5.026' FROM mail m, mail_received mr where mr.mail_id = m.mail_id AND mr.queue_id = '1GrxLs-0004N9-I1' and mr.mailhost = '129.240.10.47'; QUERY PLAN ------------------------------------------------------------------------------------------------------------ Nested Loop (cost=0.00..932360.78 rows=7184312 width=57) -> Nested Loop (cost=0.00..6.54 rows=1 width=0) -> Index Scan using received_queue_id_index on mail_received mr (cost=0.00..3.20 rows=1 width=4) Index Cond: ((queue_id)::text = '1GrxLs-0004N9-I1'::text) Filter: (mailhost = '129.240.10.47'::inet) -> Index Scan using mail_pkey on mail m (cost=0.00..3.32 rows=1 width=4) Index Cond: ("outer".mail_id = m.mail_id) -> Seq Scan on mail (cost=0.00..860511.12 rows=7184312 width=57) (8 rows) mailstats=# \d mail Table "public.mail" Column | Type | Modifiers ------------+--------------+-------------------------------------------------------- mail_id | integer | not null default nextval('mail_mail_id_seq'::regclass) size | integer | message_id | text | not null spamscore | numeric(6,3) | Indexes: "mail_pkey" PRIMARY KEY, btree (mail_id) "mail_message_id_key" UNIQUE, btree (message_id) mailstats=# \d mail_received Table "public.mail_received" Column | Type | Modifiers ---------------+-----------------------------+---------------------------------------------------------------------- reception_id | integer | not null default nextval('mail_received_reception_id_seq'::regclass) mail_id | integer | not null envelope_from | text | helohost | text | from_host | inet | protocol | text | mailhost | inet | received | timestamp without time zone | not null completed | timestamp without time zone | queue_id | character varying(16) | not null Indexes: "mail_received_pkey" PRIMARY KEY, btree (reception_id) "mail_received_queue_id_key" UNIQUE, btree (queue_id, mailhost) "mail_received_completed_idx" btree (completed) "mail_received_mailhost_index" btree (mailhost) "mail_received_received_index" btree (received) "received_id_index" btree (mail_id) "received_queue_id_index" btree (queue_id) Foreign-key constraints: "$1" FOREIGN KEY (mail_id) REFERENCES mail(mail_id) ------------------------------------------------------------------------- Thanks in advance. regards, -- Rafael Martinez, Center for Information Technology Services University of Oslo, Norway PGP Public Key: http://folk.uio.no/rafael/