Re: 8.4 index no funciona con 158 mil registros

From: Jaime Casanova <jaime(at)2ndquadrant(dot)com>
To: William Diaz Pabón <widipa(at)gmail(dot)com>
Cc: PostgreSQL Español Ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: 8.4 index no funciona con 158 mil registros
Date: 2013-06-26 03:56:02
Message-ID: CAJKUy5hKCMJJHcyfEL4s7uSj1ewz9=7b-Y+rDqS+92omKBD5uQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

2013/6/25 William Diaz Pabón <widipa(at)gmail(dot)com>:
>
> Donde tengo una tabla con un campo estado que tiene esta constraint
>
> CHECK (estado::text = 'ACTIVO'::text OR estado::text = 'INACTIVO'::text OR
> estado::text = 'PAGADO'::text OR estado::text = 'CANJE'::text OR
> estado::text = 'LIQUIDADO'::text OR estado::text = 'CONVENIO'::text OR
> estado::text = 'REFINANCIADO'::text OR estado::text = 'PRESCRITO'::text),
>

Uh! esto esta bastante feo... quizá quieras ver sobre los enumerados:
http://www.postgresql.org/docs/8.4/static/datatype-enum.html

> cuando hago un select donde el where hace estado = 'INACTIVO' y hago un
> explain query me indica que esta usando el indice.
>
> pero cuando hago estado = 'ACTIVO' y hago un explain query me indica que
> NO esta usando el indice. y me dice que hay 158 mil filas.
>
> como se hace para que sin importar la cantidad de registros use siempre el
> indice?
>

y para que querrías hacer eso? la idea es optimizar, no desoptimizar.
Y no, usar un índice no siempre es la mejor idea.

como te comento Rodrigo, deberias mostrarnos cuantos registros hay por
cada estado para poder saber si lo que está pasando es lógico o no.
además, podrías ejecutar un EXPLAIN ANALYZE de la consulta con
INACTIVO, con ACTIVO y ejecutar SET enable_seqscan TO OFF; y luego
otra vez el EXPLAIN ANALYZE con ACTIVO.

eso debería dar suficiente información para emitir un criterio informado.

--
Jaime Casanova www.2ndQuadrant.com
Professional PostgreSQL: Soporte 24x7 y capacitación
Phone: +593 4 5107566 Cell: +593 987171157

-
Enviado a la lista de correo pgsql-es-ayuda (pgsql-es-ayuda(at)postgresql(dot)org)
Para cambiar tu suscripción:
http://www.postgresql.org/mailpref/pgsql-es-ayuda

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Ruben Fitó 2013-06-26 06:55:10 Re: Guardar y obtener archivos en una BBDD Postgres en c
Previous Message Rodrigo Gonzalez 2013-06-25 22:48:11 Re: 8.4 index no funciona con 158 mil registros