Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id ABA4A21B8F9 for ; Fri, 11 Nov 2011 11:36:33 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 99905-06 for ; Fri, 11 Nov 2011 15:36:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id DB77421B8F8 for ; Fri, 11 Nov 2011 11:36:27 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id pABFaQJq005468; Fri, 11 Nov 2011 10:36:26 -0500 (EST) To: Ruslan Zakirov cc: pgsql-performance@postgresql.org Subject: Re: avoiding seq scans when two columns are very correlated In-reply-to: References: Comments: In-reply-to Ruslan Zakirov message dated "Fri, 11 Nov 2011 19:01:41 +0400" Date: Fri, 11 Nov 2011 10:36:26 -0500 Message-ID: <5467.1321025786@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.101 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.201 X-Spam-Level: X-Archive-Number: 201111/137 X-Sequence-Number: 45648 Ruslan Zakirov writes: > A table has two columns id and EffectiveId. First is primary key. > EffectiveId is almost always equal to id (95%) unless records are > merged. Many queries have id = EffectiveId condition. Both columns are > very distinct and Pg reasonably decides that condition has very low > selectivity and picks sequence scan. I think the only way is to rethink your data representation. PG doesn't have cross-column statistics at all, and even if it did, you'd be asking for an estimate of conditions in the "long tail" of the distribution. That's unlikely to be very accurate. Consider adding a "merged" boolean, or defining effectiveid differently. For instance you could set it to null in unmerged records; then you could get the equivalent of the current meaning with COALESCE(effectiveid, id). In either case, PG would then have statistics that bear directly on the question of how many merged vs unmerged records there are. regards, tom lane