Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id CC2D6635A8B for ; Sun, 22 Mar 2009 21:13:33 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 04290-03 for ; Sun, 22 Mar 2009 21:13:20 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f167.google.com (mail-ew0-f167.google.com [209.85.219.167]) by mail.postgresql.org (Postfix) with ESMTP id E1F8A6350CC for ; Sun, 22 Mar 2009 21:12:31 -0300 (ADT) Received: by ewy11 with SMTP id 11so2758784ewy.19 for ; Sun, 22 Mar 2009 17:12:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :received:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=nPTjIKoydvg+ngkJFQElfTKWMPhwnTtRP2A1Mzeu8Eg=; b=He4tk55cCA4NgTfSjKyn4d+e0FjsenpY7MlvWashEmPfGaUxf7JCr5MXbiH0zwBexc vA/TIqZ8gE7qvsy/vALAVxvJyM0rvWX3uo+8j/l4Zzri73GdNJfzvMZ/HZjCwPxNfg14 97iz4bai48j6t6RqPNfjht8p2oEqXK1+KwyF8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=vusFq/RhX93/VCuPxj319iCFgfBsbjXRFUlzpOZZF8Xo7jukUI5ceu264NFmGyRvo9 QZoKPXaNoS5KwvF63eO06/Q1LZSR1fIPyGa/R0KDs5/uYFsUvfKK2y0N8ndF55PRCXXO Vt82Tc4G1fkTnyU5xewy6qzvj4oxt0N8EiAcg= MIME-Version: 1.0 In-Reply-To: <17814.1237757356@sss.pgh.pa.us> References: <49A295F0.5010405@saparev.com> <49A2A450.20002@archonet.com> <49A2DFFA.8000001@saparev.com> <18725.1235412110@sss.pgh.pa.us> <49A429A3.9010803@saparev.com> <17814.1237757356@sss.pgh.pa.us> Date: Mon, 23 Mar 2009 01:12:14 +0100 Received: by 10.211.194.4 with SMTP id w4mr4077386ebp.99.1237767149741; Sun, 22 Mar 2009 17:12:29 -0700 (PDT) Message-ID: Subject: Re: LIMIT confuses the planner From: marcin mank To: Tom Lane Cc: kouber@saparev.com, pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 200903/322 X-Sequence-Number: 33244 > So the bottom line here is just that the estimated n_distinct is too > low. =A0We've seen before that the equation we use tends to do that more > often than not. =A0I doubt that consistently erring on the high side woul= d > be better though :-(. =A0Estimating n_distinct from a limited sample of > the population is known to be a statistically hard problem, so we'll > probably not ever have perfect answers, but doing better is on the > to-do list. > I hit an interestinhg paper on n_distinct calculation: http://www.pittsburgh.intel-research.net/people/gibbons/papers/distinct-val= ues-chapter.pdf the PCSA algorithm described there requires O(1) calculation per value. Page 22 describes what to do with updates streams. This I think (disclaimer: I know little about PG internals) means that the n_distinct estimation can be done during vacuum time (it would play well with the visibility map addon). What do You think? Greetings Marcin