Thread: Yet another question about not use on indexes

Yet another question about not use on indexes

From
Xevi Serrats
Date:
Hi,

I have created a table whit some indexes. I analize
the query of this table and never use index.

After this, I create a more simplistic table with two
columns and one index and the query uses the index.

Look at this:

pfc=# \d document
                                  Table
"public.document"
   Column   |           Type           |
      Modifiers
------------+--------------------------+----------------------------------------------------
 codi       | integer                  | not null
default nextval('seq_document'::text)
 nom        | character varying(32)    | not null
 descripcio | text                     |
 formulari  | integer                  |
 fitxer     | character varying(32)    |
 tamany     | integer                  | default -1
 data       | timestamp with time zone | default
('now'::text)::timestamp(6) with time zone
Indexes: document_pkey primary key btree (codi),
         ind_doc1 btree (codi),
         ind_document btree (formulari)
Triggers: RI_ConstraintTrigger_19414,
          RI_ConstraintTrigger_19418,
          RI_ConstraintTrigger_19419,
          actualitzaritemcercadocument,
          altaitemcercadocument,
          baixaitemcercadocument,
          eliminaracldocument,
          eliminaravaluaciodocument

pfc=# explain select * from document where codi=2;
                        QUERY PLAN
----------------------------------------------------------
 Seq Scan on document  (cost=0.00..1.19 rows=1
width=120)
   Filter: (codi = 2)
(2 rows)

This query must use index document_pkey but explain
tells us that the query does a Sequencial scan on
table document.

Look at this simplistic case:

pfc=# \d prova
            Table "public.prova"
 Column |         Type          | Modifiers
--------+-----------------------+-----------
 codi   | integer               | not null
 nom    | character varying(30) |
Indexes: prova_pkey primary key btree (codi)

pfc=# explain select * from prova where codi=1234;
                               QUERY PLAN
-------------------------------------------------------------------------
 Index Scan using prova_pkey on prova
(cost=0.00..5.99 rows=1 width=37)
   Index Cond: (codi = 1234)
(2 rows)

Now the query uses index, explain tell something about
index scan using index prova_pkey.

What is the diference with two cases? What must I do?
It is a bug? I need do something else?

Thanks a lot for helping me.

Regards,

Xevi.

_______________________________________________________________
Yahoo! Messenger
Nueva versión: Webcam, voz, y mucho más ¡Gratis!
Descárgalo ya desde http://messenger.yahoo.es


Re: Yet another question about not use on indexes

From
Richard Huxton
Date:
On Sunday 11 May 2003 2:16 pm, Xevi Serrats wrote:
> Hi,
>
> I have created a table whit some indexes. I analize
> the query of this table and never use index.

> pfc=# \d document
>                                   Table
> "public.document"
>    Column   |           Type           |
>       Modifiers
> ------------+--------------------------+-----------------------------------
>----------------- codi       | integer                  | not null
> default nextval('seq_document'::text)
>  nom        | character varying(32)    | not null
etc...

> pfc=# explain select * from document where codi=2;
>                         QUERY PLAN
> ----------------------------------------------------------
>  Seq Scan on document  (cost=0.00..1.19 rows=1
> width=120)
>    Filter: (codi = 2)
> (2 rows)

1. Have you done a VACUUM ANALYSE?
2. How many rows are in this table?
3. Can you post the output of EXPLAIN ANALYSE SELECT... - that actually runs
the query.

--
  Richard Huxton


Re: Yet another question about not use on indexes

From
Tom Lane
Date:
=?iso-8859-1?q?Xevi=20Serrats?= <tomcatbsd@yahoo.es> writes:
> pfc=# explain select * from document where codi=2;
>                         QUERY PLAN
> ----------------------------------------------------------
>  Seq Scan on document  (cost=0.00..1.19 rows=1 width=120)
>    Filter: (codi = 2)
> (2 rows)

Judging from the cost estimate, this table is too small to bother with
an indexscan.

            regards, tom lane