Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 2D36763332D for ; Wed, 7 Jul 2010 18:03:37 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 96136-10 for ; Wed, 7 Jul 2010 21:03:29 +0000 (UTC) X-Greylist: delayed 00:08:38.38898 by SQLgrey-1.7.6 Received: from smtp150.iad.emailsrvr.com (smtp150.iad.emailsrvr.com [207.97.245.150]) by mail.postgresql.org (Postfix) with ESMTP id F2D0A6332D9 for ; Wed, 7 Jul 2010 18:03:28 -0300 (ADT) Received: from relay15.relay.iad.mlsrvr.com (localhost [127.0.0.1]) by relay15.relay.iad.mlsrvr.com (SMTP Server) with ESMTP id 217CF1B4066 for ; Wed, 7 Jul 2010 16:54:49 -0400 (EDT) Received: by relay15.relay.iad.mlsrvr.com (Authenticated sender: jerry.gamache-AT-idilia.com) with ESMTPSA id 07BE71B4381 for ; Wed, 7 Jul 2010 16:54:48 -0400 (EDT) Message-ID: <4C34E998.3020000@idilia.com> Date: Wed, 07 Jul 2010 16:54:48 -0400 From: Jerry Gamache User-Agent: Thunderbird 2.0.0.19 (X11/20090316) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Logarithmic data frequency distributions and the query planner Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.1 tagged_above=-10 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201007/96 X-Sequence-Number: 39485 On 8.1, I have a very interesting database where the distributions of some values in a multi-million rows table is logarithmic (i.e. the most frequent value is an order of magnitude more frequent than the next ones). If I analyze the table, the statistics become extremely skewed towards the most frequent values and this prevents the planner from giving any good results on queries that do not target these entries. In a recent case, the planner estimated that the number of returned rows would be ~13% of the table size and from this bad assumption generated a very slow conservative plan that executed in days. If I set the statistics at zero for that table, the planner uses a hardcoded ratio (seems like 0.5%) for the number of returned rows and this helps generating a plan that executes in 3 minutes (still sub-optimal, but not as bad). Generating partial index for the less frequent cases helps, but this solution is not flexible enough for our needs as the number of complex queries grow. We are mostly left with pre-generating a lot of temporary tables whenever the planner over-evaluates the number of rows generated by a subquery (query execution was trimmed from 3 minutes to 30 seconds using this technique) or using the OFFSET 0 tweak, but it would be nice if the planner could handle this on its own. Am I missing something obvious? Setting the statistics for this table to zero seems awkward even if it gives good results. Jerry.