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 1903152814 for ; Thu, 14 Jul 2005 20:40:27 -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 28735-10 for ; Thu, 14 Jul 2005 23:40:16 +0000 (GMT) Received: from sccmmhc91.asp.att.net (sccmmhc91.asp.att.net [204.127.203.211]) by svr1.postgresql.org (Postfix) with ESMTP id 860C652C6B for ; Thu, 14 Jul 2005 20:40:12 -0300 (ADT) Received: from juju.arbash-meinel.com ([12.214.18.81]) by sccmmhc91.asp.att.net (sccmmhc91) with ESMTP id <20050714234011m9100k85k1e>; Thu, 14 Jul 2005 23:40:16 +0000 Received: by juju.arbash-meinel.com (Postfix, from userid 505) id B51E355FFC; Thu, 14 Jul 2005 18:40:07 -0500 (CDT) Received: from [192.168.1.102] (65-100-20-12.cdrr.qwest.net [65.100.20.12]) by juju.arbash-meinel.com (Postfix) with ESMTP id A941D55FF0; Thu, 14 Jul 2005 18:40:02 -0500 (CDT) Message-ID: <42D6F7CE.5010007@arbash-meinel.com> Date: Thu, 14 Jul 2005 18:39:58 -0500 From: John A Meinel User-Agent: Mozilla Thunderbird 1.0.2 (Macintosh/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Tom Lane Cc: Dan Harris , pgsql-performance@postgresql.org Subject: Re: slow joining very large table to smaller ones References: <46AF8F35-B735-4DFA-BA0A-AC82B5DA3CCE@drivefaster.net> <42D687E5.7080202@arbash-meinel.com> <42D6F177.3000002@arbash-meinel.com> <5708.1121383802@sss.pgh.pa.us> In-Reply-To: <5708.1121383802@sss.pgh.pa.us> X-Enigmail-Version: 0.92.0.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig20905F1FC17C14B2B7F6EFD9" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.03 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200507/194 X-Sequence-Number: 13435 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig20905F1FC17C14B2B7F6EFD9 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Tom Lane wrote: > John A Meinel writes: > >>What I don't understand is that the planner is actually estimating that >>joining against the new table is going to *increase* the number of >>returned rows. > > > It evidently thinks that incidentid in the k_r table is pretty > nonunique. We really need to look at the statistics data to > see what's going on. > > regards, tom lane > Okay, sure. What about doing this, then: EXPLAIN ANALYZE SELECT recordtext FROM eventactivity JOIN (SELECT DISTINCT incidentid FROM k_r JOIN k_b USING (incidentid) WHERE k_r.id = ?? AND k_b.id = ??) USING (incidentid) ; Since I assume that eventactivity is the only table with "recordtext", and that you don't get any columns from k_r and k_b, meaning it would be pointless to get duplicate incidentids. I may be misunderstanding what the query is trying to do, but depending on what is in k_r and k_b, is it possible to use a UNIQUE INDEX rather than just an index on incidentid? There is also the possibility of EXPLAIN ANALYZE SELECT recordtext FROM eventactivtity JOIN (SELECT incidentid FROM k_r WHERE k_r.id = ?? UNION SELECT incidentid FROM k_b WHERE k_b.id = ??) USING (incidentid) ; But both of these would mean that you don't actually want columns from k_r or k_b, just a unique list of incident ids. But first, I agree, we should make sure the pg_stats values are reasonable. John =:-> --------------enig20905F1FC17C14B2B7F6EFD9 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFC1vfPJdeBCYSNAAMRAgTWAJoDylT5gpCS6knbpQ0ztcboKlp/GACgsWSb zm/lpqNKxkD6fOywBwCNIls= =n7aS -----END PGP SIGNATURE----- --------------enig20905F1FC17C14B2B7F6EFD9--