X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2B3B45E46C3 for ; Mon, 30 Aug 2004 17:48:11 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 37643-03 for ; Mon, 30 Aug 2004 20:48:06 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 8E3DF5E3632 for ; Mon, 30 Aug 2004 17:48:03 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i7UKm5Xv011310; Mon, 30 Aug 2004 16:48:05 -0400 (EDT) To: Martin Sarsale Cc: pgsql-performance@postgresql.org Subject: Re: seqscan instead of index scan In-reply-to: <1093887997.1680.71.camel@kadaif> References: <1093887997.1680.71.camel@kadaif> Comments: In-reply-to Martin Sarsale message dated "Mon, 30 Aug 2004 14:46:37 -0300" Date: Mon, 30 Aug 2004 16:48:05 -0400 Message-ID: <11309.1093898885@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/460 X-Sequence-Number: 8096 Martin Sarsale writes: > I indexed columns c and d (separately) but this query used the slow > seqscan instead of the index scan: > select * from t where c<>0 or d<>0; > After playing some time, I noticed that if I change the "or" for an > "and", pg used the fast index scan (but the query w/ 'and' was not what > I as looking for). I don't think so. <> is not an indexable operator --- it appears nowhere in the index operator classes. It would help if you showed us *exactly* what you did instead of a not-very-accurate filtered version. I'm going to assume that you meant > ... > Now: I've no idea why it chooses to use a seq scan instead of the index > scan (yes, I've just vacuum analyzed the table before running the > query). Because 7.4 doesn't have statistics about expression indexes, so it has no idea that there are only a few rows with c+d > 0. What I'd suggest is select * from t where c>0 union select * from t where d>0 with separate indexes on c and d. Another possibility is a partial index on exactly the condition you want: create index nonzero on t(c) where c>0 or d>0; although I'm not certain if 7.4 has enough stats to recognize this as a win. regards, tom lane