X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 82AC6D7F5A for ; Wed, 12 Oct 2005 17:31:16 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 19910-06 for ; Wed, 12 Oct 2005 20:31:13 +0000 (GMT) Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.116.145.2]) by svr1.postgresql.org (Postfix) with ESMTP id 03B54D7F59 for ; Wed, 12 Oct 2005 17:31:14 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 7D4A331059; Wed, 12 Oct 2005 22:31:15 +0200 (MET DST) From: Martin Nickel X-Newsgroups: pgsql.performance Subject: Sequential scan on FK join Date: Wed, 12 Oct 2005 15:40:24 -0500 Organization: Hub.Org Networking Services Lines: 31 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@news.hub.org User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) To: pgsql-performance@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 required=5 tests=[none] X-Spam-Level: X-Archive-Number: 200510/237 X-Sequence-Number: 15005 All, I can see why the query below is slow. The lead table is 34 million rows, and a sequential scan always takes 3+ minutes. Mailing_id is the PK for mailing and is constrained as a foreign key (NULLS allowed) in lead. There is an index on lead.mailing_id. I've just run VACUUM ANALYZE on lead. I don't understand why it isn't being used. Thanks for your help, Martin Nickel SELECT m.mailcode, l.lead_id FROM mailing m INNER JOIN lead l ON m.mailing_id = l.mailing_id WHERE (m.maildate >= '2005-7-01'::date AND m.maildate < '2005-8-01'::date) -- takes 510,145 ms EXPLAIN SELECT m.mailcode, l.lead_id FROM mailing m INNER JOIN lead l ON m.mailing_id = l.mailing_id WHERE (m.maildate >= '2005-7-01'::date AND m.maildate < '2005-8-01'::date) Hash Join (cost=62.13..2001702.55 rows=2711552 width=20) Hash Cond: ("outer".mailing_id = "inner".mailing_id) -> Seq Scan on lead l (cost=0.00..1804198.60 rows=34065260 width=8) -> Hash (cost=61.22..61.22 rows=362 width=20) -> Index Scan using mailing_maildate_idx on mailing m (cost=0.00..61.22 rows=362 width=20) Index Cond: ((maildate >= '2005-07-01'::date) AND (maildate < '2005-08-01'::date))