X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C69AF3A6B4C; Fri, 10 Dec 2004 19:38:46 +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 15785-01; Fri, 10 Dec 2004 19:38:08 +0000 (GMT) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by svr1.postgresql.org (Postfix) with ESMTP id D31AF3A759A; Fri, 10 Dec 2004 18:30:27 +0000 (GMT) Received: from junk.nocrew.org (junk.nocrew.org [213.242.147.30]) by svr4.postgresql.org (Postfix) with ESMTP id 2E6F25B3FCE; Fri, 10 Dec 2004 11:40:54 +0000 (GMT) Received: from tomas by junk.nocrew.org with local (Exim 3.35 #1 (Debian)) id 1Ccj8k-00006d-00; Fri, 10 Dec 2004 12:40:50 +0100 To: pgsql-general@postgresql.org Cc: pgsql-performance@postgresql.org Subject: Query is not using index when it should From: tomas@nocrew.org (Tomas =?iso-8859-1?q?Sk=E4re?=) Date: 10 Dec 2004 12:40:50 +0100 Message-ID: <80oeh2tm7x.fsf@junk.nocrew.org> Lines: 61 User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200412/500 X-Sequence-Number: 70112 I tried to subscribe to pgsql-performance, but there seems to be something wrong with the majordomo, so I'm sending to general too, where I'm already subscribed. My problem is this, using PostgreSQL 7.4.6: I have a table that looks like this: Table "public.cjm_object" Column | Type | Modifiers -----------+-------------------+----------- timestamp | bigint | not null jobid | bigint | not null objectid | bigint | not null class | integer | not null field | character varying | not null data | bytea | Indexes: "cjm_object_pkey" primary key, btree ("timestamp", jobid, objectid, "class", field) "idx_cjm_object1" btree (objectid, "class", field) The table has 283465 rows, and the column combination (objectid,class,field) can occur several times. Doing a search with all columns in the pkey works, it uses the index: db=# explain analyze select * from cjm_object where timestamp=1102497954815296 and jobid=9 and objectid=4534 and class=12 and field='paroid'; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ Index Scan using cjm_object_pkey on cjm_object (cost=0.00..32.75 rows=1 width=54) (actual time=0.169..0.172 rows=1 loops=1) Index Cond: ("timestamp" = 1102497954815296::bigint) Filter: ((jobid = 9) AND (objectid = 4534) AND ("class" = 12) AND ((field)::text = 'paroid'::text)) Total runtime: 0.381 ms (4 rows) But when doing a search with objectid, class and field, it doesn't use the idx_cjm_object1 index. db=# explain analyze select * from cjm_object where objectid=4534 and class=12 and field='paroid'; QUERY PLAN ----------------------------------------------------------------------------------------------------------- Seq Scan on cjm_object (cost=0.00..7987.83 rows=2 width=54) (actual time=21.660..475.664 rows=1 loops=1) Filter: ((objectid = 4534) AND ("class" = 12) AND ((field)::text = 'paroid'::text)) Total runtime: 475.815 ms (3 rows) I have tried to set enable_seqscan to false, but it gives the same result, except that the estimated cost is higher. I have also done a vacuum full analyze, and I have reindexed the database, the table and the index. I have dropped the index and recreated it, but it still gives the same result. Please, could someone give me a clue to this? Tomas