X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 8844752D52 for ; Thu, 14 Jul 2005 05:05:30 -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 11817-09 for ; Thu, 14 Jul 2005 08:05:25 +0000 (GMT) Received: from crestone.coronasolutions.com (crestone.coronasolutions.com [66.45.104.24]) by svr1.postgresql.org (Postfix) with ESMTP id 60C7552D56 for ; Thu, 14 Jul 2005 05:05:24 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by crestone.coronasolutions.com (Postfix) with ESMTP id 39EE5644195 for ; Thu, 14 Jul 2005 02:05:03 -0600 (MDT) Received: from crestone.coronasolutions.com ([127.0.0.1]) by localhost (crestone.coronasolutions.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 02754-01 for ; Thu, 14 Jul 2005 02:05:01 -0600 (MDT) Received: from [192.168.1.100] (c-67-162-134-190.hsd1.co.comcast.net [67.162.134.190]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by crestone.coronasolutions.com (Postfix) with ESMTP id 00A57644191 for ; Thu, 14 Jul 2005 02:05:00 -0600 (MDT) Mime-Version: 1.0 (Apple Message framework v730) Content-Transfer-Encoding: 7bit Message-Id: <46AF8F35-B735-4DFA-BA0A-AC82B5DA3CCE@drivefaster.net> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: pgsql-performance@postgresql.org From: Dan Harris Subject: slow joining very large table to smaller ones Date: Thu, 14 Jul 2005 02:05:23 -0600 X-Mailer: Apple Mail (2.730) X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at drivefaster.net X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200507/171 X-Sequence-Number: 13412 I'm trying to improve the speed of this query: explain select recordtext from eventactivity inner join ( select incidentid from k_r where id = 94 ) a using ( incidentid ) inner join ( select incidentid from k_b where id = 107 ) b using ( incidentid ); QUERY PLAN ------------------------------------------------------------------------ -------------------------------------- Merge Join (cost=2747.29..4249364.96 rows=11968693 width=35) Merge Cond: (("outer".incidentid)::text = "inner"."?column2?") -> Merge Join (cost=1349.56..4230052.73 rows=4413563 width=117) Merge Cond: (("outer".incidentid)::text = "inner"."?column2?") -> Index Scan using eventactivity1 on eventactivity (cost=0.00..4051200.28 rows=44519781 width=49) -> Sort (cost=1349.56..1350.85 rows=517 width=68) Sort Key: (k_b.incidentid)::text -> Index Scan using k_b_idx on k_b (cost=0.00..1326.26 rows=517 width=68) Index Cond: (id = 107) -> Sort (cost=1397.73..1399.09 rows=542 width=68) Sort Key: (k_r.incidentid)::text -> Index Scan using k_r_idx on k_r (cost=0.00..1373.12 rows=542 width=68) Index Cond: (id = 94) (13 rows) There are many millions of rows in eventactivity. There are a few ten-thousand rows in k_r and k_b. There is an index on 'incidentid' in all three tables. There should only be less than 100 rows matched in k_r and k_b total. That part on its own is very very fast. But, it should have those 100 or so incidentids extracted in under a second and then go into eventactivity AFTER doing that. At least, that's my intention to make this fast. Right now, it looks like pg is trying to sort the entire eventactivity table for the merge join which is taking several minutes to do. Can I rephrase this so that it does the searching through k_r and k_b FIRST and then go into eventactivity using the index on incidentid? It seems like that shouldn't be too hard to make fast but my SQL query skills are only average. Thanks -Dan