X-Original-To: pgsql-admin@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 4773B475FBA for ; Sun, 20 Apr 2003 10:58:47 -0400 (EDT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id 8A359475D3B for ; Sun, 20 Apr 2003 10:58:46 -0400 (EDT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.9/8.12.9) with ESMTP id h3KEwcU6026877; Sun, 20 Apr 2003 10:58:38 -0400 (EDT) To: kp Cc: pgsql-admin@postgresql.org Subject: Re: Sequence scans on indexed row In-reply-to: <3EA231D2.2040305@pobox.gr> References: <3EA231D2.2040305@pobox.gr> Comments: In-reply-to kp message dated "Sun, 20 Apr 2003 07:36:18 +0200" Date: Sun, 20 Apr 2003 10:58:38 -0400 Message-ID: <26876.1050850718@sss.pgh.pa.us> From: Tom Lane X-Spam-Status: No, hits=-32.3 required=5.0 tests=BAYES_00,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REFERENCES,REPLY_WITH_QUOTES autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/282 X-Sequence-Number: 8437 kp writes: > The other difference between these two columns (or rather the kind of > data they contain) is that the column for which postgres *uses* the > index on contains around 32000 distinct values while the other only > contains 14 distinct values. I think it's doing what it's supposed to, then. Using an indexscan to select 1/14th of a table is a loser --- the seqscan will be faster. (If you disbelieve this, try timing it both ways. You can set enable_seqscan to off to force the planner to do it the other way.) > I have a suspicion that postgres (i'm using 7.1.3) incorrectly assumes > that a sequence scan is cheaper for the column with the 14 distinct > values in it and ends up scanning 1.6 million rows. The only way it's wrong is if the particular value being searched for is much less than 1/14th of the table. IIRC Postgres 7.1 does not have statistics about anything beyond the most common value of the column, and so it's easily misled by nonuniform data distributions. If that's your situation then an update to 7.2.* or 7.3.* seems called for. regards, tom lane