X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 7561CD1B460 for ; Fri, 5 Dec 2003 08:09:13 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 03299-10 for ; Fri, 5 Dec 2003 04:08:43 -0400 (AST) Received: from ar-sd.net (unknown [81.196.32.112]) by svr1.postgresql.org (Postfix) with ESMTP id 81837D1B474 for ; Fri, 5 Dec 2003 04:08:40 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by ar-sd.net (Postfix) with ESMTP id 262561C8D4; Fri, 5 Dec 2003 10:11:48 +0200 (EET) Received: from andy (unknown [192.168.0.11]) by ar-sd.net (Postfix) with SMTP id 055C917723; Fri, 5 Dec 2003 10:11:48 +0200 (EET) Message-ID: <00c901c3bb07$592bbe30$0b00a8c0@andy> From: "Andrei Bintintan" To: "Stephan Szabo" Cc: References: <014901c3ba76$ffd65650$0b00a8c0@andy> <20031204071432.R66123@megazone.bigpanda.com> Subject: Re: [ADMIN] Index not used. WHY? Date: Fri, 5 Dec 2003 10:11:11 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4927.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4927.1200 X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/117 X-Sequence-Number: 4977 There are around 700 rows in this table. If I set enable_seqscan=off then the index is used and I also used Vacuum Analyze recently. I find it strange because the number of values of id_user and id_modull are somehow in the same distribution and when I search the table the id_user index is used but the id_modull index is not used. Does somehow postgre know that a seq scan runs faster in this case as a index scan? Should I erase this index? I have to say that the data's in this table are not changed offen, but there are a LOT of joins made with this table. Best regards. Andy. ----- Original Message ----- From: "Stephan Szabo" To: "Andrei Bintintan" Cc: ; Sent: Thursday, December 04, 2003 5:19 PM Subject: Re: [ADMIN] Index not used. WHY? > > On Thu, 4 Dec 2003, Andrei Bintintan wrote: > > > Hi, > > > > I have the following table: > > CREATE TABLE public.rights ( > > id int4 DEFAULT nextval('"rights_id_seq"'::text) NOT NULL, > > id_user int4 NOT NULL, > > id_modull int4 NOT NULL, > > CONSTRAINT rights_pkey PRIMARY KEY (id) > > ) > > > > and I created the following indexes: > > > > CREATE INDEX right_id_modull_idx ON rights USING btree (id_modull); > > CREATE INDEX right_id_user_idx ON rights USING btree (id_user); > > > > Now the problem: > > > > EXPLAIN SELECT * FROM rights r WHERE r.id_modull =15 > > returnes: > > Seq Scan on rights r (cost=0.00..12.30 rows=42 width=12) > > Filter: (id_modull = 15) > > > > EXPLAIN SELECT * FROM rights r WHERE r.id_user =15 > > returnes: > > Index Scan using right_id_user_idx on rights r (cost=0.00..8.35 rows=11 width=12) > > Index Cond: (id_user = 15) > > > > Question: Why the right_id_modull_idx is NOT USED at the 1st query and > > the second query the right_id_user_idx index is used. > > As a note, pgsql-performance is a better list for these questions. > > So, standard questions: > > How many rows are in the table, what does EXPLAIN ANALYZE show for the > queries, if you force index usage (set enable_seqscan=off) on the first > what does EXPLAIN ANALYZE show then, have you used ANALYZE/VACUUM ANALYZE > recently? >