Re: Query lenta

From: FRANCISCO JOSE PALAO VILLANUEVA <fjpv_2000(at)yahoo(dot)es>
To: Lennin Caro <lennin(dot)caro(at)yahoo(dot)com>, "pgsql-es-ayuda(at)postgresql(dot)org" <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Query lenta
Date: 2014-02-12 11:41:00
Message-ID: 1392205260.77337.YahooMailNeo@web172601.mail.ir2.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Hola,
 el explain analyze con >= '01/12/2013'

'Aggregate  (cost=545557.78..545557.79 rows=1 width=10) (actual time=152389.058..152389.059 rows=1 loops=1)'
'  ->  Hash Join  (cost=262267.42..544984.31 rows=114695 width=10) (actual time=144506.241..151553.263 rows=673133 loops=1)'
'        Hash Cond: ((t1.av_id = t2.av_id) AND (t1.av_ov = t2.av_ov))'
'        ->  Index Scan using avavd on a_v t1  (cost=0.42..5248.01 rows=114546 width=6) (actual time=58.424..5261.424 rows=116009 loops=1)'
'              Index Cond: (av_d >= '2013-12-01'::date)'
'        ->  Hash  (cost=135397.40..135397.40 rows=6380840 width=16) (actual time=144124.583..144124.583 rows=6380667 loops=1)'
'              Buckets: 4096  Batches: 512  Memory Usage: 662kB'
'              ->  Seq Scan on a_dv t2  (cost=0.00..135397.40 rows=6380840 width=16) (actual time=19.458..125134.296 rows=6380667 loops=1)'
'Total runtime: 152389.832 ms'

el explain analyze con = '02/12/2013'

'Aggregate  (cost=14273.12..14273.13 rows=1 width=10) (actual time=1156.302..1156.304 rows=1 loops=1)'
'  ->  Nested Loop  (cost=0.86..14264.50 rows=1724 width=10) (actual time=57.399..1104.501 rows=17692 loops=1)'
'        ->  Index Scan using avavd on a_v t1  (cost=0.42..83.46 rows=1722 width=6) (actual time=0.020..6.738 rows=3144 loops=1)'
'              Index Cond: (av_d = '2013-12-02'::date)'
'        ->  Index Scan using advavidav_ov on a_dv t2  (cost=0.43..8.23 rows=1 width=16) (actual time=0.300..0.322 rows=6 loops=3144)'
'              Index Cond: ((av_id = t1.av_id) AND (av_ov = t1.av_ov))'
'Total runtime: 1156.437 ms'

Como ves la diferencia es brutal Saludos

________________________________
De: Lennin Caro <lennin(dot)caro(at)yahoo(dot)com>
Para: FRANCISCO JOSE PALAO VILLANUEVA <fjpv_2000(at)yahoo(dot)es>; "pgsql-es-ayuda(at)postgresql(dot)org" <pgsql-es-ayuda(at)postgresql(dot)org>
Enviado: Martes 11 de febrero de 2014 21:05
Asunto: Re: [pgsql-es-ayuda] Query lenta

puedes sacar un explain (buffers, analyze) de la consulta con el = y el >=

 
---------------------------------------------------------------------------------------
Ing. Lennin Caro Pérez
Usuario:GNU/LINUX
PHP Developer
PostgreSQL DBA
Oracle DBA
Linux counter id 474393

On Tuesday, February 11, 2014 3:16 PM, FRANCISCO JOSE PALAO VILLANUEVA <fjpv_2000(at)yahoo(dot)es> wrote:

Hola,
av_d es del tipo Date y la versión de postgres es la 9.3.2

Saludos

________________________________
De: Lennin Caro <lennin(dot)caro(at)yahoo(dot)com>
Para: FRANCISCO JOSE PALAO VILLANUEVA <fjpv_2000(at)yahoo(dot)es>; "pgsql-es-ayuda(at)postgresql(dot)org" <pgsql-es-ayuda(at)postgresql(dot)org>
Enviado: Martes 11 de febrero de 2014 20:11
Asunto: Re: [pgsql-es-ayuda] Query lenta

On Tuesday, February 11, 2014 2:01 PM, FRANCISCO JOSE PALAO VILLANUEVA <fjpv_2000(at)yahoo(dot)es> wrote:

Hola haber si me pueden ayudar:

Tengo una relación maestro detalle entre cabeceras de ventas y detalles de ventas. Tengo un view 'reimp' que enlaza las dos tablas mediante cod_factura y cod_oficina.

Si ejecuto un select sum(adv_q*adv_up) from reimp where av_d >= '01/12/2013' hace un escaneado secuencial de la tabla detalles
La ejecución puede demorarse hasta en 105 segundos ???, sin embargo si hago
 select sum(adv_q*adv_up) from reimp where av_d = '01/12/2013' entonces usa índices de tabla detalles y es casi inmediato si hago un between y las filas están ya cacheadas también usa índices sino no. Les pongo el explain haber si me pueden ayudar:

'Aggregate  (cost=545557.78..545557.79 rows=1 width=10)'
'  ->  Hash Join  (cost=262267.42..544984.31 rows=114695 width=10)'
'        Hash Cond: ((t1.av_id = t2.av_id) AND (t1.av_ov = t2.av_ov))'
'        ->  Index Scan using avavd on a_v t1  (cost=0.42..5248.01 rows=114546 width=6)'
'              Index Cond: (av_d >= '2013-12-01'::date)'
'        ->  Hash  (cost=135397.40..135397.40 rows=6380840 width=16)'
'              ->  Seq Scan on a_dv t2  (cost=0.00..135397.40 rows=6380840 width=16)'

La tabla detalles tiene índices sobre (av_id,av_ov), sobre av_id  y sobre av_ov. La tabla cabeceras los mismos, además de otros campos.

Saludos
Que tipo de dato es el de la columna av_d y por cierto que version de postgresql tienes.

 
---------------------------------------------------------------------------------------
Ing. Lennin Caro Pérez
Usuario:GNU/LINUX
PHP Developer
PostgreSQL DBA
Oracle DBA
Linux counter id 474393

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message FRANCISCO JOSE PALAO VILLANUEVA 2014-02-12 12:16:13 Re: Query lenta
Previous Message Alvaro Herrera 2014-02-12 03:14:45 Re: CASE en ORDER BY