Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 29BFA2E0049 for ; Thu, 10 Apr 2008 11:52:18 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 28330-03 for ; Thu, 10 Apr 2008 11:52:05 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.246]) by developer.postgresql.org (Postfix) with ESMTP id A02132E0077 for ; Thu, 10 Apr 2008 11:52:05 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c36so5736ana.33 for ; Thu, 10 Apr 2008 07:52:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:sender; bh=VAwMag5xCgM5/6sgyCzOWLKDzQcup71zgE7pC7Ab8+w=; b=O2DGSXAIHGXKhSAHfobujjo1JAOG9w5C0+kpEZPSthJJxjPp/CO1J30GV5z5QpG4oa0hBoxmwkzvwfHsCZRG8111xiQPeIZQNOuw5EopbgDDoK4IQcn+8aEAWDvpH7M7p84Hhhh2wr24eLoc35oh83wCqxjK9miVzIMhdyF7YXE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:sender; b=tZSPm0JERN5Qcdm4eQh480+noq6or2B3LttkOBRnh2dU+gJmX72gpP8X455Z23rxDF14KlTp+ZRWxWpI8xHrIGD3zsKkPuMoQLwIGv7Cg2wd7TGODFis60RkCjHh1OcBvYmZ3/ytT/EzEIzJMibZFR1gg5HHx8jAdc904Rzyv5c= Received: by 10.100.171.10 with SMTP id t10mr2945425ane.72.1207839123970; Thu, 10 Apr 2008 07:52:03 -0700 (PDT) Received: from cheetah.dev.us.cyberscience.com ( [66.250.49.67]) by mx.google.com with ESMTPS id p60sm1347327hsa.18.2008.04.10.07.52.02 (version=SSLv3 cipher=RC4-MD5); Thu, 10 Apr 2008 07:52:03 -0700 (PDT) Message-ID: <47FE29AF.6010906@starsilk.net> Date: Thu, 10 Apr 2008 08:52:31 -0600 From: Adam Gundy User-Agent: Thunderbird 2.0.0.12 (X11/20080227) MIME-Version: 1.0 To: Richard Huxton CC: pgsql-performance@postgresql.org Subject: Re: varchar index joins not working? References: <6f55f1270804092013n73ee1f81t2c6d72cee49655a3@mail.gmail.com> <47FDD3D1.9070208@archonet.com> In-Reply-To: <47FDD3D1.9070208@archonet.com> Content-Type: multipart/signed; protocol="application/x-pkcs7-signature"; micalg=sha1; boundary="------------ms020601020804060102070107" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200804/129 X-Sequence-Number: 29622 This is a cryptographically signed message in MIME format. --------------ms020601020804060102070107 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Richard Huxton wrote: > Adam Gundy wrote: >> I'm hitting an unexpected problem with postgres 8.3 - I have some >> tables which use varchar(32) for their unique IDs which I'm attempting >> to join using some simple SQL: >> >> select * >> from group_access, groups >> where group_access.groupid = groups.groupid and >> group_access.uid = '7275359408f44591d0717e16890ce335'; >> >> there's a unique index on group_access.groupid, and a non-unique index >> on groups.groupid. both are non-null. > > What about group_access.uid - I'd have thought that + groups pkey is > probably the sensible combination here. that is an index on group_access: "group_access_pkey" PRIMARY KEY, btree (groupid, uid) adding the (uid, groupid) index helps the small database, it will do an index join if forced to, but the full database still refuses to do an index join - it does a full index scan followed by a merge. >> QUERY PLAN >> -------------------------------------------------------------------------------------------------------------------------- >> >> Hash Join (cost=8.89..41329.88 rows=119940 width=287) (actual >> time=0.202..935.136 rows=981 loops=1) > > That's because it's expecting 119,940 rows to match (rather than the > actual 981 you do get). If you were getting that many results this is > probably a sensible plan. sure. but it's estimate is *wildly* off >> Hash Cond: ((groups.groupid)::text = (group_access.groupid)::text) >> -> Seq Scan on groups (cost=0.00..31696.48 rows=1123348 >> width=177) (actual time=0.011..446.091 rows=1125239 loops=1) > > It's got a good idea of the total number of rows in groups. yeah. >> -> Hash (cost=8.51..8.51 rows=30 width=110) (actual >> time=0.148..0.148 rows=30 loops=1) >> -> Seq Scan on group_access (cost=0.00..8.51 rows=30 >> width=110) (actual time=0.014..0.126 rows=30 loops=1) > > And also group_access. Oh, the seq-scan doesn't really matter here. It > probably *is* faster to read all 30 rows in one burst rather than go to > the index and then back to the table. agreed. >> it's running an index scan across the entire table (no condition >> applied) :-( >> >> so, just for the hell of it, I tried making groupid a char(32), >> despite repeated assertions in this group that there's no performance >> difference between the two: > > There's no performance difference between the two. hah. if it makes the join with char (and runs fast), or reads the whole table with varchar, then there *is* a performance difference - a big one! > The char(32) thing isn't important here, what is important is that it's > expecting ~300 rows rather than 120,000. It's still wrong, but it's > close enough to make sense. > So - the question is - why is PG expecting so many matches to your join. more to the point, why does it get the estimate right (or close) with char, but massively wrong with varchar? I've been vacuum analyzing after each change.. with the smaller database, and char type, it (for certain joins) still wants to do a seqscan because the tables are small enough, but if I disable seqscan, it does an index join (usually with a small time penalty). if I switch the types back to varchar, re-analyze, re-run, it *will not* do an index join! > How many distinct values do you have in groups.groupid and > group_access.group_id? for the small database (since it shows the same problem): group_access: 280/268 groups: 2006/139 for the large database: group_access: same groups: 1712647/140 the groupid key is an MD5 hash, so it should be uniformly distributed. maybe that throws the stats? but, again, char works, varchar doesn't :-( --------------ms020601020804060102070107 Content-Type: application/x-pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIJAzCC AtwwggJFoAMCAQICEDxC40TcaLz5pGwdgYq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