pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Arcadio Ortega Reinoso <arcadio.ortega@gmail.com>
To: pgsql-performance@lists.postgresql.org
Subject: PostgreSQL does not choose my indexes well
Date: Thu, 23 Apr 2020 13:36:43 +0200
Message-ID: <35dc90d6-e0c2-13b8-3184-e3db5d854a78@gmail.com> (raw)

Hi,

I don't understand why postgresql doesn't use clearly the most optimal 
index for a query in PLAN.

Can you help me?


create table public.tabla
(
     cod_tabla bigint not null,
     tabla varchar(31) not null,
     constraint pk_tabla primary key (cod_tabla)
);


create table public.entidad
(
     cod_entidad bigint not null,
     cod_tabla bigint not null,
     cod_entidad_tabla bigint not null,
     constraint pk_entidad primary key (cod_entidad),
     constraint fk_tabla_entidad foreign key (cod_tabla)
         references public.tabla (cod_tabla) match simple
         on update cascade
         on delete cascade
);


CREATE INDEX idx_tabla_entidad
     ON public.entidad USING btree
     (cod_tabla ASC NULLS LAST);

CREATE INDEX idx_entidad_tabla_4
     ON public.entidad USING btree
     (cod_entidad_tabla ASC NULLS LAST)
     INCLUDE(cod_entidad, cod_tabla, cod_entidad_tabla)
     WHERE cod_tabla::bigint = 4;




SELECT count(*) from entidad;
34.413.354

SELECT count(*) from entidad where cod_tabla = 4;
1.409.985



explain (analyze, buffers, format text) select * from entidad where 
cod_tabla = 4


Index Scan using idx_tabla_entidad on entidad (cost=0.56..51121.41 
rows=1405216 width=20) (actual time=0.037..242.609 rows=1409985 loops=1)
   Index Cond: ((cod_tabla)::bigint = 4)
   Buffers: shared hit=12839
Planning Time: 0.158 ms
Execution Time: 311.828 ms



Why postgresql doesnt use the index idx_entidad_tabla_4?????

Thanks in advance







view thread (21+ messages)  latest in thread

Message-ID: <35dc90d6-e0c2-13b8-3184-e3db5d854a78@gmail.com>
Permalink:  ../35dc90d6-e0c2-13b8-3184-e3db5d854a78@gmail.com/
Also on:    postgresql.org/message-id/35dc90d6-e0c2-13b8-3184-e3db5d854a78@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: arcadio.ortega@gmail.com, pgsql-performance@lists.postgresql.org
  Subject: Re: PostgreSQL does not choose my indexes well
  In-Reply-To: <35dc90d6-e0c2-13b8-3184-e3db5d854a78@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox