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 E467B3A3CD5 for ; Thu, 4 Nov 2004 07:51:09 +0000 (GMT) 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 47889-06 for ; Thu, 4 Nov 2004 07:51:03 +0000 (GMT) Received: from smtp.ops.co.at (ops004.ops.co.at [194.152.182.4]) by svr1.postgresql.org (Postfix) with ESMTP id 7CF943A4863 for ; Thu, 4 Nov 2004 07:51:03 +0000 (GMT) Received: by smtp.ops.co.at (Postfix, from userid 65534) id 07D6423C0AC; Thu, 4 Nov 2004 08:50:59 +0100 (CET) Received: from [172.27.1.101] (ts1.int.ops.co.at [172.27.1.101]) by smtp.ops.co.at (Postfix) with ESMTP id F256023C0AB for ; Thu, 4 Nov 2004 08:50:56 +0100 (CET) Message-ID: <4189E086.5060400@ops.co.at> Date: Thu, 04 Nov 2004 08:55:50 +0100 From: Mario Ivankovits User-Agent: Mozilla Thunderbird 0.8 (Windows/20040913) X-Accept-Language: de-DE, de, en-us, en MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: index not used if using IN or OR Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 200411/35 X-Sequence-Number: 8989 Hello ! Sorry if this has been discussed before, it is just hard to find in the archives using the words "or" or "in" :-o I use postgres-8.0 beta4 for windows. I broke down my problem to a very simple table - two columns "primary_key" and "secondary_key". Creates and Insert you will find below. If I query the _empty_ freshly created table I get the following explain result: select * from tt where seckey = 1; Index Scan using seckey_key on tt (cost=0.00..17.07 rows=5 width=12) Index Cond: (seckey = 1) If I use "OR" (or IN) things get worse: select * from tt where seckey = 1 or seckey = 2 Seq Scan on tt (cost=0.00..0.00 rows=1 width=12) Filter: ((seckey = 1) OR (seckey = 2)) Note the "Seq Scan" instead of using the index. After populating the table with 8920 records and "analyze" the scenario gets even worser: select * from tt where seckey = 1; Seq Scan on tt (cost=0.00..168.50 rows=1669 width=12) (actual time=0.000..15.000 rows=1784 loops=1) Filter: (seckey = 1) Total runtime: 31.000 ms Now also this simple query uses a "Seq Scan". Now the questions are: a) Why is the index not used if I use "OR" or "IN" b) Why is the index not used after "analyze" ? Any help is very appreciated! Thanks, Mario // The table and data CREATE TABLE tt ( pkey int4 NOT NULL DEFAULT nextval('public."tt_PKEY_seq"'::text), seckey int8, CONSTRAINT pkey_key PRIMARY KEY (pkey) ) WITHOUT OIDS; CREATE INDEX seckey_key ON tt USING btree (seckey); // inserted many-many times insert into tt values (default, 1); insert into tt values (default, 2); insert into tt values (default, 3); insert into tt values (default, 4); insert into tt values (default, 5);