Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id A1DDD2E00B0 for ; Mon, 14 Apr 2008 14:02:19 -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 27645-05-4 for ; Mon, 14 Apr 2008 14:02:11 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.183]) by developer.postgresql.org (Postfix) with ESMTP id E34512E0046 for ; Mon, 14 Apr 2008 14:01:59 -0300 (ADT) Received: by wa-out-1112.google.com with SMTP id n7so2685511wag.5 for ; Mon, 14 Apr 2008 10:01:58 -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=eSxD6vxB/Fj2WUK+2LAtFQ/BNzlifbmw5GXUscl5wXo=; b=ADVm++Bb8aLxlBPpip5MEV+k2cF5DipCp2UPNty7v4h5sBWP22WkP0IUeRYxhuGsjvnEI9rFjSY1Txva47Zf75ni+oKkAvXpw5jgXajhCWupNzmnzRwdAAmCQ/XXAqJzRJxkIm3NiXbciLVmkVCMu6I7FwxM/MKcO8jBfNIsjEk= 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=Ows0ifPqdkIrH6aYhZlD6md/X0GVgseGkt7JiOxJYTp5Dzb41UdKhPELMw1zqIK1+7bI41H3WZixvHqutgiFLdEjmdaR5LxHxQr9Vy5FI+NhPUNJYBemXZXd8xMXqlALOCsyLYUg0ugjKhTzUPKtfJ0D5n14j33mT+QwYft304g= Received: by 10.114.255.1 with SMTP id c1mr7230504wai.93.1208192518501; Mon, 14 Apr 2008 10:01:58 -0700 (PDT) Received: from cheetah.dev.us.cyberscience.com ( [66.250.49.67]) by mx.google.com with ESMTPS id g9sm15558828wra.10.2008.04.14.10.01.50 (version=SSLv3 cipher=RC4-MD5); Mon, 14 Apr 2008 10:01:55 -0700 (PDT) Message-ID: <48038E21.50606@starsilk.net> Date: Mon, 14 Apr 2008 11:02:25 -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> <47FE29AF.6010906@starsilk.net> <6f55f1270804101154t3d4a01eco2b7bff7a0053af38@mail.gmail.com> <48030439.9040309@archonet.com> In-Reply-To: <48030439.9040309@archonet.com> Content-Type: multipart/signed; protocol="application/x-pkcs7-signature"; micalg=sha1; boundary="------------ms000602060003080903010002" 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/179 X-Sequence-Number: 29672 This is a cryptographically signed message in MIME format. --------------ms000602060003080903010002 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Richard Huxton wrote: > Adam Gundy wrote: >> On Thu, Apr 10, 2008 at 8:52 AM, Adam Gundy wrote: >>> Richard Huxton wrote: >>>> 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 :-( >> >> OK, I'm thinking the varchar/char part is not the issue. > > Good, because it's not :-) hmm. unfortunately it did turn out to be (part) of the issue. I've discovered that mixing char and varchar in a stored procedure does not coerce the types, and ends up doing seq scans all the time. changing something like this: proc x ( y char(32) ) { select * from groups where groupid = y } into this: proc x ( y varchar(32) ) { select * from groups where groupid = y } and suddenly postgres does index lookups in the stored proc... way faster. >> I *think* that when I analyze using char instead of varchar, it is >> recording a stat for the large group, but for some reason with varchar >> doesn't add a stat for that one. >> >> so, the real question is, how do I fix this? I can turn the stats way >> up to 1000, but that doesn't guarantee that I'll get a stat for the >> large group :-( > > Well, by default it will be tracking the 10 most common values (and how > often they occur). As you say, this can be increased to 1000 (although > it obviously takes longer to check 1000 rather than 10). > > We can have a look at the stats with something like: > SELECT * FROM pg_stats WHERE tablename='group_access' AND attname='uid'; > You'll be interested in n_distinct, most_common_vals and most_common_freqs. > > However, I think the problem may be that PG doesn't track cross-column > stats, so it doesn't know that a particular uid implies one or more > particular groupid values. I doubt we could get stats stable enough for this. the number of groups will hopefully be much larger at some point. it's a shame the index entries can't be used to provide information to the planner, eg a rough count of the number of entries for a given key (or subset). it would be nice to be able to create eg a counted btree when you know you have this kind of data as a hint to the planner. >> can I turn the statistics off completely for this column? I'm guessing >> that if I can, that will mean it takes a guess based on the number of >> distinct values in the groups table, which is still large number of >> records, possibly enough to trigger the seqscan anyway. > > No - can't disable stats. Besides, you want it the other way around - > index scans for all groups except the largest. actually, disabling seqscan at the server level gives extremely good response times. I ended up rewriting a few queries that were scanning the whole group for no good reason, and bitmap index hashing seems to take care of things nicely. queries have gone from 30+ seconds to < 0.1 seconds. >> does postgres have a way of building a 'counted index' that the >> planner can use for it's record counts? some way of forcibly >> maintaining a stat for every group? > > No, but let's see what's in pg_stats. no real help there. either it hits the group being read, and does a good plan, or it doesn't, and tries to seqscan (unless I disable it). even forcing stats to 1000 only bandaids the situation, given the number of groups will eventually exceed that.. --------------ms000602060003080903010002 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 A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